Bird
Raised Fist0
DSA Pythonprogramming~20 mins

Anagram Check Techniques in DSA Python - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Anagram Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Sorting-Based Anagram Check
What is the output of this Python code that checks if two words are anagrams by sorting their letters?
DSA Python
def are_anagrams(s1, s2):
    return sorted(s1) == sorted(s2)

print(are_anagrams('listen', 'silent'))
print(are_anagrams('hello', 'bello'))
AFalse\nFalse
BTrue\nFalse
CTrue\nTrue
DFalse\nTrue
Attempts:
2 left
💡 Hint
Sorting the letters of both words and comparing them checks if they have the same letters.
❓ Predict Output
intermediate
2:00remaining
Output of Frequency Count Anagram Check
What is the output of this Python code that checks if two words are anagrams by counting letter frequencies?
DSA Python
from collections import Counter

def are_anagrams(s1, s2):
    return Counter(s1) == Counter(s2)

print(are_anagrams('triangle', 'integral'))
print(are_anagrams('apple', 'papel'))
AFalse\nTrue
BFalse\nFalse
CTrue\nFalse
DTrue\nTrue
Attempts:
2 left
💡 Hint
Counting how many times each letter appears helps check if two words are anagrams.
🔧 Debug
advanced
2:00remaining
Find the Error in Anagram Check Code
This code tries to check if two strings are anagrams but has a bug. What error does it cause when run?
DSA Python
def are_anagrams(s1, s2):
    counts = {}
    for ch in s1:
        counts[ch] = counts.get(ch, 0) + 1
    for ch in s2:
        counts[ch] = counts.get(ch, 0) - 1
    return all(value == 0 for value in counts.values())

print(are_anagrams('abc', 'cab'))
print(are_anagrams('abc', 'abb'))
ATrue\nFalse
BFalse\nFalse
CTrue\nTrue
DKeyError
Attempts:
2 left
💡 Hint
The code uses a dictionary to count letters, adding for s1 and subtracting for s2.
🧠 Conceptual
advanced
2:00remaining
Which Method is Most Efficient for Large Strings?
For very long strings, which anagram check method is generally the most efficient in terms of time complexity?
ASorting both strings and comparing them
BUsing recursion to check all permutations
CCounting letter frequencies using a fixed-size array or dictionary
DComparing each character of one string with every character of the other
Attempts:
2 left
💡 Hint
Counting frequencies can be done in one pass each, avoiding sorting overhead.
❓ Predict Output
expert
3:00remaining
Output of Case-Insensitive Anagram Check with Spaces
What is the output of this code that checks if two phrases are anagrams ignoring case and spaces?
DSA Python
def clean_string(s):
    return ''.join(ch.lower() for ch in s if ch.isalpha())

def are_anagrams(s1, s2):
    return sorted(clean_string(s1)) == sorted(clean_string(s2))

print(are_anagrams('Dormitory', 'Dirty room'))
print(are_anagrams('Conversation', 'Voices rant on'))
ATrue\nTrue
BFalse\nTrue
CTrue\nFalse
DFalse\nFalse
Attempts:
2 left
💡 Hint
Removing spaces and ignoring case helps check if phrases are anagrams.

Practice

(1/5)
1. What does it mean if two strings are anagrams of each other?
easy
A. They contain the same characters in any order
B. They have the same length but different characters
C. They start with the same character
D. They end with the same character

Solution

  1. Step 1: Understand the definition of anagrams

    Anagrams are words or phrases made by rearranging the letters of another, so they must have the same characters.
  2. Step 2: Compare the character sets

    If two strings have exactly the same characters, just in different orders, they are anagrams.
  3. Final Answer:

    They contain the same characters in any order -> Option A
  4. Quick Check:

    Anagrams = same characters [OK]
Hint: Check if sorted strings are equal to confirm anagrams [OK]
Common Mistakes:
  • Thinking same length means anagrams
  • Only checking first or last character
  • Ignoring character counts
2. Which of the following Python code snippets correctly checks if two strings s1 and s2 are anagrams using sorting?
easy
A. s1 == s2[::-1]
B. sorted(s1) == sorted(s2)
C. s1.sort() == s2.sort()
D. len(s1) == len(s2)

Solution

  1. Step 1: Understand sorting strings in Python

    The sorted() function returns a new sorted list of characters from the string.
  2. Step 2: Compare sorted lists for equality

    If sorted(s1) equals sorted(s2), both strings have the same characters in the same counts.
  3. Final Answer:

    sorted(s1) == sorted(s2) -> Option B
  4. Quick Check:

    sorted strings equal means anagrams [OK]
Hint: Use sorted() to compare strings for anagram check [OK]
Common Mistakes:
  • Using .sort() on strings (strings have no sort method)
  • Comparing reversed strings instead of sorted
  • Only checking length equality
3. What will be the output of the following code?
from collections import Counter
s1 = 'listen'
s2 = 'silent'
print(Counter(s1) == Counter(s2))
medium
A. False
B. TypeError
C. True
D. None

Solution

  1. Step 1: Understand Counter usage

    Counter counts the frequency of each character in the string.
  2. Step 2: Compare Counters for equality

    Since 'listen' and 'silent' have the same characters with same counts, their Counters are equal.
  3. Final Answer:

    True -> Option C
  4. Quick Check:

    Counter equality means anagrams [OK]
Hint: Use Counter to count chars and compare for anagrams [OK]
Common Mistakes:
  • Expecting False because strings differ in order
  • Confusing Counter with sorting
  • Not importing collections module
4. Find the error in this anagram check code:
def are_anagrams(s1, s2):
    return s1.sort() == s2.sort()
medium
A. Missing import statement
B. Function should return True always
C. Comparison should be with reversed strings
D. Strings do not have a sort() method

Solution

  1. Step 1: Check string methods

    Strings in Python do not have a sort() method; only lists do.
  2. Step 2: Identify correct approach

    Use sorted(s1) and sorted(s2) which return sorted lists for comparison.
  3. Final Answer:

    Strings do not have a sort() method -> Option D
  4. Quick Check:

    sort() not valid on strings [OK]
Hint: Use sorted() on strings, not .sort() method [OK]
Common Mistakes:
  • Trying to call .sort() on strings
  • Comparing reversed strings instead of sorted
  • Ignoring return value of sorted()
5. You want to check if two strings are anagrams but ignore spaces and capitalization. Which code snippet correctly implements this?
hard
A. sorted(s1.lower().replace(' ', '')) == sorted(s2.lower().replace(' ', ''))
B. Counter(s1) == Counter(s2)
C. s1 == s2
D. len(s1) == len(s2)

Solution

  1. Step 1: Normalize strings by lowering case and removing spaces

    Use s1.lower().replace(' ', '') and similarly for s2 to ignore case and spaces.
  2. Step 2: Sort normalized strings and compare

    Sorting ensures characters are in order for easy comparison.
  3. Final Answer:

    sorted(s1.lower().replace(' ', '')) == sorted(s2.lower().replace(' ', '')) -> Option A
  4. Quick Check:

    Normalize then sort to check anagrams ignoring spaces/case [OK]
Hint: Lowercase and remove spaces before sorting for anagram check [OK]
Common Mistakes:
  • Not removing spaces before comparison
  • Not converting to lowercase
  • Using Counter without normalization
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard