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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code