0
0
DSA Pythonprogramming~10 mins

Anagram Check Techniques in DSA Python - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if two strings are anagrams by sorting.

DSA Python
def are_anagrams(s1, s2):
    return sorted(s1) [1] sorted(s2)
Drag options to blanks, or click blank then click option'
A==
B!=
C<
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using != instead of == causes wrong result.
2fill in blank
medium

Complete the code to count characters using a dictionary for anagram check.

DSA Python
def count_chars(s):
    counts = {}
    for ch in s:
        counts[ch] = counts.get(ch, 0) [1] 1
    return counts
Drag options to blanks, or click blank then click option'
A+=
B-=
C*=
D/=
Attempts:
3 left
💡 Hint
Common Mistakes
Using -= causes counts to decrease incorrectly.
3fill in blank
hard

Fix the error in the code to check anagrams by comparing character counts.

DSA Python
def are_anagrams(s1, s2):
    if len(s1) != len(s2):
        return False
    counts = {}
    for ch in s1:
        counts[ch] = counts.get(ch, 0) + 1
    for ch in s2:
        if ch not in counts or counts[ch] [1] 0:
            return False
        counts[ch] -= 1
    return True
Drag options to blanks, or click blank then click option'
A==
B!=
C<=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using == 0 misses negative counts.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension counting characters only if they appear more than once.

DSA Python
def repeated_chars(s):
    return {ch: s.count(ch) [1] ch in s if s.count(ch) [2] 1}
Drag options to blanks, or click blank then click option'
Afor
Bif
C>
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'if' instead of 'for' causes syntax error.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps characters to their counts only if count is greater than zero.

DSA Python
def char_counts(s):
    return [1]: [2] for [3] in set(s) if s.count([3]) > 0}
Drag options to blanks, or click blank then click option'
Ach
Bs.count(ch)
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names causes errors.