DEV Community

NJOKU SAMSON EBERE
NJOKU SAMSON EBERE

Posted on • Edited on

Algorithm 101: 3 Ways to Check if Two Sentences are Anagrams

Building upon our knowledge from word anagram and sentence palindrome, we will be looking at how to check for sentence anagram just as we promised while discussing word anagram


sentenceAnagram("McDonald's restaurants", "Uncle Sam's standard rot"); // true

sentenceAnagram("Church of Scientology", "rich-chosen goofy cult"); // true

Enter fullscreen mode Exit fullscreen mode

Prerequisite

To benefit from this article, you need to have basic understanding of javascript's string, object and array methods.

Let's do this using:

  • toLowerCase(), map(), includes(), sort(), join()
      function sentenceAnagram(sentenceA, sentenceB) {
        let newSentenceA = sentenceA.toLowerCase();
        let newSentenceB = sentenceB.toLowerCase();
        let alphabet = "abcdefghijklmnopqrstuvwxyz";
        let numeric = "0123456789";
        let alphanumeric = [...alphabet, ...numeric];

        function createCharArray(sentence) {
          let charArray = [...sentence].map(char => {
            if (alphanumeric.includes(char)) {
              return char;
            }
          });

          return charArray.sort().join("");
        }

        charArrayA = createCharArray(newSentenceA);
        charArrayB = createCharArray(newSentenceB);

        return charArrayA === charArrayB;
      }
Enter fullscreen mode Exit fullscreen mode
  • regular expression, toLowerCase(), forEach(), match(), sort(), push(), JSON.stringify()
      function sentenceAnagram(sentenceA, sentenceB) {
        let newSentenceA = sentenceA.toLowerCase();
        let newSentenceB = sentenceB.toLowerCase();
        let regEx = /\w/gi;

        function createCharArray(sentence) {
          let charArray = [];
          [...sentence].forEach(char => {
            if (char.match(regEx)) {
              charArray.push(char);
            }
          });

          return JSON.stringify(charArray.sort());
        }

        charArrayA = createCharArray(newSentenceA);
        charArrayB = createCharArray(newSentenceB);

        return charArrayA === charArrayB;
      }
Enter fullscreen mode Exit fullscreen mode
  • toUpperCase(), toLowerCase(), forEach(), sort(), push(), hasOwnProperty(), JSON.stringify(), for...of...loop
      function sentenceAnagram(sentenceA, sentenceB) {
        let newSentenceA = sentenceA.toLowerCase();
        let newSentenceB = sentenceB.toLowerCase();

        function isDigit(char) {
          return char >= 0 && char <= 9;
        }

        function isLetter(char) {
          return char.toLowerCase() != char.toUpperCase();
        }

        function createCharObject(sentence) {
          let charObject = {};
          let charArray = [];

          // sanitize the sentence
          [...sentence].sort().forEach(char => {
            if (isDigit(char) || isLetter(char)) {
              charArray.push(char);
            }
          });

          let newCharArray = charArray
            .join("")
            .split(" ")
            .join("");

          // create an object of the array
          for (char of [...newCharArray]) {
            if (charObject.hasOwnProperty(char)) {
              charObject[char]++;
            } else {
              charObject[char] = 1;
            }
          }

          return JSON.stringify(charObject);
        }

        charArrayA = createCharObject(newSentenceA);
        charArrayB = createCharObject(newSentenceB);

        return charArrayA === charArrayB;
      }
Enter fullscreen mode Exit fullscreen mode

Conclusion

There are many ways to solve problems programmatically. I will love to know other ways you solved yours in the comment section.

If you have questions, comments or suggestions, please drop them in the comment section.

You can also follow and message me on social media platforms.

Up Next: Algorithm 202: Array Chunking in 3 Ways

Twitter | LinkedIn | Github

Thank You For Your Time.

Top comments (0)