Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Check Anagram

Check anagrams by normalizing case and ordinary spaces. Students compare sorted character lists and retain duplicate counts, punctuation, and other whitespace.

AdvancedPhase 5 - Category 5: Advanced String LogicExample 1 of 14
check-anagram.py
1# Take two strings
2s1 = input("Enter first string: ").lower().replace(" ", "")
3s2 = input("Enter second string: ").lower().replace(" ", "")
4
5# Check anagram
6if 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. 1

    Lowercase both responses and remove ordinary spaces.

  2. 2

    Sort their remaining character sequences.

  3. 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.