2. Valid Anagram
Difficulty: easy · String, Hash Table, Sorting
Problem
Given two strings `s` and `t`, print `true` if `t` is an **anagram** of `s`, and `false` otherwise. An anagram uses exactly the same letters as the original, each the same number of times, in any order.
Input
Two lines: the first holds `s`, the second holds `t`.
Output
Print `true` or `false`.
Example 1
Input: anagram nagaram Output: true
Explanation: Both use exactly the letters aaagmnr, rearranged.
Example 2
Input: rat car Output: false
Explanation: The letter counts differ (for example "c"), so they are not anagrams.
Example 3
Input: listen silent Output: true
Explanation: Both use exactly the letters eilnst, rearranged.
Example 4
Input: a ab Output: false
Explanation: "a" has 1 letters and "ab" has 2, so they cannot be anagrams.
Example 5
Input: aacc ccac Output: false
Explanation: The letter counts differ (for example "a"), so they are not anagrams.
Example 6
Input: dusty study Output: true
Explanation: Both use exactly the letters dstuy, rearranged.
Example 7
Input: night thing Output: true
Explanation: Both use exactly the letters ghint, rearranged.
Example 8
Input: abc abd Output: false
Explanation: The letter counts differ (for example "c"), so they are not anagrams.
Example 9
Input: elbow below Output: true
Explanation: Both use exactly the letters below, rearranged.
Example 10
Input: aabbcc abcabc Output: true
Explanation: Both use exactly the letters aabbcc, rearranged.
Constraints
- 1 <= s.length, t.length <= 5 * 10^4 - `s` and `t` consist of lowercase English letters.
Solutions are judged against 10 sample and 5 hidden tests. Sign in to solve it · All problems