0
0
DSA Pythonprogramming~20 mins

Anagram Check Techniques in DSA Python - Practice Problems & Challenges

Choose your learning style9 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.