Check Anagram
Check anagrams by normalizing case and ordinary spaces. Students compare sorted character lists and retain duplicate counts, punctuation, and other whitespace.
1# Take two strings2s1 = input("Enter first string: ").lower().replace(" ", "")3s2 = input("Enter second string: ").lower().replace(" ", "")45# Check anagram6if sorted(s1) == sorted(s2):7 print("Anagrams")8else:9 print("Not anagrams")
Output
Enter first string: listen Enter second string: silent Anagrams
In this one
- Normalize selected differences
- Compare character multisets
- Retain repeated-letter counts
How it works
- 1
Lowercase both responses and remove ordinary spaces.
- 2
Sort their remaining character sequences.
- 3
Print anagrams only when those sorted lists agree.
Edge cases
Are repeated letters significant?
Are repeated letters significant?
Yes. Their counts must match.
Are tabs ignored?
Are tabs ignored?
No. Only ordinary spaces are removed.
What's going on
Both responses are lowercased and have ordinary spaces removed. Sorting then places their remaining characters into comparable order.
Equal sorted lists mean the same characters occur with the same frequencies. listen and silent pass. Punctuation and tabs are not removed, so they still affect equality. This is a literal character-multiset test after specific cleanup, not a language-aware spelling analysis.
Are repeated letters significant? Yes. Their counts must match.
Are tabs ignored? No. Only ordinary spaces are removed.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Why sort before comparing?
Pick an answer to check it.
Question 2
Are repeated letters significant?
Pick an answer to check it.
Questions people ask
Are repeated letters significant?
Yes. Their counts must match.
Are tabs ignored?
No. Only ordinary spaces are removed.
Why sort before comparing?
Order should not matter for anagrams.
๐Related Content
- ๐
Phase 5 - Learn Concepts
Review Phase 5 concepts and explanations
- ๐
Phase 5 - Quiz
Test your Phase 5 understanding with quiz questions
- ๐ป
Phase 5 - All Practice Problems
Explore all practice problems for Phase 5
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum