LeetCode 150: Valid Anagram.

LeetCode 150: Valid Anagram.

·

2 min read

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

An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

Example 1:

Input: s = "anagram", t = "nagaram"
Output: true

Example 2:

Input: s = "rat", t = "car"
Output: false

Solution 1:

We convert the same string into a string and each character is sorted based on its alphabetical position. The result of everything will be the same and we compare it.

If they are the same string return true otherwise false.

var isAnagram = fuction(value, compareValue) {
    if (value.length !== compareValue.length) return false;
    return value.split("").sort().join("") === compareValue.split("").sort().join("");

Solution 2:

We can use an integer array freq of length 26 to keep track of the frequency of each letter in the two strings. We first check if the length of both strings is the same. If not, we return false because anagrams must have the same length. We loop through each character in both strings and increment the frequency of the corresponding letter in s and decrement the frequency of the corresponding letter in t.
Finally, we loop through the freq array and check if all requencies are zero. If not, we return false. If all frequencies are zero, we return true because both strings are anagrams of each other.

var isAnagram = function(s, t) {
    if (s.length !== t.length) return false;
    var freq = new Array(26).fill(0);
    for (var i = 0; i < s.length; i++) {
        freq[s.charCodeAt(i) - 'a'.charCodeAt(0)]++;
        freq[t.charCodeAt(i) - 'a'.charCodeAt(0)]--;
    }

    return freq.every((item) => item === 0)
};