DSA problem
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 or phrase formed by rearranging the letters of a different word or phrase, using all the original letters exactly once. Two strings are anagrams if and only if they have the same length and the same multiset of characters.
For example, "anagram" and "nagaram" are anagrams, while "rat" and "car" are not because their character counts differ.
Examples
Example 1
Input:
s = "anagram", t = "nagaram"Output:
trueExplanation: Both strings contain the letters a x3, n x1, g x1, r x1, m x1, so t is a rearrangement of s.
Example 2
Input:
s = "rat", t = "car"Output:
falseExplanation: s contains a t while t contains a c, so the character counts do not match.
Constraints
- 1 <= s.length, t.length <= 5 * 10^4
- s and t consist of lowercase English letters.
3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.