JavaScript anagram comparison

前端 未结 16 2097
走了就别回头了
走了就别回头了 2020-12-17 02:30

I\'m trying to compare two strings to see if they are anagrams.

My problem is that I\'m only comparing the first letter in each string. For example, \"Mary\" and \"

相关标签:
16条回答
  • 2020-12-17 03:06

    I think this is quite easy and simple.

    function checkAnagrams(str1, str2){
    
      var newstr1 = str1.toLowerCase().split('').sort().join();
      var newstr2 = str2.toLowerCase().split('').sort().join();
    
      if(newstr1 == newstr2){
        console.log("String is Anagrams");
      }
      else{
        console.log("String is Not Anagrams");
      }
    
    }
    
    checkAnagrams("Hello", "lolHe");
    checkAnagrams("Indian", "nIndisn");
    
    0 讨论(0)
  • 2020-12-17 03:06

    Here's my contribution, I had to do this exercise for a class! I'm finally understanding how JS works, and as I was able to came up with a solution (it's not - by far - the best one, but it's ok!) I'm very happy I can share this one here, too! (although there are plenty solutions here already, but whatever :P )

    function isAnagram(string1, string2) {
        // first check: if the lenghts are different, not an anagram 
        if (string1.length != string2.length)
            return false
        else {
        // it doesn't matter if the letters are capitalized, 
        // so the toLowerCase method ensures that...
            string1 = string1.toLowerCase()
            string2 = string2.toLowerCase()
            
        // for each letter in the string (I could've used for each :P)
            for (let i = 0; i < string1.length; i++) {
                // check, for each char in string2, if they are NOT somewhere at string1
                if (!string1.includes(string2.charAt(i))) {
                    return false
                }
                else {
                    // if all the chars are covered 
                    // and the condition is the opposite of the previous if
                    if (i == (string1.length - 1))
                        return true
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-17 03:07

    If you want to write a function, without using inbuilt one, Check the below solution.

    function isAnagram(str1, str2) {
    
      if(str1 === str2) {
        return true;
      }
    
      let srt1Length = str1.length;
      let srt2Length = str2.length;
    
      if(srt1Length !== srt2Length) {
        return false;
      }
    
      var counts = {};
    
      for(let i = 0; i < srt1Length; i++) {
        let index = str1.charCodeAt(i)-97;
        counts[index] = (counts[index] || 0) + 1;
      }
    
      for(let j = 0; j < srt2Length; j++) {
        let index = str2.charCodeAt(j)-97;
        if (!counts[index]) { 
          return false; 
        }
        counts[index]--;
      }
    
      return true;
    }
    
    0 讨论(0)
  • 2020-12-17 03:07

    First of all, you can do the length check before the for loop, no need to do it for each character...

    Also, "break" breaks the whole for loop. If you use "continue" instead of "break", it skips the current step. That is why only the first letters are compared, after the first one it quits the for loop.

    I hope this helps you.

    0 讨论(0)
提交回复
热议问题