Mutating counts without cleanup
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
You are given a string s containing one or more words. Every consecutive pair of words is separated by a single space ' '.
A string t is an anagram of string s if the ith word of t is a permutation of the ith word of s.
"acb dfe" is an anagram of "abc def", but "def cab" and "adc bef" are not.Return the number of distinct anagrams of s. Since the answer may be very large, return it modulo 109 + 7.
Example 1:
Input: s = "too hot" Output: 18 Explanation: Some of the anagrams of the given string are "too hot", "oot hot", "oto toh", "too toh", and "too oht".
Example 2:
Input: s = "aa" Output: 1 Explanation: There is only one anagram possible for the given string.
Constraints:
1 <= s.length <= 105s consists of lowercase English letters and spaces ' '.Problem summary: You are given a string s containing one or more words. Every consecutive pair of words is separated by a single space ' '. A string t is an anagram of string s if the ith word of t is a permutation of the ith word of s. For example, "acb dfe" is an anagram of "abc def", but "def cab" and "adc bef" are not. Return the number of distinct anagrams of s. Since the answer may be very large, return it modulo 109 + 7.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map · Math
"too hot"
"aa"
group-anagrams)count-ways-to-build-rooms-in-an-ant-colony)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2514: Count Anagrams
import java.math.BigInteger;
class Solution {
private static final int MOD = (int) 1e9 + 7;
public int countAnagrams(String s) {
int n = s.length();
long[] f = new long[n + 1];
f[0] = 1;
for (int i = 1; i <= n; ++i) {
f[i] = f[i - 1] * i % MOD;
}
long p = 1;
for (String w : s.split(" ")) {
int[] cnt = new int[26];
for (int i = 0; i < w.length(); ++i) {
++cnt[w.charAt(i) - 'a'];
}
p = p * f[w.length()] % MOD;
for (int v : cnt) {
p = p * BigInteger.valueOf(f[v]).modInverse(BigInteger.valueOf(MOD)).intValue()
% MOD;
}
}
return (int) p;
}
}
// Accepted solution for LeetCode #2514: Count Anagrams
const mod int = 1e9 + 7
func countAnagrams(s string) int {
ans, mul := 1, 1
for _, w := range strings.Split(s, " ") {
cnt := [26]int{}
for i, c := range w {
i++
cnt[c-'a']++
ans = ans * i % mod
mul = mul * cnt[c-'a'] % mod
}
}
return ans * pow(mul, mod-2) % mod
}
func pow(x, n int) int {
res := 1
for ; n > 0; n >>= 1 {
if n&1 > 0 {
res = res * x % mod
}
x = x * x % mod
}
return res
}
# Accepted solution for LeetCode #2514: Count Anagrams
class Solution:
def countAnagrams(self, s: str) -> int:
mod = 10**9 + 7
ans = mul = 1
for w in s.split():
cnt = Counter()
for i, c in enumerate(w, 1):
cnt[c] += 1
mul = mul * cnt[c] % mod
ans = ans * i % mod
return ans * pow(mul, -1, mod) % mod
// Accepted solution for LeetCode #2514: Count Anagrams
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2514: Count Anagrams
// import java.math.BigInteger;
//
// class Solution {
// private static final int MOD = (int) 1e9 + 7;
//
// public int countAnagrams(String s) {
// int n = s.length();
// long[] f = new long[n + 1];
// f[0] = 1;
// for (int i = 1; i <= n; ++i) {
// f[i] = f[i - 1] * i % MOD;
// }
// long p = 1;
// for (String w : s.split(" ")) {
// int[] cnt = new int[26];
// for (int i = 0; i < w.length(); ++i) {
// ++cnt[w.charAt(i) - 'a'];
// }
// p = p * f[w.length()] % MOD;
// for (int v : cnt) {
// p = p * BigInteger.valueOf(f[v]).modInverse(BigInteger.valueOf(MOD)).intValue()
// % MOD;
// }
// }
// return (int) p;
// }
// }
// Accepted solution for LeetCode #2514: Count Anagrams
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #2514: Count Anagrams
// import java.math.BigInteger;
//
// class Solution {
// private static final int MOD = (int) 1e9 + 7;
//
// public int countAnagrams(String s) {
// int n = s.length();
// long[] f = new long[n + 1];
// f[0] = 1;
// for (int i = 1; i <= n; ++i) {
// f[i] = f[i - 1] * i % MOD;
// }
// long p = 1;
// for (String w : s.split(" ")) {
// int[] cnt = new int[26];
// for (int i = 0; i < w.length(); ++i) {
// ++cnt[w.charAt(i) - 'a'];
// }
// p = p * f[w.length()] % MOD;
// for (int v : cnt) {
// p = p * BigInteger.valueOf(f[v]).modInverse(BigInteger.valueOf(MOD)).intValue()
// % MOD;
// }
// }
// return (int) p;
// }
// }
Use this to step through a reusable interview workflow for this problem.
For each element, scan the rest of the array looking for a match. Two nested loops give n × (n−1)/2 comparisons = O(n²). No extra space since we only use loop indices.
One pass through the input, performing O(1) hash map lookups and insertions at each step. The hash map may store up to n entries in the worst case. This is the classic space-for-time tradeoff: O(n) extra memory eliminates an inner loop.
Review these before coding to avoid predictable interview regressions.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.