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 another word or phrase, using all the original letters exactly once.
Example 1
Input
anagram nagaram
Output
true
Explanation
The string "nagaram" is formed by rearranging all the characters of "anagram", so it is a valid anagram.
Example 2
Input
rat car
Output
false
Explanation
The characters in "car" do not match the characters in "rat", so it is not an anagram.
Constraints
Hints:
Hint 1
If the two strings have different lengths, they cannot be anagrams.
Hint 2
Count the frequency of every character in both strings using a hash map or frequency array. The strings are anagrams if all frequencies match.
Author & Technical Reviewer
Technically reviewed by: ExamAdda Technical Review Team
Technical Reviewers, ExamAdda
Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.
Published
Aug 5, 2026
Last updated
Aug 6, 2026
Content Verification Methodology
Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.
Expected Output