Graph BFS / DFS
LC #721Medium
Accounts Merge
Graph BFS / DFS
GoogleAmazonMetaLinkedInProblem
Merge accounts that share any email. Each account has a name and list of emails. Accounts belong to the same person if they share any email.
graphunion-finddfsstring
Constraints
- ›1 ≤ accounts.length ≤ 1000
- ›2 ≤ accounts[i].length ≤ 10
- ›1 ≤ accounts[i][j].length ≤ 30
- ›accounts[i][0] is a name (not email)
Example
Input
accounts = [["John","johnsmith@mail.com","john_newyork@mail.com"],["John","johnsmith@mail.com","john00@mail.com"],["Mary","mary@mail.com"]]Output
[["John","john00@mail.com","john_newyork@mail.com","johnsmith@mail.com"],["Mary","mary@mail.com"]]Why
Two John accounts share johnsmith@mail.com, so they merge