|
| 1 | +--- |
| 2 | +id: longest-uncommon-subsequence-ii |
| 3 | +title: Longest Uncommon Subsequence II |
| 4 | +sidebar_label: 0522-Longest-Uncommon-Subsequence-II |
| 5 | +tags: |
| 6 | +- String |
| 7 | +- Sorting |
| 8 | +description: "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." |
| 9 | +--- |
| 10 | + |
| 11 | +## Problem |
| 12 | + |
| 13 | +Given an array of strings `strs`, return the length of the longest uncommon subsequence between them. An uncommon subsequence is a subsequence that is not common to any other string in the array. |
| 14 | + |
| 15 | +A **subsequence** of a string `s` is a string that can be obtained after deleting any number of characters from `s`. |
| 16 | + |
| 17 | +### Examples |
| 18 | + |
| 19 | +**Example 1:** |
| 20 | + |
| 21 | +**Input:** `strs = ["aba","cdc","eae"]` |
| 22 | +**Output:** `3` |
| 23 | + |
| 24 | +**Example 2:** |
| 25 | + |
| 26 | +**Input:** `strs = ["aaa","aaa","aa"]` |
| 27 | +**Output:** `-1` |
| 28 | + |
| 29 | +### Constraints |
| 30 | + |
| 31 | +- `2 <= strs.length <= 50` |
| 32 | +- `1 <= strs[i].length <= 10` |
| 33 | +- `strs[i]` consists of lowercase English letters. |
| 34 | + |
| 35 | +--- |
| 36 | + |
| 37 | +## Approach |
| 38 | + |
| 39 | +To find the longest uncommon subsequence, we need to consider the following: |
| 40 | + |
| 41 | +1. If a string is unique in the list (i.e., it does not appear more than once), we need to check if it is not a subsequence of any other string. |
| 42 | +2. If a string is not unique (i.e., it appears more than once), it cannot be an uncommon subsequence. |
| 43 | +3. We can start by checking the strings in descending order of their lengths. This way, we can return the first string that meets the criteria as soon as we find it. |
| 44 | + |
| 45 | +### Steps: |
| 46 | + |
| 47 | +1. Sort the strings by length in descending order. |
| 48 | +2. Check each string: |
| 49 | + - If the string appears only once in the list. |
| 50 | + - If it is not a subsequence of any other string longer than it. |
| 51 | +3. If such a string is found, return its length. |
| 52 | +4. If no such string is found, return `-1`. |
| 53 | + |
| 54 | +### Helper Function |
| 55 | + |
| 56 | +A helper function `is_subsequence` can be used to determine if one string is a subsequence of another. |
| 57 | + |
| 58 | +### Solution |
| 59 | + |
| 60 | +#### Java Solution |
| 61 | + |
| 62 | +```java |
| 63 | +import java.util.*; |
| 64 | + |
| 65 | +class Solution { |
| 66 | + public int findLUSlength(String[] strs) { |
| 67 | + Arrays.sort(strs, (a, b) -> b.length() - a.length()); |
| 68 | + for (int i = 0; i < strs.length; i++) { |
| 69 | + boolean isSubsequence = false; |
| 70 | + for (int j = 0; j < strs.length; j++) { |
| 71 | + if (i != j && isSubsequence(strs[i], strs[j])) { |
| 72 | + isSubsequence = true; |
| 73 | + break; |
| 74 | + } |
| 75 | + } |
| 76 | + if (!isSubsequence) { |
| 77 | + return strs[i].length(); |
| 78 | + } |
| 79 | + } |
| 80 | + return -1; |
| 81 | + } |
| 82 | + |
| 83 | + private boolean isSubsequence(String a, String b) { |
| 84 | + int i = 0, j = 0; |
| 85 | + while (i < a.length() && j < b.length()) { |
| 86 | + if (a.charAt(i) == b.charAt(j)) { |
| 87 | + i++; |
| 88 | + } |
| 89 | + j++; |
| 90 | + } |
| 91 | + return i == a.length(); |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
| 95 | +#### C++ Solution |
| 96 | + |
| 97 | +```cpp |
| 98 | +#include <vector> |
| 99 | +#include <string> |
| 100 | +#include <algorithm> |
| 101 | +using namespace std; |
| 102 | + |
| 103 | +class Solution { |
| 104 | +public: |
| 105 | + int findLUSlength(vector<string>& strs) { |
| 106 | + sort(strs.begin(), strs.end(), [](const string &a, const string &b) { |
| 107 | + return b.size() < a.size(); |
| 108 | + }); |
| 109 | + |
| 110 | + for (int i = 0; i < strs.size(); i++) { |
| 111 | + bool isSubsequence = false; |
| 112 | + for (int j = 0; j < strs.size(); j++) { |
| 113 | + if (i != j && isSubsequence(strs[i], strs[j])) { |
| 114 | + isSubsequence = true; |
| 115 | + break; |
| 116 | + } |
| 117 | + } |
| 118 | + if (!isSubsequence) { |
| 119 | + return strs[i].size(); |
| 120 | + } |
| 121 | + } |
| 122 | + return -1; |
| 123 | + } |
| 124 | + |
| 125 | +private: |
| 126 | + bool isSubsequence(const string &a, const string &b) { |
| 127 | + int i = 0, j = 0; |
| 128 | + while (i < a.size() && j < b.size()) { |
| 129 | + if (a[i] == b[j]) { |
| 130 | + i++; |
| 131 | + } |
| 132 | + j++; |
| 133 | + } |
| 134 | + return i == a.size(); |
| 135 | + } |
| 136 | +}; |
| 137 | +``` |
| 138 | +#### Python Solution |
| 139 | + |
| 140 | +```python |
| 141 | +class Solution: |
| 142 | + def findLUSlength(self, strs: List[str]) -> int: |
| 143 | + strs.sort(key=len, reverse=True) |
| 144 | + |
| 145 | + def is_subsequence(a, b): |
| 146 | + it = iter(b) |
| 147 | + return all(c in it for c in a) |
| 148 | + |
| 149 | + for i, s in enumerate(strs): |
| 150 | + if all(not is_subsequence(s, strs[j]) for j in range(len(strs)) if i != j): |
| 151 | + return len(s) |
| 152 | + |
| 153 | + return -1 |
| 154 | +``` |
| 155 | +### Complexity Analysis |
| 156 | +**Time Complexity:** O(n^2 * l) |
| 157 | +>Reason: Sorting the list takes O(n log n). Checking if a string is a subsequence of another string takes O(l) time, and this is done for each pair of strings, leading to O(n^2 * l) in total. |
| 158 | +
|
| 159 | +**Space Complexity:** O(1) |
| 160 | +>Reason: The space complexity is constant as we are not using any extra space proportional to the input size, other than the space required for sorting. |
| 161 | +
|
| 162 | +### References |
| 163 | +LeetCode Problem: Longest Uncommon Subsequence II |
0 commit comments