Anagram check
Return True if a and b use exactly the same letters the same number of times, ignoring spaces and upper/lower case. "Listen" and "Silent" are anagrams.
Write is_anagram(a, b) in Python, isAnagram(a, b) in JavaScript or TypeScript, or int is_anagram(char a[], char b[]) in C. Level: Easy.
Examples
Input: a = 'listen', b = 'silent' Output: True
Input: a = 'hello', b = 'world' Output: False
- 0 ≤ length of a, b ≤ 1000
Hint
Remove the spaces and lower-case both. Then either sort the letters and compare, or count each letter in a, subtract the counts for b, and check everything ends at zero.
The approach, step by step
- Clean a and b: no spaces, lower case
- Same length?
- Sort the letters of both
- Sorted a == sorted b?
- Return True
- Return False