↓Skip to main content
  1. LeetCode/

LeetCode 522: Longest Uncommon Subsequence II

·2 mins· ·
LeetCode Medium Two-Pointers String Sorting Array Hash-Table
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-04-05

  • Submissions: Accepted (first try)

Approach
#

Key observation: if some subsequence of a string is uncommon (not a subsequence of any other string), the whole string is uncommon too. If the whole string fit inside another string, every one of its subsequences would fit as well. So only whole strings need checking.

strs[i] is uncommon when, for every j != i, strs[i] is not a subsequence of strs[j].

  • Strings shorter than strs[i] cannot contain it, so skip them.
  • Sort by length descending; the first string that qualifies is the answer. If none does, return -1.

The subsequence check uses same-direction pointers: i walks the shorter string and advances only on a match, while the scan over the longer string advances every step; at the end, check whether i finished.

My Solution
#

class Solution:
    def findLUSlength(self, strs: List[str]) -> int:
        def is_subsequence(s: str, t: str) -> bool:    # is s a subsequence of t
            i = 0
            for ch in t:
                if i < len(s) and s[i] == ch:
                    i += 1
            return i == len(s)

        strs.sort(key=len, reverse=True)
        n = len(strs)
        for i in range(n):
            uncommon = True
            for j in range(n):
                if len(strs[j]) < len(strs[i]):
                    break                              # the rest are shorter and cannot contain strs[i]
                if i != j and is_subsequence(strs[i], strs[j]):
                    uncommon = False
                    break
            if uncommon:
                return len(strs[i])
        return -1

Duplicate strings are subsequences of each other, so they rule themselves out with no special handling.

Complexity
#

Time O(n^2 · L), where L is the string length; space O(1) apart from sorting.

Mistakes to Avoid
#

  • Enumerating every subsequence of each string: exponential.
  • Comparing only with strictly longer strings: equal-length strings with the same content must be compared too.

Interview Explanation
#

If a subsequence is uncommon, the whole string containing it is uncommon too, so the answer is always the length of a whole string. I sort strings by length descending and check whether each one is a subsequence of any other string at least as long, using two pointers. The first one that is not is the answer; otherwise I return -1. Time O(n^2 · L).

Related

LeetCode 244: Shortest Word Distance II
·2 mins
LeetCode Medium Two-Pointers Design Hash-Table String Array
LeetCode 244 note: precompute each word’s positions (sorted by construction), then answer each query by merging the two position lists with two pointers, always advancing the smaller one
LeetCode 923: 3Sum With Multiplicity
·4 mins
LeetCode Medium Two-Pointers Sorting Hash-Table Counting Array
LeetCode 923 note: sort, fix the first number, and find the other two with converging pointers; equal end values are counted with a combination, different ones by multiplying each end’s duplicate count
LeetCode 49: Group Anagrams
·2 mins
LeetCode Medium Array Hash-Table String Sorting
LeetCode 49 note: use each string’s sorted form as a key; strings sharing a key form one anagram group
LeetCode 3555: Smallest Subarray to Sort in Every Sliding Window
·2 mins
LeetCode Medium Two-Pointers Greedy Sorting Array Monotonic-Stack
LeetCode 3555 note: every length-k window is one instance of LeetCode 581; with n only 1000, two O(k) scans per window are enough
LeetCode 581: Shortest Unsorted Continuous Subarray
·2 mins
LeetCode Medium Two-Pointers Greedy Sorting Array Monotonic-Stack
LeetCode 581 note: scanning left to right with a running max, the last index below it is the right boundary; scanning right to left with a running min, the last index above it is the left boundary
LeetCode 1877: Minimize Maximum Pair Sum in Array
·1 min
LeetCode Medium Two-Pointers Greedy Sorting Array
LeetCode 1877 note: after sorting, pair the smallest with the largest, the second smallest with the second largest, and take the largest pair sum with converging pointers