Count query words that can be stretched into a target string

Quick Overview

Count how many query words can be turned into a given string by stretching runs of repeated letters to length three or more. The problem tests run-length comparison of two strings, careful group size rules and handling the ends of both strings.

Count query words that can be stretched into a target string

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Sometimes people repeat letters to show extra feeling. For example, `"heeellooo"` might be a stretchy version of `"hello"`. A group is a maximal run of identical consecutive characters. Starting from a query word, an extension picks one group and adds more copies of its character so that the group ends up with size 3 or more. A query word is stretchy if some sequence of extensions, each applied to any group, turns it into the given string `s`. For example, starting with `"hello"`, extending the group `"e"` and the group `"o"` gives `"heeellooo"`. Also extending `"ll"` to `"llll"` gives `"heeellllooo"`, which is allowed because that group's final size, 4, is at least 3. `"helloo"` cannot be reached from `"hello"`, because growing `"o"` to `"oo"` leaves the group at size 2. Given a string `s` and an array of query words `words`, return the number of query words that are stretchy. ### Function Signature ```python def expressive_words(s: str, words: list[str]) -> int: ``` ### Rules - Zero extensions are allowed, so a word equal to `s` is stretchy. - An extension only adds characters to an existing group. It never removes characters and never creates a new group. - Each entry of `words` is checked on its own, so a stretchy word that appears twice in `words` counts twice. ### Constraints - `1 <= len(s) <= 100` - `1 <= len(words) <= 100` - `1 <= len(words[i]) <= 100` - `s` and `words[i]` consist of lowercase English letters. ### Examples ```text Input: s = "heeellooo", words = ["hello", "hi", "helo"] Output: 1 ``` `"hello"` reaches `s` by extending `e` and `o` to size 3 each. `"hi"` has `i` where `s` has `e`, and `"helo"` would need its single `l` grown to 2, which is below 3, so only one word counts. ```text Input: s = "zzzzzyyyyy", words = ["zzyy", "zy", "zzz"] Output: 2 ``` `"zzyy"` and `"zy"` both reach `s` by growing each group to size 5. `"zzz"` has no `y` group, and an extension cannot add one. ```text Input: s = "heeellllooo", words = ["hello", "hhello", "heeellllooo"] Output: 2 ``` `"hello"` reaches `s` by growing `e` to 3, `ll` to 4 and `o` to 3, and the word equal to `s` needs no extension. `"hhello"` starts with two `h` characters while `s` has one, and extensions never remove characters.

Overview: Count how many query words can be turned into a given string by stretching runs of repeated letters to length three or more. The problem tests run-length comparison of two strings, careful group size rules and handling the ends of both strings.

Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Sometimes people repeat letters to show extra feeling. For example, "heeellooo" might be a stretchy version of "hello".

A group is a maximal run of identical consecutive characters. Starting from a query word, an extension picks one group and adds more copies of its character so that the group ends up with size 3 or more. A query word is stretchy if some sequence of extensions, each applied to any group, turns it into the given string s.

For example, starting with "hello", extending the group "e" and the group "o" gives "heeellooo". Also extending "ll" to "llll" gives "heeellllooo", which is allowed because that group's final size, 4, is at least 3. "helloo" cannot be reached from "hello", because growing "o" to "oo" leaves the group at size 2.

Given a string s and an array of query words words, return the number of query words that are stretchy.

Function Signature

def expressive_words(s: str, words: list[str]) -> int:

Rules

  • Zero extensions are allowed, so a word equal to s is stretchy.
  • An extension only adds characters to an existing group. It never removes characters and never creates a new group.
  • Each entry of words is checked on its own, so a stretchy word that appears twice in words counts twice.

Constraints

  • 1 <= len(s) <= 100
  • 1 <= len(words) <= 100
  • 1 <= len(words[i]) <= 100
  • s and words[i] consist of lowercase English letters.

Examples

Input: s = "heeellooo", words = ["hello", "hi", "helo"]
Output: 1

"hello" reaches s by extending e and o to size 3 each. "hi" has i where s has e, and "helo" would need its single l grown to 2, which is below 3, so only one word counts.

Input: s = "zzzzzyyyyy", words = ["zzyy", "zy", "zzz"]
Output: 2

"zzyy" and "zy" both reach s by growing each group to size 5. "zzz" has no y group, and an extension cannot add one.

Input: s = "heeellllooo", words = ["hello", "hhello", "heeellllooo"]
Output: 2

"hello" reaches s by growing e to 3, ll to 4 and o to 3, and the word equal to s needs no extension. "hhello" starts with two h characters while s has one, and extensions never remove characters.