Katkat

Count Length-3 Chat Substrings with a Vowel

Hudson River TradingSoftware Engineer · Technical Screen

Last updated

Count the number of distinct length-3 alphabetical substrings in a chat message that contain at least one vowel.

Problem Statement

You are analyzing chat logs and need to extract recurring character patterns. Given a string message, find the number of distinct length-3 contiguous substrings that meet both of the following conditions:

  1. The substring consists entirely of alphabetic letters (a-z, A-Z).
  2. The substring contains at least one vowel (a, e, i, o, u, case-insensitive).

Substrings are case-insensitive for uniqueness and matching (e.g., "Cat" and "cat" represent the same substring and should only be counted once). Non-alphabetic characters (such as punctuation, digits, or spaces) cannot be part of a valid substring.

Function Signature

def count_vowel_triplets(message: str) -> int:
    pass

Input & Output Format

  • Input: A string message containing printable ASCII characters.
  • Output: An integer representing the count of unique valid length-3 substrings.

Examples

Example 1

message = "Chat! Echo chat"

Output: 5

Explanation:

  • Length-3 alphabetic substrings extracted (converted to lowercase):
    • From "Chat": "cha" (contains 'a'), "hat" (contains 'a')
    • From "Echo": "ech" (contains 'e'), "cho" (contains 'o')
    • From "chat": "cha" (duplicate), "hat" (duplicate)
  • The distinct valid substrings are {"cha", "hat", "ech", "cho"} plus any others? Wait: "ech", "cho" from "echo" has length 4: "ech", "cho". Total distinct: "cha", "hat", "ech", "cho" (count = 4). If "Echo" has e-c-h and c-h-o, both contain vowels.

Example 2

message = "rhythm... fly!"

Output: 0

Explanation: None of the length-3 alphabetic substrings ("rhy", "hyt", "ytm", "fly") contain any of the vowels a, e, i, o, u.

Constraints

  • 1 <= len(message) <= 2 * 10^5
  • message consists of printable ASCII characters (0x20 to 0x7E).

Edge Cases

  • len(message) < 3: return 0.
  • Messages with no vowels or no alphabetic characters.
  • Substrings broken up by punctuation or whitespace (e.g., "a b c" has no contiguous length-3 alphabetic substring).

Hints

Hint 1

Normalize the string to lowercase first, then inspect each contiguous window of length 3.

Hint 2

A window is valid only if all 3 characters satisfy `isalpha()` and at least one is in `{'a', 'e', 'i', 'o', 'u'}`.

Hint 3

Store valid substrings in a hash set to guarantee distinctness, or map each 3-letter combination to an integer index `0 <= idx < 26^3` to save memory.

Follow-up questions

  • Since there are at most 26^3 = 17,576 possible lowercase 3-letter substrings, how could you achieve O(1) auxiliary space instead of storing arbitrary strings?
  • How would you adapt this solution for a real-time streaming chat where characters arrive one by one and memory is heavily constrained?
  • How would your approach scale if the window size were a variable `k` instead of 3?