Search code examples
javascripttime-complexitycomputer-scienceanagram

Time Complexity for JavaScript Anagram Function


I had an assignment to right a function that will take 2 strings and return the number of characters needed to be deleted in order to make the 2 strings anagrams of each other. My question is what the time complexity of this function is and if there is a faster way to achieve the same result. Here is my solution:

function anagramDelete(str1, str2){
    let obj1 = {}, obj2 = {};

    // Load obj1 with all characters of str1 and their count
    str1.split('').forEach((char)=> {
        if(char in obj1){
            obj1[char]++;
        } else {
            obj1[char] = 1;
        }
    });

    // Load obj2 with all characters of str1 and their count
    str2.split('').forEach((char)=> {
        if(char in obj2){
            obj2[char]++;
        } else {
            obj2[char] = 1;
        }
    });

    // Track # of chars deleted
    let numOfDeletions = 0;

    // Compare each char in obj1 w obj2 and count deletions
    for(char in obj1){
        if(obj2[char]){
            numOfDeletions += Math.abs(obj2[char] - obj1[char]);
        } else {
            numOfDeletions += obj1[char];
        }
    }
    // Compare each char in obj2 w obj1 and count deletions
    for(char in obj2){
        if(!obj1[char]){
            numOfDeletions += obj2[char];
        }
    }
    return numOfDeletions;
}

As far as I can tell, because there are 4 loops it would be O(4n) or just O(n). I say this because there are no nested loops. Is this correct? Any better solutions?


Solution

  • You could use a single object and sum only the absolut values.

    This solution uses the strings as array like objects.

    function anagramDelete(str1, str2) {
        var letters = {};
    
        Array.prototype.forEach.call(str1, char => letters[char] = (letters[char] || 0) + 1);
        Array.prototype.forEach.call(str2, char => letters[char] = (letters[char] || 0) - 1);
     
        return Object.keys(letters).reduce((r, k) => r + Math.abs(letters[k]), 0);
    }
    
    console.log(anagramDelete('anagram', 'function'));