Determine whether two strings contain exactly the same characters with the same multiplicities.
Given two strings, determine whether one string is an anagram of the other. Two strings are anagrams if they use the same characters the same number of times, possibly in a different order.
A valid solution should handle repeated characters correctly and return whether the strings match as multisets of characters.
Input Format
- Two strings,
sandt. - Each string may contain lowercase letters only unless the platform statement says otherwise.
Output Format
- Return
trueiftis an anagram ofs. - Otherwise, return
false.
Constraints
- The strings may be empty.
- Compare character counts rather than order.
- A typical interview solution runs in linear time with respect to the total length of the strings.
Example 1
Input
s = "anagram", t = "nagaram"
Output
true
Explanation
Both strings contain the same letters with the same frequencies.
Example 2
Input
s = "rat", t = "car"
Output
false
Explanation
The strings have different character counts.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.