Every Window of Length Three or More Must Be a Known Word
StringsAuto-graded · 24 test casesLast updated
Check that every contiguous substring of length at least 3 appears in a word list, testing set-based lookups, loop bounds and the vacuous case for short strings.
You are given a string s made of letters and a list of strings dictionary that holds the allowed words.
Decide whether every contiguous substring of s that is at least 3 characters long appears in dictionary. Return True if all of them do, and False if at least one is missing.
Rules
- A substring is a run of consecutive characters of
s. Characters cannot be skipped or reordered. - Every pair of positions
(i, j)with0 <= i <= j < len(s)andj - i + 1 >= 3defines a substring that must be checked. Two different positions that yield identical text still count as separate checks, but they need the same word. - Words are compared by exact string equality, so
"Cat"and"cat"are different. - If
shas fewer than 3 characters, no substring qualifies, so the answer isTruewhatever the dictionary contains, even if it is empty. - The dictionary may contain duplicates. They have no effect on the answer.
Constraints
scontains only letters; it may be empty.dictionarymay be empty and may contain repeated entries.- Matching is case-sensitive and exact.
- The input is small enough that checking all substrings of
sdirectly is acceptable. - Return a boolean.
Examples
Example 1
Input: every_long_window_is_word('cats', ['cat', 'ats', 'cats'])
Output: True
Example 2
Input: every_long_window_is_word('cats', ['cat', 'cats'])
Output: False
Example 3
Input: every_long_window_is_word('banana', ['ban', 'ana', 'nana', 'banan', 'anana', 'banana', 'nan'])
Output: False
Example 4
Input: every_long_window_is_word('ab', ['ab'])
Output: True
Hints
Hint 1
Turn the dictionary into a set once, so each membership test is a constant-time average lookup instead of a scan over the list.
Hint 2
Fix a starting index, then grow the end of the slice so the length is at least 3. Make sure the exclusive slice end can reach len(s).
Hint 3
You can stop at the first substring that is missing. If the string is shorter than 3, the loops never run and True is the correct answer.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The condition applies to all qualifying substrings, so the straightforward method is to enumerate them and test each one.
- Build a set from
dictionary. This makes each membership test O(1) on average, where scanning a list would cost time proportional to the dictionary size. - For every start index
startfrom0ton - 1, let the exclusive end index run fromstart + 3toninclusive. Each slices[start:end]has length at least 3. - If any slice is not in the set, return
Falseimmediately. - If every slice passes, return
True.
Why it is correct
- Completeness: each substring of length 3 or more corresponds to exactly one pair
(start, end)withend >= start + 3andend <= n. The two loops visit exactly those pairs, so nothing is skipped. - Soundness:
Falseis returned only after finding a specific qualifying substring that is not a word, which is a valid counterexample. - Short strings: when
n < 3, the inner range is empty for everystart. No checks happen and the function returnsTrue, which matches the "for all" reading over an empty collection. - Duplicates in the dictionary collapse in the set, and repeated substrings of
sare simply looked up again.
Complexity
Let n = len(s) and W be the total number of characters in the dictionary.
- Building the set takes O(W) time and O(W) space.
- There are O(n²) qualifying substrings. Creating and hashing each slice costs up to O(n), so enumeration is O(n³) in the worst case. Early exit makes failing inputs faster.
- Total time is O(W + n³). Extra space is O(W) for the set plus O(n) for a temporary slice.
Possible improvements
- If
n >= 3, the whole string is itself a qualifying substring, so a string longer than the longest dictionary word can be rejected right away. - A trie built from the dictionary lets you walk forward from each start index one character at a time, which removes the cost of slicing and hashing and gives O(n²) enumeration after the build.
Reference solution
from typing import List
def every_long_window_is_word(s: str, dictionary: List[str]) -> bool:
words = set(dictionary)
n = len(s)
for start in range(n):
for end in range(start + 3, n + 1):
if s[start:end] not in words:
return False
return True