package leetcode;

import java.util.HashSet;
import java.util.Set;

/**
 * https://leetcode.com/problems/longest-uncommon-subsequence-ii/
 */
public class Problem522 {
    public int findLUSlength(String[] strs) {
        int max = -1;
        Set<String> ignored =  new HashSet<>();
        for (int i = 0; i < strs.length; i++) {
            for (int j = i + 1; j < strs.length; j++) {
                if (isSubsequence(strs[i], strs[j])) {
                    ignored.add(strs[i].length() <= strs[j].length() ? strs[i] : strs[j]);
                } else {
                    if (!ignored.contains(strs[i])) {
                        max = Math.max(max, strs[i].length());
                    }
                    if (!ignored.contains(strs[j])) {
                        max = Math.max(max, strs[j].length());
                    }
                }
            }
        }
        return max;
    }

    private static boolean isSubsequence(String a, String b) {
        if (a.equals(b)) {
            return true;
        }
        String str1 = (a.length() <= b.length()) ? a : b;
        String str2 = (a.length() > b.length()) ? a : b;
        int i = 0;
        int j = 0;
        for (; i < str1.length(); i++) {
            while (j < str2.length()) {
                if (str1.charAt(i) == str2.charAt(j)) {
                    j++;
                    break;
                }
                j++;
            }
            if (j == str2.length()) {
                break;
            }
        }
        if (i == str1.length() || str1.charAt(i) == str2.charAt(j - 1)) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        Problem522 prob = new Problem522();
//        System.out.println(isSubsequence("abcde", "xyz"));
//        System.out.println(isSubsequence("aaabbbccc", "abc"));
//        System.out.println(isSubsequence("aaabbbccc", "aabcc"));
//        System.out.println(isSubsequence("aaabbbccc", "aabccd"));
//        System.out.println(isSubsequence("abc", "xyz"));
//        System.out.println(isSubsequence("aaa", "acb"));
//        System.out.println(isSubsequence("aabbcc", "bcc"));
        System.out.println(prob.findLUSlength(new String[]{"aba", "cdc", "eae"})); // 3
        System.out.println(prob.findLUSlength(new String[]{"aba", "eae"})); // 3
        System.out.println(prob.findLUSlength(new String[]{"xbc", "acd"})); // 3
        System.out.println(prob.findLUSlength(new String[]{"abc", "x"})); // 3
        System.out.println(prob.findLUSlength(new String[]{"aabbcc", "aabbcc", "cb", "abc"})); // 2
        System.out.println(prob.findLUSlength(new String[]{"aabbcc", "aabbcc", "abc"})); // -1
        System.out.println(prob.findLUSlength(new String[]{"abc", "abc", "xyzdef", "xyzd"})); // 6
        System.out.println(prob.findLUSlength(new String[]{"aaa", "aaa", "aa"})); // -1
        System.out.println(prob.findLUSlength(new String[]{"aabbcc", "aabbcc", "cb"})); // 2
        System.out.println(prob.findLUSlength(new String[]{"aaa", "acb"})); // 3
        System.out.println(prob.findLUSlength(new String[]{"aabbcc", "aabbcc", "bc", "bcc"})); // -1
        System.out.println(prob.findLUSlength(new String[]{"aabbcc", "aabbcc","bc","bcc","aabbccc"})); // 7
    }
}

@@ -211,30 +211,8 @@
 ) %7B%0A
-        int max = -1;%0A
     
@@ -526,15 +526,8 @@
    %7D
- else %7B
 %0A   
@@ -531,32 +531,34 @@
             
+%7D%0A
         
 if (!ignored
@@ -549,131 +549,80 @@
     
-if (!ignored.contains(strs%5Bi%5D)) %7B%0A                        max = Math.max(max,
+%7D%0A        int max = -1;%0A        for (int i = 0; i %3C
  strs
-%5Bi%5D
 .length
-());%0A                    %7D%0A    
+; i++) %7B%0A
     
@@ -633,17 +633,12 @@
     
-    
 if (
-!
 igno
@@ -651,25 +651,25 @@
 ntains(strs%5B
-j
+i
 %5D)) %7B%0A      
@@ -666,32 +666,60 @@
 ) %7B%0A            
+    continue;%0A            %7D%0A
             max 
@@ -735,25 +735,25 @@
 x(max, strs%5B
-j
+i
 %5D.length());
@@ -757,62 +757,8 @@
 ));%0A
-                    %7D%0A                %7D%0A            %7D%0A
     
@@ -1433,1735 +1433,134 @@
  %7C%7C 
-str1.charAt
 (i
-)
  == 
+(
 str
-2.charAt(j - 1)) %7B%0A            return true;%0A        %7D%0A        return false;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem522 prob = new Problem522();%0A//        System.out.println(isSubsequence(%22abcde%22, %22xyz%22));%0A//        System.out.println(isSubsequence(%22aaabbbccc%22, %22abc%22));%0A//        System.out.println(isSubsequence(%22aaabbbccc%22, %22aabcc%22));%0A//        System.out.println(isSubsequence(%22aaabbbccc%22, %22aabccd%22));%0A//        System.out.println(isSubsequence(%22abc%22, %22xyz%22));%0A//        System.out.println(isSubsequence(%22aaa%22, %22acb%22));%0A//        System.out.println(isSubsequence(%22aabbcc%22, %22bcc%22));%0A        System.out.println(prob.findLUSlength(new String%5B%5D%7B%22aba%22, %22cdc%22, %22eae%22%7D)); // 3%0A        System.out.println(prob.findLUSlength(new String%5B%5D%7B%22aba%22, %22eae%22%7D)); // 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        System.out.println(prob.findLUSlength(new String%5B%5D%7B%22aabbcc%22, %22aabbcc%22, %22cb%22, %22abc%22%7D)); // 2%0A        System.out.println(prob.findLUSlength(new String%5B%5D%7B%22aabbcc%22, %22aabbcc%22, %22abc%22%7D)); // -1%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        System.out.println(prob.findLUSlength(new String%5B%5D%7B%22aaa%22, %22acb%22%7D)); // 3%0A        System.out.println(prob.findLUSlength(new String%5B%5D%7B%22aabbcc%22, %22aabbcc%22, %22bc%22, %22bcc%22%7D)); // -1%0A        System.out.println(prob.findLUSlength(new String%5B%5D%7B%22aabbcc%22, %22aabbcc%22,%22bc%22,%22bcc%22,%22aabbccc%22%7D)); // 7
+1.length() - 1) && str1.charAt(i) == str2.charAt(j - 1))) %7B%0A            return true;%0A        %7D%0A        return false;
 %0A   

Solve problem 522