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 - String Stretching INPUT Target String s: "heeellooo" Character Groups: h 1 eee 3 ll 2 ooo 3 Query Words: "hello" "hi" "helo" s = "heeellooo" words = ["hello", "hi", "helo"] ALGORITHM STEPS 1 Parse Groups Extract char groups + counts 2 Compare Each Word Match groups with target s 3 Check Stretchy Rules Count(s) >= Count(word) 4 Extend Rule If extending, need >= 3 chars Word Comparison Results "hello" h:1 e:1 l:2 o:1 OK "hi" h:1 i:1 (diff chars) NO "helo" l:1 but s has l:2 NO 2 < 3, can't extend l group Only "hello" is stretchy! FINAL RESULT Stretchy Word Found: "hello" --> stretch --> "heeellooo" Extension Details: h(1) --> h(1): same e(1) --> eee(3): extended! ll(2) --> ll(2): same o(1) --> ooo(3): extended! Output: 1 1 word is stretchy ("hello" can become "heeellooo") Key Insight: A word is "stretchy" if each character group can match the target. Two rules apply: 1) Same count: group sizes match exactly. 2) Extension: target group >= 3 AND >= word group. Groups with count < 3 in target cannot be extended (like "ll" with count 2), so word must match exactly. TutorialsPoint - Expressive Words | Optimal Two-Pointer Solution
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