> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Longest Uncommon Subsequence II

> Tested Python solution for LeetCode 522 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 522, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Two Pointers](/catalog/topics/two-pointers), [String](/catalog/topics/string), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/longest-uncommon-subsequence-ii/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 522   # by problem number
lcpy gen -s longest_uncommon_subsequence_ii   # by problem name
```

## Problem

Given an array of strings \<code>strs\</code>, return \<em>the length of the \<strong>longest uncommon subsequence\</strong> between them\</em>. If the longest uncommon subsequence does not exist, return \<code>-1\</code>.

An \<strong>uncommon subsequence\</strong> between an array of strings is a string that is a \<strong>subsequence of one string but not the others\</strong>.

A \<strong>subsequence\</strong> of a string \<code>s\</code> is a string that can be obtained after deleting any number of characters from \<code>s\</code>.

\<ul>
\<li>For example, \<code>"abc"\</code> is a subsequence of \<code>"aebdc"\</code> because you can delete the underlined characters in \<code>"a\<u>e\</u>b\<u>d\</u>c"\</code> to get \<code>"abc"\</code>. Other subsequences of \<code>"aebdc"\</code> include \<code>"aebdc"\</code>, \<code>"aeb"\</code>, and \<code>""\</code> (empty string).\</li>
\</ul>

### Examples

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

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

### Constraints

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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/longest_uncommon_subsequence_ii/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/longest_uncommon_subsequence_ii/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n^2 * (len_i + len_j))
    # Space: O(1)
    def find_lus_length(self, strs: list[str]) -> int:
        # A longest uncommon subsequence, when one exists, can always be taken
        # as one of the input strings in full: any candidate longer than every
        # string it could embed in is already uncommon, so extending it never
        # helps. So scan each string and keep the longest one that is not a
        # subsequence of any other string (duplicates disqualify each other).
        def is_subsequence(short: str, long: str) -> bool:
            if len(short) > len(long):
                return False
            i = 0
            for ch in long:
                if i < len(short) and short[i] == ch:
                    i += 1
            return i == len(short)

        best = -1
        for i, candidate in enumerate(strs):
            if any(
                is_subsequence(candidate, other)
                for j, other in enumerate(strs)
                if i != j and len(other) >= len(candidate)
            ):
                continue
            best = max(best, len(candidate))
        return best
```

## Complexity

| Time | Space |
| - | - |
| O(n^2 \* (len\_i + len\_j)) | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.