Valid Anagram Feature Image

Valid Anagram Solution | Typescript | Leetcode problem

In this post, I am going to solve the Leetcode problem called Valid Anagram and provide solution using TypeScript. In this problem, given that we have 2 strings, we have to tell whether they are Anagram or not. For those who don’t know, 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.

Though process for the valid anagram problem

Before trying to solve we can find a few clues about anagrams.

  1. As I stated earlier, both of the string will have same amount of letter.
  2. The number of letter occurrences will be same.

To solve a problem, I always check for the simple conditional part. That way, I can take out the obvious test cases which would fail. In this case, I need to check both of the strings are of same length. If not, then they are not anagram. Plain and simple. Which is why I wrote this line in the beginning of the code.

if(s.length !== t.length) return false;

For the second point, we need to check how many times a character/letter used. So I wrote a simple for loop that basically checks every character one by one and they it checks in it’s own dictionary to see if that was used before or not. If yes, I increase the value by one. If not, I add a new property in that dictionary. (Note: In JS/TS there is no direct way to use dictionary like python/C#, so we have to create an object and use it like a dictionary). Here is the code for that.

for(let i = 0; i<text.length; i++){
        const ch = text.charAt(i);

        if (ch in dict) {
            dict[ch] += 1;
        } else {
            dict[ch] = 1
        }
    }

I encapsulated the dictionary creation part into a nice little function so that it is easier to read the code. Once I am able to create the dictionary for both the original and the test string, I called a lodash function called isEqual. It performs a deep comparison between two objects. Here is the full code for the valid anagram solution.

Full Valid Anagram Solution

function isAnagram(s: string, t: string): boolean {
    if(s.length !== t.length) return false;
    
    const input = getDictionary(s);
    const testInput = getDictionary(t);
    
    return _.isEqual(input, testInput);
};

function getDictionary(text: string) {
    let dict: { [letter: string]: number } = {};
    
    for(let i = 0; i<text.length; i++){
        const ch = text.charAt(i);

        if (ch in dict) {
            dict[ch] += 1;
        } else {
            dict[ch] = 1
        }
    }
    
    return dict;
}

Hello readers, If you are like me who wants to get better at coding, please check out my other blog posts where I solve other leetcode problems.

In the next post, I am going to solve Two Sum problem. In the meantime, you can checkout the post I have written on another problem called Contains Duplicate

1 thought on “Valid Anagram Solution | Typescript | Leetcode problem”

  1. Pingback: Contains Duplicate - Leetcode problem - Al Fahim

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.