Skip to main content

Longest Uncommon Subsequence II - Solution & Explanation

MediumArrayHash TableTwo PointersString20 min readAsked at: Google
Practice this problem

Problem Statement

Given an array of strings strs, return the length of the longest uncommon subsequence between them. If the longest uncommon subsequence does not exist, return -1.

An uncommon subsequence between an array of strings is a string that is a subsequence of one string but not the others.

A subsequence of a string s is a string that can be obtained after deleting any number of characters from s.

  • For example, "abc" is a subsequence of "aebdc" because you can delete the underlined characters in "aebdc" to get "abc". Other subsequences of "aebdc" include "aebdc", "aeb", and "" (empty string).

 

Example 1:

Input: strs = ["aba","cdc","eae"]
Output: 3

Example 2:

Input: strs = ["aaa","aaa","aa"]
Output: -1

 

Constraints:

  • 2 <= strs.length <= 50
  • 1 <= strs[i].length <= 10
  • strs[i] consists of lowercase English letters.

Approach Overview

Problem Overview: You receive an array of strings and must return the length of the longest string that is not a subsequence of any other string in the array. If every string can be formed as a subsequence of another, the answer is -1. The challenge is efficiently verifying subsequence relationships across multiple strings.

Approach 1: Brute Force Check (O(n^2 * L) time, O(1) space)

Check every string against every other string and test whether it appears as a subsequence. For each pair, run a subsequence check using two pointers: move one pointer through the candidate string and another through the potential parent string while matching characters. If a string is not a subsequence of any other string, it is an uncommon subsequence candidate. Track the maximum length among valid candidates.

This method directly models the definition of the problem. With n strings and maximum length L, each subsequence comparison costs O(L), and you perform up to n^2 comparisons. The approach is straightforward and useful for understanding the mechanics of subsequence matching in string problems.

Approach 2: Sorted Length Check (O(n^2 * L) time, O(n) space)

A better strategy is to process longer strings first. Sort the input array in descending order by length using sorting. The first string that is not a subsequence of any other string must be the answer because no later string can be longer.

After sorting, iterate through each string and check whether it is a subsequence of any other string. Use the same two‑pointer subsequence check. A small optimization is handling duplicates with a hash table: if a string appears multiple times, it cannot be uncommon because identical strings are subsequences of each other. Skip those early to reduce unnecessary comparisons.

The key insight is that once you encounter a valid uncommon subsequence while scanning from longest to shortest, you can immediately return its length. This avoids evaluating shorter candidates and reduces practical runtime even though the theoretical complexity remains O(n^2 * L).

Recommended for interviews: The sorted length approach is what most interviewers expect. Starting with brute force shows you understand the definition of subsequences and pairwise comparison. Moving to the sorted strategy demonstrates optimization thinking: process candidates in descending length order and exit early once a valid uncommon subsequence appears.

Approach 1: Brute Force Check

This approach involves iterating through every string in the array and checking if it is a subsequence of any other string. If a string is a subsequence of no other string, we note its length and update our longest uncommon subsequence length.

For a given string, iterating over all other strings and checking if the current string can be a subsequence can be done using a helper function.

In this C solution, we define a helper function isSubsequence to check if one string is a subsequence of another. The main function findLUSlength iterates over each string, using the helper function to check subsequence relationships. If a string is not a subsequence of any other, its length is considered for the maximum length.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n^2 * l) where n is the number of strings and l is the maximum length of a string. Space Complexity: O(1) as we use only constant extra space.

Try this approach in the editor →

Approach 2: Sorted Length Check

This approach builds upon sorting the strings based on their length in descending order. The idea is to try longer strings first, as they have a better chance of being unique. The process involves checking if the current string is a subsequence of any other strings.

This C solution sorts the strings in descending order of length using the qsort function. Then, it checks each string to see if it's not a subsequence of any other string. The function stops once it finds such a string, returning its length.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n^2 * l) with an added O(n log n) for sorting. Space Complexity: O(1), constant extra space is used.

Try this approach in the editor →

Approach 3: Subsequence Judgment

We define a function check(s, t) to determine whether string s is a subsequence of string t. We can use a two-pointer approach, initializing two pointers i and j to point to the beginning of strings s and t respectively, then continuously move pointer j. If s[i] equals t[j], then move pointer i. Finally, check if i equals the length of s. If i equals the length of s, it means s is a subsequence of t.

To determine if string s is unique, we only need to take string s itself and compare it with other strings in the list. If there exists a string for which s is a subsequence, then s is not unique. Otherwise, string s is unique. We take the longest string among all unique strings.

The time complexity is O(n^2 times m), where n is the length of the list of strings, and m is the average length of the strings. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Brute Force Check

Time Complexity: O(n^2 * l) where n is the number of strings and l is the maximum length of a string. Space Complexity: O(1) as we use only constant extra space.

Sorted Length Check

Time Complexity: O(n^2 * l) with an added O(n log n) for sorting. Space Complexity: O(1), constant extra space is used.

Subsequence Judgment

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force CheckO(n^2 * L)O(1)When implementing the simplest interpretation of the problem or during initial reasoning in interviews
Sorted Length CheckO(n^2 * L)O(n)General optimized approach; process longest strings first and exit early when a valid uncommon subsequence is found

Video Solution

Longest Uncommon Subsequence II | Leetcode 522 | Live Coding session 🔥🔥🔥🔥Coding Decoded4,798 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Longest Uncommon Subsequence II easy or hard?
Longest Uncommon Subsequence II is considered a medium‑difficulty problem. The logic for checking subsequences is simple, but the challenge comes from handling comparisons across many strings efficiently and recognizing that sorting by length allows early termination.
How to solve Longest Uncommon Subsequence II in O(n)?
An O(n) solution is not feasible because the algorithm must compare strings to determine subsequence relationships. Each comparison requires scanning characters, and multiple pairwise checks are required. The practical optimal approach remains O(n^2 * L) using sorting and efficient subsequence checks.
What is the best approach for Longest Uncommon Subsequence II?
The sorted length check approach is typically the best solution. Sort all strings by length in descending order, then check whether each string is a subsequence of any other string using a two‑pointer comparison. The first string that is not a subsequence of another is the answer. This runs in O(n^2 * L) time where L is the maximum string length.
What data structure is used in Longest Uncommon Subsequence II?
The solution mainly relies on arrays of strings and simple subsequence checks using two pointers. A hash table can be used to count duplicate strings and skip them early. Sorting is also used in the optimized approach to process longer strings first.
What is the time complexity of Longest Uncommon Subsequence II?
The common solution runs in O(n^2 * L) time. You may need to compare each string against every other string, and each subsequence check takes O(L) using a two‑pointer scan. Space complexity ranges from O(1) to O(n) depending on whether a hash table is used to track duplicate strings.
Longest Uncommon Subsequence II Python or Java solution approach?
Both Python and Java implementations typically sort the array of strings by length in descending order and then perform subsequence checks. A helper function uses two pointers to verify whether one string is a subsequence of another. The algorithm runs in O(n^2 * L) time and works well within the typical constraints.
Is Longest Uncommon Subsequence II asked at Google, Amazon, or Meta?
Problems involving subsequences, string comparison, and candidate filtering appear frequently in interviews at large tech companies such as Amazon, Google, and Meta. While this exact problem may not appear verbatim, the techniques used—two‑pointer subsequence checks, sorting by length, and duplicate filtering—are common interview patterns.

Ready to solve this problem?

Practice Longest Uncommon Subsequence II with our built-in code editor and test cases.

Practice on FleetCode