Anagrams (v2)

Revision 2 of this benchmark created on


Setup

const arrayCompare = (a1, a2) => (a1.length === a2.length) && a1.every((v,i) => v === a2[i]);
const dataMock = [
    ['', '', ['']],
    ['I like cat and tac and act', 'act', [ 'cat', 'tac', 'act' ]],
    ['Silent lion lets Inlets stilets enlist Listens', 'listen', [ 'Silent', 'Inlets', 'enlist' ]],
    ['A Aa Aaa aaa aaaa aaaaa aba ', 'aaa', [ 'Aaa', 'aaa' ]],
    ['Aaa ab ba ba ba Ba Ab bab aabaa aba ', 'ab', [ 'ab', 'ba', 'Ba', 'Ab' ] ]

];

Test runner

Ready to run.

Testing in
TestOps/sec
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) {
    // Helper function to check if two strings are anagrams
    function isAnagram(str1, str2) {
      const normalize = (str) => str.toLowerCase().split('').sort().join('');
      return normalize(str1) === normalize(str2);
    }
  
    // Split the string into an array of words
    const words = string.split(' ');
  
    // Filter out anagrams of the target word
    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

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.