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

Valid Anagram - Fill the Blank (C++)

Complete the C++ 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(s.size() != t.size()) return ___;".

  2. Fill in the key parts of the cpp 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

unordered_map & unordered_set in C++ref

C++ unordered_map and unordered_set use hash tables for O(1) average operations. Include <unordered_map> and <unordered_set>. For sorted order, use map/set (O(log n) red-black tree).

-unordered_map<K,V> — O(1) insert/find/erase
-unordered_set<T> — O(1) insert/count/erase
-operator[] auto-inserts default if missing
-count(key) returns 0 or 1 (use as boolean)
#include <unordered_map>
#include <unordered_set>

unordered_map<string, int> m;
m["a"] = 1;
m.count("a");  // 1 (exists)

unordered_set<int> s = {1, 2, 3};
s.count(2);    // 1
Official docs →
unordered_map & unordered_set in C++ref

C++ unordered_map and unordered_set use hash tables for O(1) average operations. Include <unordered_map> and <unordered_set>. For sorted order, use map/set (O(log n) red-black tree).

-unordered_map<K,V> — O(1) insert/find/erase
-unordered_set<T> — O(1) insert/count/erase
-operator[] auto-inserts default if missing
-count(key) returns 0 or 1 (use as boolean)
#include <unordered_map>
#include <unordered_set>

unordered_map<string, int> m;
m["a"] = 1;
m.count("a");  // 1 (exists)

unordered_set<int> s = {1, 2, 3};
s.count(2);    // 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
0
1
1// hashmap solution, similar to neetcode python implementation
2class Solution {
3public:
4 bool isAnagram(string s, string t) {
5 if(s.size() != t.size()) return ___;
6 unordered_map<char, int> smap;
7 unordered_map<char, int> tmap;
8 for(int i = ___; i < s.size(); i++){
9 smap[s[i]]++;
10 tmap[t[i]]++;
11 }
12 for (auto const& [key, value] : smap) {
13 if (value ___ tmap[key]) {
14 return false;
15 }
16 }
17 return true;
18 }
19};