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
Expressive Words: String StretchingTarget: "heeellooo" โ† Can "hello" be stretched to match this?Word Analysis: "hello"h (1)e (1)ll (2)o (1)h (1)eee (3)ll (2)ooo (3)Original:Target:โœ“1โ†’1โœ“1โ†’3Result: "hello" CAN be stretched to "heeellooo" โœ“Key: Groups with <3 chars must match exactly, groups โ‰ฅ3 can be stretched
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.
Asked in
Google 45 Amazon 32 Meta 28 Microsoft 18
23.5K Views
Medium-High Frequency
~18 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