Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Finding the longest "uncommon" sequence in JavaScript
We are required to write a JavaScript function that takes in an array of strings. The function should find the longest uncommon subsequence among the strings of the array.
By longest uncommon subsequence we mean the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings.
Our function should return the length of this longest uncommon subsequence.
For example: If the input array is ?
const arr = ["aba", "cdc", "eae"];
Then the output should be 3.
Understanding the Problem
The key insight is that if two strings are different, then each string is an uncommon subsequence of itself (since it doesn't appear as a subsequence in any other string). If strings are identical, we need to find other unique strings.
Example
The code for this will be ?
const arr = ["aba", "cdc", "eae"];
const findUncommonLength = (array = []) => {
const seen = {};
const uniqueStrings = [];
let max = -1;
let duplicateString = null;
// Count occurrences of each string
for(let i = 0; i < array.length; i++){
seen[array[i]] = (seen[array[i]] || 0) + 1;
if(seen[array[i]] > 1){
if(duplicateString === null || array[i].length > duplicateString.length){
duplicateString = array[i];
}
}
}
// If no duplicates, return length of longest string
if(duplicateString === null) {
array.forEach(str => {
if(str.length > max) max = str.length;
});
return max;
}
// Collect unique strings
for(let i = 0; i < array.length; i++){
if(seen[array[i]] === 1) uniqueStrings.push(array[i]);
}
// Filter out strings that are subsequences of the duplicate string
const filteredStrings = uniqueStrings.filter(str => {
let matchIndex = 0;
for(let j = 0; j < duplicateString.length; j++){
if(duplicateString[j] === str[matchIndex]){
matchIndex++;
}
}
return matchIndex !== str.length; // Keep if not a complete subsequence
});
// Find maximum length among remaining strings
max = -1;
filteredStrings.forEach(str => {
if(str.length > max) max = str.length;
});
return max;
};
console.log(findUncommonLength(arr));
Output
3
How It Works
The algorithm works in these steps:
- Count duplicates: Track how many times each string appears
- Handle no duplicates: If all strings are unique, return the longest one
- Filter subsequences: Remove strings that are subsequences of duplicate strings
- Return maximum: Find the length of the longest remaining uncommon subsequence
Alternative Approach
For simpler cases where we just need to check if strings are different:
const simpleUncommonLength = (str1, str2) => {
// If strings are different, each is an uncommon subsequence
if(str1 !== str2) {
return Math.max(str1.length, str2.length);
}
// If identical, no uncommon subsequence exists
return -1;
};
console.log(simpleUncommonLength("aba", "cdc")); // 3
console.log(simpleUncommonLength("abc", "abc")); // -1
3 -1
Conclusion
The longest uncommon subsequence problem has an elegant solution: if strings are different, the longest string itself is the answer. The algorithm efficiently handles duplicates and subsequence filtering to find the optimal result.
