Complete the code to check if two strings are anagrams by sorting.
def are_anagrams(s1, s2): return sorted(s1) [1] sorted(s2)
We compare sorted versions of both strings for equality to check anagrams.
Complete the code to count characters using a dictionary for anagram check.
def count_chars(s): counts = {} for ch in s: counts[ch] = counts.get(ch, 0) [1] 1 return counts
We add 1 to the count for each character encountered.
Fix the error in the code to check anagrams by comparing character counts.
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
We check if count is less than 0 to detect extra characters in s2.
Fill both blanks to create a dictionary comprehension counting characters only if they appear more than once.
def repeated_chars(s): return {ch: s.count(ch) [1] ch in s if s.count(ch) [2] 1}
Use 'for' to iterate and '>' to filter characters appearing more than once.
Fill all three blanks to create a dictionary comprehension that maps characters to their counts only if count is greater than zero.
def char_counts(s): return [1]: [2] for [3] in set(s) if s.count([3]) > 0}
Use 'ch' as key and variable, and count characters with s.count(ch).