Expressive Words - Problem
Have you ever noticed how people stretch out letters when they're really excited? Like when someone types "hellooooo!" instead of just "hello", or "hiiiiii" instead of "hi"? That's exactly what this problem is about!
You're given a stretched string s and an array of normal words. Your task is to find how many of these normal words could have been stretched to create the string s.
The stretching rules are:
- You can take any group of consecutive identical letters in a word
- You can extend that group by adding more of the same letter
- But here's the catch: the final group must have at least 3 letters
Example: Starting with "hello", you could stretch the "o" to get "hellooo" (3 o's), or stretch the "ll" to get "hellllo" (4 l's), but you cannot get "helloo" because "oo" has only 2 letters.
Goal: Return the count of words that can be stretched to match the given string s.
Input & Output
example_1.py โ Basic Stretching
$
Input:
s = "heeellooo", words = ["hello", "hi", "helo"]
โบ
Output:
1
๐ก Note:
Only "hello" can be stretched to "heeellooo". We can stretch 'e' (1โ3), keep 'll' (2โ2), and stretch 'o' (1โ3). "hi" has different characters, and "helo" has only one 'l' which can't become "ll".
example_2.py โ No Valid Stretches
$
Input:
s = "zzzzzyyyyy", words = ["zy", "zyy"]
โบ
Output:
0
๐ก Note:
Neither word can be stretched. "zy" would need z (1โ5) and y (1โ5), but single characters can't be stretched to 5. "zyy" would need z (1โ5) which is impossible since 1 < 3.
example_3.py โ Multiple Valid Stretches
$
Input:
s = "abccc", words = ["abc", "abcc", "abccc"]
โบ
Output:
2
๐ก Note:
"abcc" (2โ3 c's, valid stretch) and "abccc" (already matches) are valid. "abc" can't work because 1 c can't become 3 c's (needs at least 3 to stretch).
Constraints
- 1 โค s.length, words[i].length โค 50
- 1 โค words.length โค 100
- s and words[i] consist of lowercase English letters only
- A group of characters can only be stretched if the result has at least 3 characters
- Groups with less than 3 characters must remain unchanged
Visualization
Tap to expand
Understanding the Visualization
1
Group Formation
Identify consecutive identical characters in both strings
2
Stretching Rules
Check if word groups can be stretched to match target groups
3
Validation
Ensure all groups match and both strings are fully processed
4
Count Results
Count how many words can be successfully stretched
Key Takeaway
๐ฏ Key Insight: Group consecutive identical characters and validate stretching rules - target groups must be at least as large as word groups, and groups smaller than 3 characters cannot be stretched.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code