KatInterview

Every Window of Length Three or More Must Be a Known Word

StringsAuto-graded · 24 test cases
MediumAuto-gradedStringsstringshash-setsubstrings

Last 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) with 0 <= i <= j < len(s) and j - i + 1 >= 3 defines 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 s has fewer than 3 characters, no substring qualifies, so the answer is True whatever the dictionary contains, even if it is empty.
  • The dictionary may contain duplicates. They have no effect on the answer.

Constraints

  • s contains only letters; it may be empty.
  • dictionary may be empty and may contain repeated entries.
  • Matching is case-sensitive and exact.
  • The input is small enough that checking all substrings of s directly 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.