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

  1. Clean a and b: no spaces, lower case
  2. Same length?
  3. Sort the letters of both
  4. Sorted a == sorted b?
  5. Return True
  6. Return False

← All practice problems

Try AI Flowchart free →