Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何获得数组中没有相似值的最长字符串?_Javascript - Fatal编程技术网

Javascript 如何获得数组中没有相似值的最长字符串?

Javascript 如何获得数组中没有相似值的最长字符串?,javascript,Javascript,我有两个数组 1: [a, ab, abc, abcde] 2: [a, ab, abc, abcde, abcdefe, axde] 在第一个数组中,我使用此代码获得最长的行 function longestChain(words) { // Write your code here var xintTOstring = ""; var result = 0; for (var x = 0; x < words.length; x++){

我有两个数组

1: [a, ab, abc, abcde]
2: [a, ab, abc, abcde, abcdefe, axde]
在第一个数组中,我使用此代码获得最长的行

function longestChain(words) {
    // Write your code here
    var xintTOstring = "";
    var result = 0;
    for (var x = 0; x < words.length; x++){
        xintTOstring = words[x].toString();

            if (xintTOstring.length > result) {
                result = xintTOstring.length;
            }

    }
    return result;
}
问候

function equalLike(word) {
     // should the equality be checked within the array or in global stream?
}

function longestChain(words) {
    return words.reduce((longest,word) => longest = longest.length > equalLike(word).length ? 
                                                    longest : word,'');
}

最长的单词充当累加器。

如果我理解正确,每次调用最长的单词都应该返回尚未找到的最长单词。浏览每个列表,保留最长单词的对象,对照该对象进行检查,并对照键检查子字符串

const longestWords = {};

const longestChain = function(words) {
  let longestInList = "";

  words.forEach(function(word) {
    if (validLongestWord(word) && word.length > longestInList.length) {
      longestInList = word;
    }
  });

  longestWords[longestInList] = longestInList.length; //maybe handy for sorting later

  return longestInList;
}

const validLongestWord = function(word) {
    if(longestWords[word]) return false;

  return !Object.keys(longestWords).some(key=>key.indexOf(word) >=0);
}

console.log(longestChain(["a", "ab", "abc", "abcde", "abcdefe", "axde"])); //abcdefe
console.log(longestChain(["a", "ab", "abc", "abcde", "abcdefe", "axde"])); //axde
console.log(longestChain(["a", "ab", "abc", "abcde", "abcdefe", "axde"])); //none

我相信这就是OP试图用JavaScript解决的问题:

欢迎任何人编辑此答案,为问题提供解决方案

var堆栈溢出;
(功能(堆栈溢出){
var LongestChain=(函数(){
函数LongestChain(){}
LongestChain.main=函数(args){
//字串
变量词=[“a”、“ab”、“abc”、“abcdefe”、“axde”];
console.info(
“最长链长度:”+最长链。最长链(字)
);
};
LongestChain.longest_chain=函数(w){
if(null==w | | w.length<1){
返回0;
}
var maxChainLen=0;
变量字=w.slice(0)。slice(0);
var wordToLongestChain={};
对于(变量index7809=0;index7809字长){
持续
}
瓦尔·库查伦=
最长链。查找最长链(单词,单词,单词至最长链)+1;
/*put*/wordToLongestChain[word]=curChainLen;
maxChainLen=Math.max(maxChainLen,curChainLen);
}
}
返回maxChainLen;
};
LongestChain.find_chain_len=函数(字、字、字到最长链){
var curChainLen=0;
for(变量i=0;i=0){
if(wordToLongestChain.hasOwnProperty(nextWord)){
curChainLen=Math.max(
柯查伦,
/*get*/(函数(m,k){
返回m[k]?m[k]:空;
})(单词至最长链,nextWord)
);
}否则{
var nextWordChainLen=LongestChain.find_chain_len(
下一站,
话,
单词最长链
);
curChainLen=Math.max(curChainLen,nextWordChainLen+1);
}
}
}
返回curChainLen;
};
返回最长链;
})();
StackOverFlow.LongestChain=LongestChain;
LongestChain[“_类”]=“StackOverFlow.LongestChain”;
})(StackOverFlow | |(StackOverFlow={}));

StackOverFlow.LongestChain.main(空)
您的数组看起来是wierd'[{el1,el2}],而不是[el1,el2],这应该是什么:
[{a,ab,abc,abcde,abcdefe,axde}]
?它是字符串数组吗?如果是这样,它应该是
['a',ab',abc']
等…为什么
abcdefe
不是字符最长的
abcdf
?给我们输入和输出值的示例。可能的重复请阅读问题和评论。如果包含其他字符串的字符串的长度减少,则有一个警告。这并不能解决问题。看看第二个例子。预期的输出是“axde”,但您返回的是“abcdefe”。这个问题非常复杂。用英语来说,什么是“类似于相等的值”?如果问题不清楚,答案是什么?我的朋友,这会导致“bug”。
const longestWords = {};

const longestChain = function(words) {
  let longestInList = "";

  words.forEach(function(word) {
    if (validLongestWord(word) && word.length > longestInList.length) {
      longestInList = word;
    }
  });

  longestWords[longestInList] = longestInList.length; //maybe handy for sorting later

  return longestInList;
}

const validLongestWord = function(word) {
    if(longestWords[word]) return false;

  return !Object.keys(longestWords).some(key=>key.indexOf(word) >=0);
}

console.log(longestChain(["a", "ab", "abc", "abcde", "abcdefe", "axde"])); //abcdefe
console.log(longestChain(["a", "ab", "abc", "abcde", "abcdefe", "axde"])); //axde
console.log(longestChain(["a", "ab", "abc", "abcde", "abcdefe", "axde"])); //none