package leetcode;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * https://leetcode.com/problems/accounts-merge/
 */
public class Problem721 {
    public List<List<String>> accountsMerge(List<List<String>> accounts) {
        // TODO
        return null;
    }

    private static class UnionFind {
        private final Map<String, String> map = new HashMap<>();

        public void connect(String a, String b) {
            if (!map.containsKey(a)) {
                map.put(a, a);
            }
            if (!map.containsKey(b)) {
                map.put(b, b);
            }
            if (isConnected(a, b)) {
                return;
            }
            for (Map.Entry<String, String> entry : map.entrySet()) {
                if (entry.getValue().equals(map.get(a))) {
                    map.put(entry.getKey(), map.get(b));
                }
            }
        }

        public boolean isConnected(String a, String b) {
            if (!map.containsKey(a)) {
                return false;
            }
            if (!map.containsKey(b)) {
                return false;
            }
            return map.get(a).equals(map.get(b));
        }
    }

    public static void main(String[] args) {
        Problem721 prob = new Problem721();
        System.out.println(prob.accountsMerge(Arrays.asList(
            Arrays.asList(new String[]{"John", "johnsmith@mail.com", "john00@mail.com"}),
            Arrays.asList(new String[]{"John", "johnnybravo@mail.com"}),
            Arrays.asList(new String[]{"John", "johnsmith@mail.com", "john_newyork@mail.com"}),
            Arrays.asList(new String[]{"Mary", "mary@mail.com"})
        ))); // [["John", 'john00@mail.com', 'john_newyork@mail.com', 'johnsmith@mail.com'],  ["John", "johnnybravo@mail.com"], ["Mary", "mary@mail.com"]]
    }
}

@@ -279,15 +279,467 @@
     
-// TODO
+Map%3CString, UnionFind%3E map = new HashMap%3C%3E();%0A        for (List%3CString%3E account : accounts) %7B%0A            String name = account.get(0);%0A            if (account.size() %3E 1) %7B%0A                UnionFind uf = new UnionFind();%0A                String email = account.get(1);%0A                for (int i = 2; i %3C account.size(); i++) %7B%0A                    uf.connect(email, account.get(i));%0A                %7D%0A                map.put(name, uf);%0A            %7D%0A        %7D
 %0A   
@@ -1104,24 +1104,36 @@
 if (
-isConnected(a, b
+map.get(a).equals(map.get(b)
 )) %7B

Update problem 721