CodeSpeek

Valid Anagram

Easy · Arrays & Hashing

Given two strings s and t, determine whether t is an anagram of s, meaning t can be formed by rearranging all the letters of s exactly once with no letters added, removed, or changed. Return true if t is an anagram of s, and false otherwise. Comparison is case-sensitive.

Examples

Input:  s = "anagram", t = "nagaram"
Output: true
Why:    Both strings contain the same letters with the same frequencies.
Input:  s = "rat", t = "car"
Output: false
Why:    The letters do not match up in frequency.
Input:  s = "a", t = "ab"
Output: false
Why:    The strings have different lengths so one cannot be a rearrangement of the other.

Constraints

1 <= s.length, t.length <= 5*10^4, s and t consist of printable characters, comparison is case-sensitive

Practise it by voice

Describe the solution out loud and the interviewer writes exactly what you say, asks when you are vague, and runs the tests in your browser.

Practise Valid Anagram

This statement is written for CodeSpeek. The problem is part of the NeetCode 150 list; Watch NeetCode's explanation of Valid Anagram. Reference solutions from the NeetCode repository (MIT) are used to verify our tests.