301. Valid Anagram

Easy
Frequency Map
Hashing

Problem

Given two strings s and t, return true if t is an anagram of s. An anagram is formed by rearranging the letters of another word using all original characters exactly once.

Examples

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⁴

s and t consist of lowercase English letters.

Hints

?? Two anagrams must have the same length.

?? Count how many times each character appears.

?? Compare the character frequencies.

Expected Complexity
Time: O(n)
Space: O(k)
Follow-up

How would you adapt the solution if the strings contained Unicode characters?

Practice Notes

Attempts: 0

Time spent: 0min

Hints used: 0

00:00
Loading...
Case 1
Case 2
Case 3
Input
"anagram", "nagaram"
Expected
true