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;
  • Todo
  • class Solution(object):
      def findLUSlength(self, strs):
        :type strs: List[str]
        :rtype: int
        def findLUSlength(a, b):
          return max(len(a), len(b)) if a != b else -1
        def isSubsequence(s, t):
          d = collections.defaultdict(list)
          for i, c in enumerate(t):
          start = 0
          for c in s:
            idx = bisect.bisect_left(d[c], start)
            if len(d[c]) == 0 or idx >= len(d[c]):
              return False
            start = d[c][idx] + 1
          return True
        ans = -1
        strs.sort(key=len, reverse=True)
        for i in range(len(strs)):
          flag = True
          for j in range(len(strs)):
            if i != j and (findLUSlength(strs[i], strs[j]) == -1 or isSubsequence(strs[i], strs[j])):
              flag = False
          if flag:
            return len(strs[i])
        return -1

All Problems

All Solutions