11class Solution {
22 public List <List <String >> accountsMerge (List <List <String >> accounts ) {
3- List <List <String >> result = new ArrayList <>();
4-
5- int n = accounts .size ();
3+ UnionFind uf = new UnionFind (accounts .size ());
64
7- UnionFind uf = new UnionFind ( n );
5+ Map < String , Integer > emailToAcc = new HashMap <>( );
86
9- Map <String , Integer > emailToAccountIdx = new HashMap <>();
10-
11- /*
12- * If we've seen an e-mail before, it means that it belongs to a previous
13- * account! All we need to do is grab the idx of the account that it belongs to
14- * and update our parents[] array in our UnionFind object using the union()
15- * method
16- */
177 for (int i = 0 ; i < accounts .size (); i ++) {
188 for (int j = 1 ; j < accounts .get (i ).size (); j ++) {
199 String email = accounts .get (i ).get (j );
2010
21- if (emailToAccountIdx .containsKey (email )) {
22- /*
23- * If we've seen this e-mail before, it means that all e-mails in this new
24- * account actually belong to an existing user
25- *
26- * In this case, we can grab the account index of the user we've previously
27- * encountered and set it as the parent for this account
28- *
29- * For example, if account ID 2 already had "johnsmith@mail.com" and for account
30- * 7, we encounter "johnsmith@mail.com" once more, it means that ALL emails in
31- * account 7 actually belong to account 2!
32- *
33- * How do we represent this? By setting the parent of account 7 to point to
34- * account 2, or in more general terms, setting the parent ID of the current
35- * account to be the ID of the account for the email we've previously seen
36- */
37- int previousParent = emailToAccountIdx .get (email );
38- uf .union (previousParent , i );
11+ if (emailToAcc .containsKey (email )) {
12+ int prevParent = emailToAcc .get (email );
13+ uf .union (prevParent , i );
3914 } else {
40- emailToAccountIdx .put (email , i );
15+ emailToAcc .put (email , i );
4116 }
4217 }
4318 }
4419
45- Map <Integer , Set <String >> accountEmails = new HashMap <>();
20+ Map <Integer , Set <String >> emailsForAcc = new HashMap <>();
4621
4722 for (int i = 0 ; i < accounts .size (); i ++) {
48- int parent = uf .findParent (i );
23+ int parent = uf .find (i );
4924 List <String > emails = accounts .get (i );
5025
51- accountEmails .putIfAbsent (parent , new HashSet <>());
52- accountEmails .get (parent ).addAll (emails .subList (1 , emails .size ()));
26+ emailsForAcc .putIfAbsent (parent , new HashSet <>());
27+ emailsForAcc .get (parent ).addAll (emails .subList (1 , emails .size ()));
5328 }
5429
55- for (int key : accountEmails .keySet ()) {
30+ List <List <String >> result = new ArrayList <>();
31+
32+ for (int key : emailsForAcc .keySet ()) {
5633 List <String > temp = new ArrayList <>();
57- temp .addAll (accountEmails .get (key ));
34+ temp .addAll (emailsForAcc .get (key ));
5835 Collections .sort (temp );
5936 temp .add (0 , accounts .get (key ).get (0 ));
6037 result .add (temp );
@@ -69,23 +46,25 @@ private class UnionFind {
6946 public UnionFind (int n ) {
7047 parents = new int [n ];
7148
72- for (int i = 0 ; i < n ; i ++) {
49+ for (int i = 0 ; i < parents . length ; i ++) {
7350 parents [i ] = i ;
7451 }
7552 }
7653
77- public int findParent (int account ) {
78- if (parents [account ] == account ) {
79- return account ;
54+ public int find (int node ) {
55+ if (parents [node ] == node ) {
56+ return node ;
8057 }
81-
82- parents [account ] = findParent (parents [account ]);
83- return parents [account ];
58+ parents [node ] = find (parents [parents [node ]]);
59+ return parents [node ];
8460 }
8561
86- public void union (int p1 , int p2 ) {
87- int i = findParent (p1 ), j = findParent (p2 );
88- parents [j ] = i ;
62+ public void union (int i , int j ) {
63+ int p1 = find (i ), p2 = find (j );
64+ if (p1 == p2 ) {
65+ return ;
66+ }
67+ parents [p2 ] = p1 ;
8968 }
9069 }
91- }
70+ }
0 commit comments