let str1 = "racecar is "
let str2 = "carerac is"


// const isAnagram = (str1, str2) => {
//     if(str1.length !== str2.length){
//         return false
//     }

    
//     //console.log(str1.toLowerCase().split("").sort().join("") , str2.toLowerCase().split("").sort().join(""))
//     //return str1.toLowerCase().split("").sort().join("") === str2.toLowerCase().split("").sort().join("")
//     const obj = {}
    
    
    
// }

// console.log(isAnagram(str1, str2))

function isAnagram(str1, str2) {
    // Convert to lowercase and remove non-alphanumeric characters
    function cleanString(str) {
        let cleanedStr = '';
        for (let char of str) {
            let lowerChar = char.toLowerCase();
            if ((lowerChar >= 'a' && lowerChar <= 'z') || (lowerChar >= '0' && lowerChar <= '9')) {
                cleanedStr += lowerChar;
            }
        }
        return cleanedStr;
    }

    str1 = cleanString(str1);
    str2 = cleanString(str2);

    // If lengths are not equal, they cannot be anagrams
    if (str1.length !== str2.length) {
        return false;
    }

    // Create a frequency count object
    let charCount = {};

    // Count characters in str1
    for (let char of str1) {
        charCount[char] = (charCount[char] || 1) + 1;
    }
    console.log(charCount)

    // Subtract counts using str2
    for (let char of str2) {
        if (!charCount[char]) {
            return false; // If a character is not found or count becomes negative, not an anagram
        }
        charCount[char]--;
    }
    console.log(charCount)
    

    return true; // If all counts are zero, it's an anagram
}

// Example usage:
console.log(isAnagram("listen", "silent")); // true
console.log(isAnagram("hello", "oellhh")); // true
console.log(isAnagram("hello", "world")); // false

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: