bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch
🌱Newbie
0 XP
Back to Valid Anagram
āœļøFill the BlankpythonEasyArrays & Hashing

Valid Anagram - Fill the Blank (Python)

Complete the Python solution

Problem Brief

Given two strings s and t, return true if t is an anagram of s, and false otherwise. An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

Puzzle Hints
  1. Read the blank inside this exact statement: "return ___".

  2. Python dict.get() for safe access with default 0.

  3. Check the variable that is read immediately after the blank; that usually tells you what the blank must produce.

Asked at 21 companies
AffirmAmazonApple
Valid Anagram — Frequency Count
hashmap
1
5

Input: s="anagram", t="nagaram". Count char frequencies

dict & set in Pythonref

Python dict is a hash map with O(1) average access. set stores unique hashable elements. Both are built-in and highly optimized. Use collections.Counter for frequency counting and collections.defaultdict to avoid KeyError.

-dict — {key: value}, O(1) access via d[key]
-set() — unique elements, O(1) in/add/discard
-dict.get(key, default) avoids KeyError
-Counter(iterable) counts element frequencies
from collections import Counter, defaultdict

d = {}
d['a'] = 1
d.get('b', 0)       # 0 (default)

s = set([1, 2, 2])  # {1, 2}
2 in s               # True — O(1)

Counter("aab")       # Counter({'a': 2, 'b': 1})
Official docs →
dict & set in Pythonref

Python dict is a hash map with O(1) average access. set stores unique hashable elements. Both are built-in and highly optimized. Use collections.Counter for frequency counting and collections.defaultdict to avoid KeyError.

-dict — {key: value}, O(1) access via d[key]
-set() — unique elements, O(1) in/add/discard
-dict.get(key, default) avoids KeyError
-Counter(iterable) counts element frequencies
from collections import Counter, defaultdict

d = {}
d['a'] = 1
d.get('b', 0)       # 0 (default)

s = set([1, 2, 2])  # {1, 2}
2 in s               # True — O(1)

Counter("aab")       # Counter({'a': 2, 'b': 1})
Official docs →
How to think: Hash Map / Setguide

You need O(1) lookups — checking if something exists, counting frequencies, or finding pairs.

1.Ask: "Am I searching for something repeatedly?" → Hash Map
2.Ask: "Do I need to check existence?" → Set
3.Ask: "Do I need to count occurrences?" → Map with value = count
4.Ask: "Do I need to find a pair that satisfies a condition?" → Store complement in Map
5.The tradeoff: O(n) extra space buys you O(1) per lookup

vs Nested loops (O(n²)): You're comparing every element against every other — a Map does it in one pass

vs Sorting (O(n log n)): You just need existence/frequency checks, not order

find pairtwo numbers thatfrequencycountduplicateanagramgroup by
How to think: Hash Map / Setguide

You need O(1) lookups — checking if something exists, counting frequencies, or finding pairs.

1.Ask: "Am I searching for something repeatedly?" → Hash Map
2.Ask: "Do I need to check existence?" → Set
3.Ask: "Do I need to count occurrences?" → Map with value = count
4.Ask: "Do I need to find a pair that satisfies a condition?" → Store complement in Map
5.The tradeoff: O(n) extra space buys you O(1) per lookup

vs Nested loops (O(n²)): You're comparing every element against every other — a Map does it in one pass

vs Sorting (O(n log n)): You just need existence/frequency checks, not order

find pairtwo numbers thatfrequencycountduplicateanagramgroup by

Drag tokens into the blanks

False
True
1
0
1def isAnagram(s, t):
2 if len(s) != len(t):
3 return ___
4 count = {}
5 for c in s:
6 count[c] = count.get(c, 0) + ___
7 for c in t:
8 count[c] = count.get(c, 0) - 1
9 if count[c] < ___:
10 return False
11 return True