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

Valid Anagram - Fill the Blank (Java)

Complete the Java 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.length() != t.length()) return ___;".

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

HashMap & HashSet in Javaref

Java HashMap<K,V> provides O(1) average put/get/containsKey. HashSet<E> wraps HashMap internally. Use getOrDefault() to avoid null checks. For thread safety, use ConcurrentHashMap.

-HashMap<K,V> — put(), get(), containsKey() all O(1)
-HashSet<E> — add(), contains(), remove() O(1)
-getOrDefault(key, defaultValue) simplifies counting
-entrySet() to iterate key-value pairs
HashMap<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.getOrDefault("b", 0);  // 0

HashSet<Integer> set = new HashSet<>();
set.add(1);
set.contains(1);  // true
Official docs →
HashMap & HashSet in Javaref

Java HashMap<K,V> provides O(1) average put/get/containsKey. HashSet<E> wraps HashMap internally. Use getOrDefault() to avoid null checks. For thread safety, use ConcurrentHashMap.

-HashMap<K,V> — put(), get(), containsKey() all O(1)
-HashSet<E> — add(), contains(), remove() O(1)
-getOrDefault(key, defaultValue) simplifies counting
-entrySet() to iterate key-value pairs
HashMap<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.getOrDefault("b", 0);  // 0

HashSet<Integer> set = new HashSet<>();
set.add(1);
set.contains(1);  // 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

null
1
0
!=
true
false
charAt
1class Solution {
2 public boolean isAnagram(String s, String t) {
3 if (s.length() != t.length()) return ___;
4 int[] store = new int[26];
5 for (int i = ___; i < s.length(); i++) {
6 store[s.___(i) - 'a']++;
7 store[t.charAt(i) - 'a']--;
8 }
9 for (int n : store) if (n ___ 0) return false;
10 return true;
11 }
12}