public class Solution {
    public int findLUSlength(String[] strs) {
        Arrays.sort(strs, new Comparator<String>(){
            public int compare(String a, String b) {
                if (a.length() != b.length()) {
                    return b.length()-a.length();
                } else {
                    return a.compareTo(b);
                }
            }
        });
        for (int i=0; i<strs.length; i++) {
            boolean flag = (i == strs.length-1 || !strs[i].equals(strs[i+1]));
            
            if (flag) {
                for (int j=i-1; j>=0; j--) {
                    if (isSub(strs[i], strs[j])) {
                        flag = false;
                        break;
                    }
                }
            }
            if (flag) {
                return strs[i].length();
            }
        }
        return -1;
    }
    private boolean isSub(String a, String b) {
        if (a.length() <= b.length()) {
            for (int state=1, i=0; i<b.length(); i++) {
                int next = 1;
                
                for (int j=0; j<=i && j<a.length(); j++) {
                    if (0 != (state&(1<<j)) && a.charAt(j) == b.charAt(i)) {
                        if (j == a.length()-1) {
                            return true;
                        }
                        next |= (1<<(j+1));
                    }
                }
                state |= next;
            }
        }
        return false;
    }
}
