Skip to content
Problem 522 · LeetCode 522(opens in a new tab)

Longest Uncommon Subsequence II

MediumSorting

A subsequence of a string is what remains after deleting any number of its letters (possibly none) without reordering the rest. For example, "ace" is a subsequence of "abcde".

You get a list of strings strs. A string is an uncommon subsequence of the list when exactly one string in the list has it as a subsequence. Find the longest such string and return how many letters it has, or -1 if there is no such string.

Example 1

Input:  strs = ["dog", "dog", "do", "cat"]
Output: 3
Why:    "cat" fits inside no other string
2 more examples and the constraints

Example 2

Input:  strs = ["ab", "ab", "a"]
Output: -1
Why:    "ab" appears twice and "a" fits inside "ab"

Example 3

Input:  strs = ["hello", "help", "he"]
Output: 5
Why:    "hello" is a subsequence of no other string

Constraints

  • The list holds between 2 and 50 strings
  • Each string has 1 to 10 letters
  • Every string uses only lowercase English letters

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Count every subsequenceO(n · 2^L · L)
  2. Test whole strings onlyO(n² · L)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeWork for 50 strings of length 10Verdict
Count every subsequenceO(n · 2^L · L)
Passes; 50,000 map entries
Test whole strings onlyO(n² · L)
Best; no strings are built

n = the number of strings, L = the longest string length (at most 10).

Edge cases to test

  • Duplicates. A string that appears twice is never uncommon. If the longest strings are all duplicated, look at shorter ones.
  • Shorter but out of order. In ["aabbcc", "aabbcc", "cb"], "cb" does not fit inside "aabbcc" because the b comes after the c. The answer is 2.
  • Nested strings. In ["aaaa", "aaa", "aa", "a"] only the longest is uncommon: 4.
  • Nothing uncommon. When every string fits inside another one, return -1.

Common mistakes

  • Skipping a string's duplicate by comparing values instead of indexes, so twins look uncommon.
  • Only checking strings that are longer: a string can fit inside one of the same length, its own copy.
  • Returning the length of the longest string without testing it at all.

How to talk through it

  1. 1Clarify. Can strings repeat? Is the empty string allowed? How long can they be?
  2. 2Give the brute force. Strings are short, so count every subsequence of every string in a map and keep the ones with count 1.
  3. 3Shrink the search. If a piece of a string is uncommon, the whole string is uncommon and longer. So only whole strings can be the answer.
  4. 4Order the checks. Sort longest first, test each string against all others with two pointers, and return the first that passes.
  5. 5Test out loud. Try a list with duplicates, nested strings and a short string whose letters are out of order.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?