Problem · Graph
Group Transitive String Aliases
Learn this problemProblem statement
You are given an array pairs, where each row contains two string aliases. Alias relationships are bidirectional and transitive: if a is an alias of b, and b is an alias of c, then all three strings belong to the same alias group.
Return every connected alias group. Within each group, sort all words lexicographically; the first word is therefore the key that the source associates with that group. Return the groups in lexicographic order by their first words.
Function
groupAliases(pairs: String[][]) → List<List<String>>Examples
Example 1
pairs = [["a","b"],["b","c"],["d","e"]]return = [["a","b","c"],["d","e"]]The first two pairs connect a, b, and c. The third pair forms the separate group [d,e].
Example 2
pairs = [["z","x"],["m","m"],["x","y"],["z","x"]]return = [["m"],["x","y","z"]]A self-pair keeps m as a one-word group. Repeated pairs do not change connectivity.
Constraints
1 <= pairs.length <= 10^5pairs[i].length == 2- Every alias is a non-empty printable-ASCII string of length at most
30. - Duplicate pairs and self-pairs are allowed.
More Apple problems
- Insert IntervalPHONE SCREEN · Seen Jul 2026
- Design Task ManagerPHONE SCREEN · Seen Jul 2026
- Unit Conversion IIPHONE SCREEN · Seen Jul 2026
- Two-Color an Undirected GraphPHONE SCREEN · Seen Jul 2026
- Word Search IIPHONE SCREEN · Seen Jul 2026
- Phone DirectoryPHONE SCREEN · Seen Jul 2026
- Product Except Self With ZerosONSITE INTERVIEW · Seen Jul 2026
- Subset Sum PossibleONSITE INTERVIEW · Seen Jul 2026