Valid Anagram
Valid Anagram
Two strings are anagrams if one can be formed by rearranging all the characters of the other. Given two strings s and t, determine whether t is an anagram of s. Your solve(...) function receives these inputs directly as ready-made arguments — no input reading or parsing needed. Just compute and return the answer. Example: s = "anagram", t = "nagaram" → YES Example: s = "rat", t = "car" → NO
Input format — only if your code reads from stdin
Input format (stdin): - First line: string s - Second line: string t Output: YES if t is an anagram of s, otherwise NO.
Approach
If the lengths differ, immediately print NO. Otherwise count character frequencies for both strings — if all counts match, they are anagrams. A single frequency array of size 26 (increment for s, decrement for t) works in O(n) time.
Input
Output
- •
1 ≤ len(s), len(t) ≤ 5×10^4, strings contain only lowercase English letters
More Strings problems
Browse all coding problems →Press Run to test your custom input, or Submit to judge against all test cases.
Sign in to continue
Create a free account to run code, submit solutions and track progress.
- Free forever — no credit card
- 10+ languages with live execution
- Points, streaks & leaderboards