Check if all substrings are anagrams of words
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: This question evaluates string-processing skills and combinatorial reasoning about character multisets, specifically anagram recognition, substring enumeration, and the use of efficient representations for repeated checks.
Read the full Google Software Engineer interview experience this question came from
Constraints
- 0 <= len(s) <= 300
- 0 <= len(words) <= 5000
- s and every word in words contain only lowercase English letters 'a' to 'z'
- The total number of characters across all dictionary words is at most 200000
Examples
Input: ("act", ["cat", "dog"])
Expected Output: True
Explanation: The only substring of length at least 3 is "act", which is an anagram of "cat".
Input: ("abca", ["cab", "abca", "zzz"])
Expected Output: True
Explanation: Length-3 substrings are "abc" and "bca", both anagrams of "cab". The full substring "abca" is already in the dictionary.
Hints
- Two strings are anagrams exactly when their 26-letter frequency counts are the same.
- Group dictionary words by length, then use a sliding window over `s` for each possible substring length to compare frequency signatures efficiently.