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

Valid Anagram - Fill the Blank (Go)

Complete the Go 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: "if len(s) ___ len(t) {".

  2. Fill in the key parts of the go solution. Each blank represents a critical piece of the algorithm.

  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

map in Goref

Go has built-in map[K]V type. Access is O(1) average. The comma-ok idiom val, ok := m[key] checks existence without panicking. Use make(map[K]V) to initialize. No built-in Set — use map[T]bool or map[T]struct{}.

-make(map[K]V) — initialize a hash map
-val, ok := m[key] — comma-ok pattern
-delete(m, key) — remove entry
-map[T]struct{} as a memory-efficient Set
m := make(map[string]int)
m["a"] = 1
val, ok := m["a"]  // val=1, ok=true

// Set pattern
seen := make(map[int]struct{})
seen[42] = struct{}{}
_, exists := seen[42]  // exists=true
Official docs →
map in Goref

Go has built-in map[K]V type. Access is O(1) average. The comma-ok idiom val, ok := m[key] checks existence without panicking. Use make(map[K]V) to initialize. No built-in Set — use map[T]bool or map[T]struct{}.

-make(map[K]V) — initialize a hash map
-val, ok := m[key] — comma-ok pattern
-delete(m, key) — remove entry
-map[T]struct{} as a memory-efficient Set
m := make(map[string]int)
m["a"] = 1
val, ok := m["a"]  // val=1, ok=true

// Set pattern
seen := make(map[int]struct{})
seen[42] = struct{}{}
_, exists := seen[42]  // exists=true
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
<
null
!=
1func isAnagram(s string, t string) bool {
2 if len(s) ___ len(t) {
3 return ___
4 }
5 var freq [26]int
6 for idx := 0; idx ___ len(s); idx++ {
7 freq[s[idx] - 'a']++
8 freq[t[idx] - 'a']--
9 }
10 for idx := 0; idx < len(freq); idx++ {
11 if freq[idx] != 0 {
12 return false
13 }
14 }
15 return ___
16}