Practice Problem
Valid Anagram
Difficulty: Easy
Given two strings, determine if one is an anagram of the other by comparing character frequencies.
Valid Anagram
Given two strings s and t, return true if t is an anagram of s, and false otherwise.
An anagram is a word formed by rearranging the letters of another word, using all the original letters exactly once.
Examples
Example 1:
Input: s = "anagram", t = "nagaram"
Output: true
Explanation: Rearranging the letters of "nagaram" gives "anagram".Example 2:
Input: s = "rat", t = "car"
Output: false
Explanation: 'r', 'a', 't' vs 'c', 'a', 'r'. The character 't' is in s but not in t, and 'c' is in t but not in s.Example 3:
Input: s = "a", t = "a"
Output: trueConstraints
1 <= s.length, t.length <= 5 * 10^4sandtconsist of lowercase English letters.
Expected Complexity
- Time: O(n)
- Space: O(1). the frequency map uses at most 26 keys (lowercase English letters)
EASY
Arrays
Hash Map / Dictionary
Strings
Beginner
0 views
Solution
Hints
Hint 1
Hint 2
Premium
Hint 3
Premium
This section is available for CodeSnatch Premium members only.
