我编写了一个程序,以查找由数组中也存在的其他单词组成的最长单词。
sort_arr.forEach(word => {
if (isLargest(word, word)) {
console.log(word);
}
});
function isLargest(initialWord, word) {
let first_half = 0;
let second_half = 0;
let start = 0;
let end = 0;
for (let i = 0; i < word.length; i++) {
end++;
first_half = word.substring(start, end);
for (let j = 0; j < sort_arr.length; j++) {
if (first_half === sort_arr[j]) {
second_half = word.substring(end, word.length);
if(second_half === ''){
return word !== initialWord;
}
else{
return isLargest(initialWord, second_half);
}
}
}
}
}
但是当数组words
包含
[ 'catxdogcatsrat',
'catsdogcats',
'dogcatsdog',
'cats',
'cat',
'dog',
'rat' ]
它给出输出null
但是结果应该是catsdogcats
我知道在catsdogcats
中,前缀为cat
,后缀为sdogcats
时会出现问题。但这不是在检查前缀cats
和后缀dogcats
。
能否请一些建议我的不使用ties
的方法。
答案 0 :(得分:0)
这比最初预期的要复杂一些。您必须查看其他单词与当前单词的开头相同,然后尝试使用每个单词,直到获得由其他单词组成的完整单词。
const canMakeWord = (array, word) => {
//recursive function
const recur = (array, word) => {
//word passed is empty, so we could make current word
// with the list of other words
if (word === '') {
return true;
}
//see what words in the list of other words
// can start to make up this word
const candidates = array.filter(
(otherWord) => word.indexOf(otherWord) === 0,
);
if (!candidates.length) {
console.warn('giving up for word', word);
}
return (
//no candidates, return false
!!candidates.length &&
//try each candidate recursively
candidates.reduce(
(result, otherWord) =>
result || //try for every result until it's true
//use whole list of other words but remove
// the other word used for this check from
// current word
console.log(
'trying with word:',
word,
'using candidate:',
JSON.stringify(otherWord),
) ||
recur(array, word.replace(otherWord, '')),
false,
)
);
};
//return recursive function
return recur(
array
//do not use other words that are longer than current word
.filter((w) => w.length <= word.length)
//do not include current word
.filter((w) => w !== word),
word,
);
};
const words = ['x', 'xxs', 'xxsxxsxx'];
const result = words
.map((word) => [word.length, word])
.sort(([a], [b]) => b - a)
.map(([_, word]) => word)
.find(
(word, index, all) =>
canMakeWord(all, word),
);
// .map((word, index, all) => [canMakeWord(all, word), word])
// //take out all words that could not have been made up out of
// // other words
// .filter(([canMake]) => canMake)
// .map(
// //map to [wordLength,word]
// ([_, word]) => [word.length, word],
// )
// .sort(
// ([a], [b]) => b - a, //sort to longest word
// );
console.log('RESULT:');
console.log(result);
答案 1 :(得分:0)
不确定这是否是您想要的,但是findLongestCombination(arr)
返回一个单词在数组中由其他单词构成的最长组合(每个单词仅使用一次)。在这种情况下:[“ 123,” 111“,” 1“,” 3“]
它通过尝试使用每种可能的构词方法,并递归使用findLongestCombinationSingle(word, otherWords)
,从其余词中找出构词的最长组合。
如果您有任何疑问,或者我不明白这个问题,请随时发表评论。
arr = ['123', '111', '12311113', '1', '2', '3'];
console.log(findLongestCombination(arr));
function findLongestCombination(arr){
var result = [];
for(var i=0; i<arr.length; i++){
var arrOthers = arr.slice(0,i).concat(arr.slice(i+1));
var comb = findLongestCombinationSingle(arr[i], arrOthers);
if(comb.length > result.length) result = comb;
}
return result;
}
function findLongestCombinationSingle(word, otherWords){
var result = [];
for(var i=0; i<otherWords.length; i++){
if(word.startsWith(otherWords[i])){
var wordsLeft = otherWords.slice(0,i).concat(otherWords.slice(i+1));
var restWord = word.replace(otherWords[i], "");
var subresult = [otherWords[i]].concat(findLongestCombinationSingle(restWord, wordsLeft));
if(subresult.length > result.length) result = subresult;
}
}
return result;
}
如果一个单词不可组合,它不会中断。要解决这个问题,请给我一些时间