Algorithm 101: 3 Ways to Check if Two Sentences are Anagrams
Tags: algorithms, javascript, anagram, beginners
Published: 2020-03-13 22:52:41

Building upon our knowledge from [word anagram](https://dev.to/ebereplenty/algorithm-101-3-ways-to-check-if-two-words-are-anagrams-1j6l) and [sentence palindrome](https://dev.to/ebereplenty/algorithm-101-3-ways-to-check-if-a-sentence-is-a-palindrome-443d), we will be looking at how to check for ``sentence anagram`` just as we promised while discussing [word anagram](https://dev.to/ebereplenty/algorithm-101-3-ways-to-check-if-two-words-are-anagrams-1j6l)

```javascript

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

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

```

### 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()

```javascript
      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;
      }
```

* regular expression, toLowerCase(), forEach(), match(), sort(), push(), JSON.stringify()

```javascript
      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;
      }
```

* toUpperCase(), toLowerCase(), forEach(), sort(), push(), hasOwnProperty(), JSON.stringify(), for...of...loop

```javascript
      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;
      }
```

### 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](https://dev.to/ebereplenty/algorithm-202-array-chunking-in-3-ways-1ank)

**[Twitter](https://twitter.com/eberetwit) | [LinkedIn](https://www.linkedin.com/in/samson-ebere-njoku-profile/) | [Github](https://github.com/EBEREGIT)**

Thank You For Your Time.