Expressive Words
We need to first identify the groups of consecutive letters in the string s
. We also need to identify the count of each group. After that, for each word in the words
list, we’ll identify its groups and their counts and compare them with those of s
. A word is stretchy if:
- Both the word and
s
have the same groups of consecutive letters. - For each group, if the count in
s
is 3 or more, the count in the word should be less than or equal to the count ins
. If the count ins
is less than 3, the count in the word should be the same as the count ins
.
Here’s the step-by-step solution:
- Write a helper function
getGroups
that, given a string, returns the groups of consecutive letters and their counts. - Use the helper function to get the groups and counts of
s
. - For each word in the
words
list, use the helper function to get its groups and counts. Then, compare them with the groups and counts ofs
to determine if the word is stretchy. - Return the total count of stretchy words.
Here’s the code:
|
|
This solution first identifies the groups and their counts in s
and then does the same for each word. The comparison to check if a word is stretchy is based on the identified groups and counts.