Formatted question description:

522. Longest Uncommon Subsequence II




Given a list of strings, you need to find the longest uncommon subsequence among them. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings.

A subsequence is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements. Trivially, any string is a subsequence of itself and an empty string is a subsequence of any string.

The input will be a list of strings, and the output needs to be the length of the longest uncommon subsequence. If the longest uncommon subsequence doesn’t exist, return -1.

Example 1:

Input: “aba”, “cdc”, “eae”

Output: 3


  1. All the given strings’ lengths will not exceed 10.
  2. The length of the given list will be in the range of [2, 50].


First sort the array of strings according to the lengths in descending order. Then obtain each string’s number of occurrences of each string in the array, and use a map to store each string’s number of occurrences. Loop over the array from left to right where the longest string is visited first. If a string occurs more than once, then it must be another string’s subsequence (another string has the same content as the current string). If a string occurs only once, then it may be an uncommon subsequence. If the current string is not a subsequence of any string that has already been visited, then the current string is the longest uncommon subsequence, so return its length. If no uncommon subsequence is found, return -1.

class Solution {
    public int findLUSlength(String[] strs) {
        Arrays.sort(strs, new Comparator<String>() {
            public int compare(String str1, String str2) {
                if (str1.length() != str2.length())
                    return str2.length() - str1.length();
                    return str1.compareTo(str2);
        Map<String, Integer> map = new HashMap<String, Integer>();
        int length = strs.length;
        for (int i = 0; i < length; i++) {
            String str = strs[i];
            int count = map.getOrDefault(str, 0);
            map.put(str, count);
        Set<String> set = new HashSet<String>();
        for (int i = 0; i < length; i++) {
            String str = strs[i];
            boolean isLUS = true;
            if (map.get(str) == 1) {
                for (String prevStr : set) {
                    if (isSubsequence(str, prevStr)) {
                        isLUS = false;
                if (isLUS)
                    return str.length();
        return -1;

    public boolean isSubsequence(String str1, String str2) {
        int length1 = str1.length(), length2 = str2.length();
        int index1 = 0;
        for (int index2 = 0; index1 < length1 && index2 < length2; index2++) {
            if (str1.charAt(index1) == str2.charAt(index2))
        return index1 == length1;

All Problems

All Solutions