Created
August 28, 2021 00:28
-
-
Save germanescobar/cd3fdbca647b88459349dbe40f4c32c8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var findLUSlength = function(strs) { | |
strs = strs.sort((a, b) => b.length - a.length) | |
let count = 0 | |
for (let i=strs.length-1; i >= 0; i--) { | |
for (let j=strs.length-1; j >= 0; j--) { | |
if (i !== j) { | |
if (strs[j].includes(strs[i])) { | |
count++ | |
break | |
} | |
} | |
} | |
} | |
console.log("count", count) | |
if (count === strs.length) { | |
return -1 | |
} | |
for (let i=0; i < strs.length; i++) { | |
const len = strs[i].length | |
let isLast = true | |
for (let j=i+1; j < strs.length; j++) { | |
if (i !== j) { | |
isLast = false | |
if (strs[j].length < len) { | |
return len | |
} | |
if (strs[i] === strs[j]) { | |
i++ | |
break | |
} | |
} | |
} | |
if (isLast) return len | |
} | |
return -1 | |
}; |
He conseguido esta primera aproximación, falta hacerle refactor pero cumple su trabajo:
const checkEsSubstr = (cad1,cad2) => {
let j=0
let coincide=true
for (let i=0;i<cad1.length;i++){
coincide = false
for(j;j<cad2.length;j++){
if(cad1[i]===cad2[j]){
coincide = true
j++
break
}
}
if(!coincide) return coincide
}
return coincide
}
let noComunes = []
for(let i=0;i<strs.length;i++){
let esSubstr = false
for(let j=0;j<strs.length;j++){
if(i!==j && strs[i].length <= strs[j].length){
if(checkEsSubstr(strs[i],strs[j])) {
esSubstr = true
break
}
}
}
if (!esSubstr) {
noComunes.push(strs[i])
}
}
if (noComunes.length===0) return -1
return Math.max(...noComunes.map(x=>x.length))
};
Buenas noches, logré realizar esta solución
var findLUSlength = function(strs) {
let max = -1;
let flag = false;
for(let i=0;i<strs.length;i++){
for(let j=0;j<strs.length;j++){
if(i===j) continue;
flag = false;
if(esSub(strs[i],strs[j])){
flag = true;
break;
}
}
if(!flag) {
if(strs[i].length >max) max =strs[i].length;
}
}
return max;
function esSub(a,b){
if(a.length > b.length) return false;
let i = 0;
let j = 0;
while(i<a.length && j<b.length){
if(a[i]===b[j]) {
i++;
j++;
}
else j++;
}
return (i == a.length)
}
};
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Esta solución no funciona.