📘
Valid Anagram
EasyArrays & Hashing
Given two strings s and t, return True if t is an anagram of s, and False otherwise.
An anagram uses the exact same characters with the exact same frequencies.
Example:
Input:
anagram nagaram
Output:
True
Reasoning:
- The function first checks if the two input strings
sandthave the same length. In this case, both "anagram" and "nagaram" have 7 characters. - It then compares the frequency of each character in both strings. For "anagram" and "nagaram", the characters and their frequencies are:
- a: 3
- n: 1
- g: 1
- r: 1
- m: 1
- Since both strings have the same characters with the same frequencies, the function returns
True, indicating that "nagaram" is an anagram of "anagram". - The final output is therefore:
True
Constraints:
- 1 <= len(s), len(t) <= 5 * 10^4
- s and t consist of lowercase English letters
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.