Anagram Checker
Check whether two strings are anagrams of each other — made of exactly the same letters, rearranged, ignoring case and spaces.
Approach: strip spaces and lower-case both strings, sort their characters, and compare the sorted results — two anagrams will always produce identical sorted character lists.
Input: Two lines: the first string, then the second string.
Output: One line: They are anagrams or They are not anagrams.
listen silent
They are anagrams
- 1 <= length of each string <= 1000
Hint 1
sorted(text) turns a string into a sorted list of its characters — two anagrams produce equal sorted lists.
Hint 2
Remove spaces with .replace(" ", "") and normalize case with .lower() before comparing.
Clean both strings the same way — lower-case them and strip out spaces — then compare sorted(clean1) to sorted(clean2). Two strings are anagrams exactly when they contain the same characters in some order, which is exactly what comparing their sorted character lists checks.