# Valid Anagram

By: Jay the Code Monkey
Posted: Jul 2, 2021 Updated: Apr 18, 2023

Valid Anagram

Get the code & notes on

Ask Questions & Share Solutions in

# Problem Description

Given two strings s and t, return true if t is an anagram of s, and false otherwise.

# Examples

Ex 1) Input: s = "anagram", t = "nagaram" Output: true

Ex 2) Input: s = "rat", t = "car" Output: false

# Constraints

  • s and t consist of lowercase English letters

# What is an Anagram?

  • A word or a phrase that is formed by rearranging the letters of a different word or phrase typically using all of the original letters exactly once.

# Assumptions

  • To clarify the provided constraints we're also assuming s and t contain no spaces, and we're only able to use each letter once.

# How to Check if t is an Anagram of s?

  • Since we're assuming we can only use each letter in s and t once, then for t to be an anagram of s they must be the same length.

  • We can first check that s and t are the same length, and if they aren't we can return false.

  • Now, if s and t are the same length, they must also contain the same letters.

# How to Check if s and t contain the same letters?

  • We're given s and t as strings, but we care about being able to examine each character in the strings. So, what we can do is create an array of characters for s and t.

    • Ex 1) s = "anagram" sArray = ["a", "n", "a", "g", "r", "a", "m"]
      t = "nagaram" tArray = ["n", "a", "g", "a", "r", "a", "m"]

    • Ex 2) s = "rat" sArray = ["r", "a", "t"]
      t = "car" tArray = ["c", "a", "r"]

  • We can use the split() method, and pass it a pattern of "".

  • This tells split() to split our strings into substrings where a "" pattern occurs which is between each character in our strings.

  • split() will then return an array of these substrings.

  • Now, we need a way to compare the letters of sArray with the letters of tArray.

  • We could take the first value of sArray and compare it with each value of tArray. Then break when the letters are the same, and keep track of which index the match occurred at. This will allow us to not use the value at the matched index again on the next comparison.

  • A simplier solution though is to realize that we can sort our arrays, and if t is an anagram of s, then our arrays will have all the same letters in the same order.

    • Ex 1) sArray.sort() = ["a", "a", "a", "g", "m", "n", "r"]
      tArray.sort() = ["a", "a", "a", "g", "m", "n", "r"]

    • Ex 2) sArray.sort() = ["a", "r", "t"]
      tArray.sort() = ["a", "c", "r"]

  • Then we can check if the letters in the arrays are equal by comparing each value at index i of sArray with each value at index i of tArray.

  • To do this we can loop over the length of one of the arrays, and if the values of our arrays at index i are not equal, then we can break the loop by returning false.

  • If the arrays are equal, then we'll loop over the entire length of our array and return true.

# Implementation

var isAnagram = function(s, t) {
  if (s.length === t.length) {
    let sArray = s.split('');
    let tArray = t.split('');

    sArray.sort();
    tArray.sort();

    for (let i = 0; i < s.length; i++) {
      if (sArray[i] !== tArray[i]) {
        return false;
      }
    }

    return true;

  } else {
    return false;
  }
};

let s = 'rat';
let t = 'car';

console.log(isAnagram(s, t));

Made by & for Code Monkeys 🐵