← All coding problems
Valid Anagram
easyArrays & Hashing+10 XPTime limit 2s
Two words are anagrams if one can be formed by rearranging all the letters of the other (for example listen and silent). Decide whether s and t are anagrams.
Input:
- line 1:
s - line 2:
t
Output: true or false.
Constraints: 1 ≤ length of s, t ≤ 10⁵; only lowercase English letters.
Think about it: sorting both works in O(n log n). Counting the 26 letters works in O(n).
Sample input 1
anagram nagaram
Sample output 1
true
Sample input 2
rat car
Sample output 2
false
📖 Stuck? Learn the pattern
Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).