LeetCode Python/Java/C++/JS  >  Hash Table  >  242. Valid Anagram  >  Solved in Java, Python, JavaScript, C#, Go, Ruby, C++  >  GitHub or Repost

LeetCode link: 242. Valid Anagram, difficulty: Easy.

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, 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

Constraints:

  • 1 <= s.length, t.length <= 5 * 10^4
  • s and t consist of lowercase English letters.

Intuition

Webmaster (Zhang Jian): πŸ‘‹

Hi everyone! I am Zhang Jian.
I know the challenge of transitioning from mastering algorithms to actually landing a great job. That's why, in addition to this resource, I personally developed leader.me!

πŸš€ leader.me is the ultimate all-in-one platform for programmers to build their personal brand, featuring portfolio hosting, resume builders, and integrated blogs.

Build your programmer brand at leader.me β†’


  1. If the lengths of the two strings are different, return false directly.
  2. Use two hash tables to store the statistics of the two strings respectively, with the key being the character and the value being the number of characters.
  3. Compare the two hash tables to see if they are equal.

Complexity

Time complexity

O(N)

Space complexity

O(N)

Java #

class Solution {
    public boolean isAnagram(String s, String t) {
        if (s.length() != t.length()) {
            return false;
        }

        var sCharToCount = new HashMap<Character, Integer>();
        for (var character : s.toCharArray()) {
            sCharToCount.put(character, sCharToCount.getOrDefault(character, 0) + 1);
        }

        var tCharToCount = new HashMap<Character, Integer>();
        for (var character : t.toCharArray()) {
            tCharToCount.put(character, tCharToCount.getOrDefault(character, 0) + 1);
        }

        return sCharToCount.equals(tCharToCount);
    }
}

Python #

# from collections import defaultdict

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        if len(s) != len(t):
            return False

        s_char_to_count = defaultdict(int)
        for char in s:
            s_char_to_count[char] += 1

        t_char_to_count = defaultdict(int)
        for char in t:
            t_char_to_count[char] += 1

        return s_char_to_count == t_char_to_count

JavaScript #

var isAnagram = function (s, t) {
  if (s.length != t.length) {
    return false;
  }

  const sCharToCount = new Map()
  const tCharToCount = new Map()

  for (const char of s) {
    sCharToCount.set(char, (sCharToCount.get(char) || 0) + 1)
  }

  for (const char of t) {
    tCharToCount.set(char, (tCharToCount.get(char) || 0) + 1)
  }

  return _.isEqual(sCharToCount, tCharToCount)
};

C# #

public class Solution
{
    public bool IsAnagram(string s, string t)
    {
        if (s.Length != t.Length)
            return false;

        var sCharToCount = new Dictionary<char, int>();
        var tCharToCount = new Dictionary<char, int>();

        foreach (char character in s)
            sCharToCount[character] = sCharToCount.GetValueOrDefault(character, 0) + 1;

        foreach (char character in t)
            tCharToCount[character] = tCharToCount.GetValueOrDefault(character, 0) + 1;

        foreach (var entry in sCharToCount)
        {
            if (entry.Value != tCharToCount.GetValueOrDefault(entry.Key))
            {
                return false;
            }
        }

        return true;
    }
}

Go #

import "reflect"

func isAnagram(s string, t string) bool {
    if len(s) != len(t) {
        return false
    }

    // Create frequency maps for both strings
    sCharCount := make(map[rune]int)
    for _, char := range s {
        sCharCount[char]++
    }

    tCharCount := make(map[rune]int)
    for _, char := range t {
        tCharCount[char]++
    }

    return reflect.DeepEqual(sCharCount, tCharCount)
}

Ruby #

def is_anagram(s, t)
  return false if s.length != t.length

  s_char_to_count = Hash.new(0)
  t_char_to_count = Hash.new(0)

  s.each_char do |char|
    s_char_to_count[char] += 1
  end

  t.each_char do |char|
    t_char_to_count[char] += 1
  end

  s_char_to_count == t_char_to_count
end

C++ #

class Solution {
public:
    bool isAnagram(string s, string t) {
        if (s.length() != t.length()) {
            return false;
        }

        unordered_map<char, int> s_char_to_count;
        for (char character : s) {
            s_char_to_count[character]++;
        }

        unordered_map<char, int> t_char_to_count;
        for (char character : t) {
            t_char_to_count[character]++;
        }

        return s_char_to_count == t_char_to_count;
    }
};

Other languages

Welcome to contribute code to LeetCode.blog GitHub -> 242. Valid Anagram. Thanks!

Level Up Your Developer Identity

πŸš€ While mastering algorithms is key, showcasing your talent is what gets you hired.
We recommend leader.me β€” the ultimate all-in-one personal branding platform for programmers.

The All-In-One Career Powerhouse:

  • πŸ“„ Resume, Portfolio & Blog: Integrate your skills, projects, and writing into one stunning site.
  • 🌐 Free Custom Domain: Bind your own personal domain for freeβ€”forever.
  • ✨ Premium Subdomains: Stand out with elite tech handle like name.leader.me.

Build Your Programmer Brand at leader.me β†’