Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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.
"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 <= 501 <= strs[i].length <= 10strs[i] consists of lowercase English letters.Problem summary: 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).
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Two Pointers
["aba","cdc","eae"]
["aaa","aaa","aa"]
longest-uncommon-subsequence-i)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #522: Longest Uncommon Subsequence II
class Solution {
public int findLUSlength(String[] strs) {
int ans = -1;
int n = strs.length;
for (int i = 0, j; i < n; ++i) {
int x = strs[i].length();
for (j = 0; j < n; ++j) {
if (i != j && check(strs[i], strs[j])) {
x = -1;
break;
}
}
ans = Math.max(ans, x);
}
return ans;
}
private boolean check(String s, String t) {
int m = s.length(), n = t.length();
int i = 0;
for (int j = 0; i < m && j < n; ++j) {
if (s.charAt(i) == t.charAt(j)) {
++i;
}
}
return i == m;
}
}
// Accepted solution for LeetCode #522: Longest Uncommon Subsequence II
func findLUSlength(strs []string) int {
ans := -1
check := func(s, t string) bool {
m, n := len(s), len(t)
i := 0
for j := 0; i < m && j < n; j++ {
if s[i] == t[j] {
i++
}
}
return i == m
}
for i, s := range strs {
x := len(s)
for j, t := range strs {
if i != j && check(s, t) {
x = -1
break
}
}
ans = max(ans, x)
}
return ans
}
# Accepted solution for LeetCode #522: Longest Uncommon Subsequence II
class Solution:
def findLUSlength(self, strs: List[str]) -> int:
def check(s: str, t: str):
i = j = 0
while i < len(s) and j < len(t):
if s[i] == t[j]:
i += 1
j += 1
return i == len(s)
ans = -1
for i, s in enumerate(strs):
for j, t in enumerate(strs):
if i != j and check(s, t):
break
else:
ans = max(ans, len(s))
return ans
// Accepted solution for LeetCode #522: Longest Uncommon Subsequence II
struct Solution;
use std::cmp::Reverse;
trait IsSubSequenceOf {
fn is_subsequence_of(&self, s: &str) -> bool;
}
impl IsSubSequenceOf for String {
fn is_subsequence_of(&self, s: &str) -> bool {
let mut it = self.chars().peekable();
for c in s.chars() {
if let Some(&first) = it.peek() {
if first == c {
it.next();
}
}
}
it.next().is_none()
}
}
impl Solution {
fn find_lu_slength(mut strs: Vec<String>) -> i32 {
let n = strs.len();
strs.sort_by_key(|s| Reverse(s.len()));
for i in 0..n {
let mut count = 0;
for j in 0..n {
if i != j {
if !strs[i].is_subsequence_of(&strs[j]) {
count += 1;
}
}
}
if count == n - 1 {
return strs[i].len() as i32;
}
}
-1
}
}
#[test]
fn test() {
let strs = vec_string!["aba", "cdc", "eae"];
let res = 3;
assert_eq!(Solution::find_lu_slength(strs), res);
}
// Accepted solution for LeetCode #522: Longest Uncommon Subsequence II
function findLUSlength(strs: string[]): number {
const n = strs.length;
let ans = -1;
const check = (s: string, t: string): boolean => {
const [m, n] = [s.length, t.length];
let i = 0;
for (let j = 0; i < m && j < n; ++j) {
if (s[i] === t[j]) {
++i;
}
}
return i === m;
};
for (let i = 0; i < n; ++i) {
let x = strs[i].length;
for (let j = 0; j < n; ++j) {
if (i !== j && check(strs[i], strs[j])) {
x = -1;
break;
}
}
ans = Math.max(ans, x);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.