package leetcode;

/**
 * https://leetcode.com/problems/longest-uncommon-subsequence-ii/
 */
public class Problem522 {
    public int findLUSlength(String[] strs) {
        int max = 0;
        for (int i = 0; i < strs.length; i++) {
            for (int j = i + 1; j < strs.length; j++) {
                String a = strs[i];
                String b = strs[j];
                if (a.length() < b.length()) {
                    a = strs[j];
                    b = strs[i];
                }
                int count = 0;
                for (int x = 0; x < a.length(); x++) {
                    int y = 0;
                    while (y < b.length() && a.charAt(x) != b.charAt(y)) {
                        y++;
                    }
                    if (y < b.length()) {
                        count++;
                    }
                }
                max = Math.max(max, a.length() - count);
            }
        }
        return max;
    }

    public static void main(String[] args) {
        Problem522 prob = new Problem522();
//        System.out.println(prob.findLUSlength(new String[]{"aba", "cdc", "eae"})); // 3
        System.out.println(prob.findLUSlength(new String[]{"aba", "eae"})); // 2
//        System.out.println(prob.findLUSlength(new String[]{"aabbcc", "aabbcc","cb","abc"})); // 2
    }
}

@@ -12,16 +12,116 @@
 tcode;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;%0A%0A
 /**%0A * h
@@ -270,19 +270,50 @@
     
-int max = 0
+Map%3CString, Integer%3E map = new HashMap%3C%3E()
 ;%0A  
@@ -374,241 +374,155 @@
     
-for (int j = i + 1; j %3C strs.length; j++) %7B%0A                String a = strs%5Bi%5D;%0A                String b = strs%5Bj%5D;%0A                if (a.length() %3C b.length()) %7B%0A                    a = strs%5Bj%5D;%0A                    b = strs%5Bi%5D;%0A    
+if (!map.containsKey(strs%5Bi%5D)) %7B%0A                map.put(strs%5Bi%5D, 1);%0A            %7D else %7B%0A                map.put(strs%5Bi%5D, map.get(strs%5Bi%5D) + 1);%0A
     
@@ -535,32 +535,34 @@
   %7D%0A        
+%7D%0A
         
 int count = 
@@ -553,221 +553,180 @@
     
-int count = 0;%0A                for (int x = 0; x %3C a.length(); x++) %7B%0A                    int y = 0;%0A                    while (y %3C b.length() && a.charAt(x) != b.charAt(y)) %7B%0A                        y++;%0A        
+List%3CString%3E list = new ArrayList%3C%3E();%0A        for (String str : strs) %7B%0A            int count = map.get(str);%0A            if (count == 1) %7B%0A                list.add(str);%0A
     
@@ -739,32 +739,34 @@
   %7D%0A        
+%7D%0A
         
     if (y %3C 
@@ -757,161 +757,214 @@
     
-    if (y %3C b.length()) %7B%0A                        count++;%0A                    %7D%0A                %7D%0A                max = Math.max(max, a.length() - count
+int max = -1;%0A        for (int i = 0; i %3C list.size(); i++) %7B%0A            for (int j = i + 1; j %3C list.size(); j++) %7B%0A                max = Math.max(max, Math.max(list.get(i).length(), list.get(j).length())
 );%0A 
@@ -1192,16 +1192,18 @@
 ); // 3%0A
+//
         
@@ -1273,17 +1273,181 @@
 %7D)); // 
-2
+3%0A//        System.out.println(prob.findLUSlength(new String%5B%5D%7B%22xbc%22, %22acd%22%7D)); // 3%0A//        System.out.println(prob.findLUSlength(new String%5B%5D%7B%22abc%22, %22x%22%7D)); // 3
 %0A//     
@@ -1523,13 +1523,15 @@
 cc%22,
+ 
 %22cb%22,
+ 
 %22abc
@@ -1541,16 +1541,300 @@
 ); // 2%0A
+//        System.out.println(prob.findLUSlength(new String%5B%5D%7B%22abc%22, %22abc%22, %22xyzdef%22, %22xyzd%22%7D)); // 6%0A//        System.out.println(prob.findLUSlength(new String%5B%5D%7B%22aaa%22, %22aaa%22, %22aa%22%7D)); // -1%0A        System.out.println(prob.findLUSlength(new String%5B%5D%7B%22aabbcc%22, %22aabbcc%22, %22cb%22%7D)); // 2%0A
     %7D%0A%7D%0A

Update problem 522