Expressive Words - Problem

Sometimes people repeat letters to represent extra feeling. For example:

  • "hello""heeellooo"
  • "hi""hiiii"

In strings like "heeellooo", we have groups of adjacent letters that are all the same: "h", "eee", "ll", "ooo".

You are given a string s and an array of query strings words. A query word is stretchy if it can be made equal to s by extending character groups to have 3 or more characters.

For example, starting with "hello", we could extend the group "o" to get "hellooo", but we cannot get "helloo" since the group "oo" has size less than 3. We could also extend "ll""lllll" to get "helllllooo".

Return the number of query strings that are stretchy.

Input & Output

Example 1 — Basic Stretching
$ Input: s = "heeellooo", words = ["hello", "hi", "helo"]
Output: 1
💡 Note: "hello" can be stretched: h(1→1), e(1→3), l(2→2), o(1→3). "hi" fails because 'h' matches but 'i' doesn't match 'e'. "helo" fails because it's missing one 'l'.
Example 2 — No Valid Stretches
$ Input: s = "zzzzzyyyyy", words = ["zzyy", "zy", "zyy"]
Output: 3
💡 Note: "zzyy": z(2→5)✓, y(2→5)✓. "zy": z(1→5)✓, y(1→5)✓. "zyy": z(1→5)✓, y(2→5)✓. All can be stretched to match the target.
Example 3 — Edge Case Single Character
$ Input: s = "aaa", words = ["a", "aa", "aaa"]
Output: 2
💡 Note: "a" can be stretched to "aaa" (1→3). "aa" cannot be stretched because 2→3 requires at least 3 in target for extension. "aaa" matches exactly.

Constraints

  • 1 ≤ s.length, words[i].length ≤ 50
  • 1 ≤ words.length ≤ 50
  • s and words[i] consist of lowercase English letters

Visualization

Tap to expand
Expressive Words: Count Stretchy WordsTarget: "heeellooo"heeelloooQuery Words:"hello" → Stretchy ✓"hi" → Not stretchy ✗"helo" → Not stretchy ✗Stretching Rules:• Groups can be extended to 3+• Cannot shrink groups• Character order must matchOutput: 1 (only "hello" is stretchy)
Understanding the Visualization
1
Input
Target string and array of query words
2
Process
Check each word to see if it can be stretched to match target
3
Output
Count of words that can be stretched
Key Takeaway
🎯 Key Insight: Compare character groups rather than individual characters, checking if groups can be stretched (extended to 3+ chars)
Asked in
Google 42 Facebook 28
28.5K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen