Problem Description
Given a string s and an array of query strings words, count the number of words that are "stretchy." A word is considered stretchy if it can be transformed into s by repeatedly extending groups of characters. In an extension operation, you can take a group of adjacent identical characters in the word and add more of the same character such that the group’s length becomes at least 3. The goal is to determine exactly how many words from the list can be transformed into s following the given extension rules.
Key Insights
- Both the target string s and each candidate word can be broken down into groups of repeated characters.
- Use a two-pointer technique to iterate over s and the query word simultaneously by comparing corresponding character groups.
- For each group, the characters must match. Let countS be the group length in s and countW be the group length in the word.
- It is valid if countS equals countW, or if countS is at least 3 and countS is greater than countW (allowing the extension).
- If any group does not satisfy the conditions, the word cannot be stretched to match s.
Space and Time Complexity
Time Complexity: O(N * L) where N is the number of words and L is the average length of a word (since each word is scanned in linear time relative to its length). Space Complexity: O(1) additional space, not counting the space used for the input.
Solution
The solution uses a two-pointer approach to simultaneously traverse the string s and the query word. For each corresponding group of consecutive characters, we count the lengths. We then verify the following:
- The characters in the current groups match.
- The group length in s must either be equal to the group length in the query word, or be greater or equal to 3 and at least one more than the query word group length to allow for the extension. If any group comparison fails, the word is not stretchy. We repeat this process for every word in the list and count those that satisfy the criteria.
Data structures used include simple variables for pointers and counters. The algorithmic approach leverages two-pointer iteration and in-place group counting without any extra storage, making it straightforward and efficient.