Map | function solution(sentance, targetWord) {
const lowerTargetWord = targetWord.toLowerCase();
const words = sentance.split(' ')
.filter(word => word.toLowerCase() !== lowerTargetWord && targetWord.length === word.length);
const targetWordMap = new Map();
for(let i = 0; i < lowerTargetWord.length; i++) {
const char = lowerTargetWord[i];
const num = targetWordMap.get(char) || 0;
targetWordMap.set(char, num + 1);
}
return words.filter(word => {
const wordMap = new Map(targetWordMap);
const lowerWord = word.toLowerCase();
for (let i = 0; i < word.length; i++) {
const char = lowerWord[i];
if (!wordMap.has(char))
{
return false;
}
let num = wordMap.get(char);
if (--num == 0) {
wordMap.delete(char);
} else {
wordMap.set(char, num);
}
}
return wordMap.size === 0;
});
}
dataMock.forEach((data, index) => {
const result = solution(data[0], data[1]);
console.assert(arrayCompare(result, data[2]), `Case #${index}: ${result} expected ${data[2]}`);
});
| ready |
Optimized Solution | function solution(sentence, word) {
const words = sentence.split(' ');
const targetWord = word.toLowerCase();
const targetWordSize = new Set(targetWord).size;
const anagrams = words.reduce((result, currentWord) => {
if (currentWord.length !== word.length) {
return result;
}
const wordSize = new Set(targetWord + currentWord.toLowerCase()).size;
if (targetWordSize === wordSize) {
result.add(currentWord);
}
return result;
}, new Set);
return [...anagrams];
}
dataMock.forEach((data, index) => {
const result = solution(data[0], data[1]);
console.assert(arrayCompare(result, data[2]), `Case #${index}: ${result} expected ${data[2]}`);
});
| ready |
Int | function solution(sentence, word) {
const words = sentence.split(' ');
const targetWord = word.toLowerCase();
const targetWordInt = [...targetWord].reduce((sum, c) => sum + c.charCodeAt(0), 0);
const anagrams = words.reduce((result, currentWord) => {
if (currentWord.length !== word.length || targetWord === currentWord.toLowerCase()) {
return result;
}
const wordInt = [...currentWord.toLowerCase()].reduce((sum, c) => sum + c.charCodeAt(0), 0);
if (targetWordInt === wordInt) {
result.add(currentWord);
}
return result;
}, new Set);
return [...anagrams];
}
dataMock.forEach((data, index) => {
const result = solution(data[0], data[1]);
console.assert(arrayCompare(result, data[2]), `Case #${index}: ${result} expected ${data[2]}`);
});
| ready |
ChatGPT | function solution(string, targetWord) {
function isAnagram(str1, str2) {
const normalize = (str) => str.toLowerCase().split('').sort().join('');
return normalize(str1) === normalize(str2);
}
const words = string.split(' ');
return words.filter(word => isAnagram(word, targetWord) && word.toLowerCase() !== targetWord.toLowerCase());
}
dataMock.forEach((data, index) => {
const result = solution(data[0], data[1]);
console.assert(arrayCompare(result, data[2]), `Case #${index}: ${result} expected ${data[2]}`);
});
| ready |
Optimized 2 | function solution(sentence, targetWord) {
const anagrams = [];
const targetWordLen = targetWord.length;
const targetWordLower = targetWord.toLowerCase();
const words = sentence.split(' ').filter(w => w.length === targetWordLen);
const targetWordSymbols = Object.create(null);
for (let i = 0; i < targetWordLen; i++) {
const char = targetWordLower[i];
targetWordSymbols[char] = (targetWordSymbols[char] || 0) + 1;
}
for (const word of words) {
const wordLower = word.toLowerCase()
const wordSymbols = {...targetWordSymbols};
for (let i = 0; i < word.length; i++) {
const char = wordLower[i];
if (!wordSymbols[char] || wordSymbols[char] < 0) {
return false;
}
wordSymbols[char]--;
}
if (!Object.values(wordSymbols).find(i => i > 0)) {
anagrams.push(word);
}
}
return anagrams;
}
dataMock.forEach((data, index) => {
const result = solution(data[0], data[1]);
console.assert(arrayCompare(result, data[2]), `Case #${index}: ${result} expected ${data[2]}`);
});
| ready |
Optimized 3 | function solution(sentence, targetWord) {
const targetWordLen = targetWord.length;
const targetWordLower = targetWord.toLowerCase();
const targetWordSymbols = Object.create(null);
for (let i = 0; i < targetWordLen; i++) {
const char = targetWordLower[i];
targetWordSymbols[char] = (targetWordSymbols[char] || 0) + 1;
}
return sentence
.split(' ')
.filter(word => {
if (word.length !== targetWordLen || word.toLowerCase() === targetWordLower) {
return false;
}
const wordLower = word.toLowerCase()
const wordSymbols = {...targetWordSymbols};
for (let i = 0; i < word.length; i++) {
const char = wordLower[i];
if (!wordSymbols[char] || wordSymbols[char] < 0) {
return false;
}
wordSymbols[char]--;
}
return Object.values(wordSymbols).find(i => i > 0) ? false : word;
});
}
dataMock.forEach((data, index) => {
const result = solution(data[0], data[1]);
console.assert(arrayCompare(result, data[2]), `Case #${index}: ${result} expected ${data[2]}`);
});
| ready |
Sort | function solution(sentence, targetWord) {
const targetWordSorted = targetWord.toLowerCase().split('').sort().join('');
return sentence
.split(' ')
.filter(word => {
if (word.length !== targetWord.length) {
return false;
}
return word.toLowerCase().split('').sort().join('') === targetWordSorted;
});
}
dataMock.forEach((data, index) => {
const result = solution(data[0], data[1]);
console.assert(arrayCompare(result, data[2]), `Case #${index}: ${result} expected ${data[2]}`);
});
| ready |