Skip to content
Problem 809 · LeetCode 809(opens in a new tab)

Expressive Words

MediumStrings

When people type with feeling, they hold down letters: "ok" turns into "okkk". Split a string into groups, the longest runs of one repeated letter. "yeeesss" has the groups "y", "eee" and "sss".

A word is stretchy for a string s if you can turn the word into s by stretching groups. One stretch takes a group and adds more of the same letter so that the group ends up with at least 3 letters. You may stretch any number of groups, but never remove letters.

Given s and a list words, return how many of the words are stretchy.

Example 1

Input:  s = "goood", words = ["god","good","gooood"]
Output: 2
Why:    "o" and "oo" can grow to "ooo"; "oooo" cannot shrink
2 more examples and the constraints

Example 2

Input:  s = "yeeesss", words = ["yes","yees","yess","ye"]
Output: 3
Why:    "ye" has no s group to stretch

Example 3

Input:  s = "hoot", words = ["hot","hoot"]
Output: 1
Why:    "oo" has only 2 letters, so "o" cannot be stretched into it

Constraints

  • 1 ≤ s.length ≤ 100
  • 1 ≤ words.length ≤ 100
  • Each word has 1 to 100 letters
  • All strings use only lowercase English letters

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Compare group listsO(L)
  2. Two pointers per wordO(L)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeLetter reads for 100 words of 100 lettersVerdict
Compare group listsO(L)
Passes; builds a list per word
Two pointers per wordO(L)
Best; no extra storage

L = the total length of all words plus s once per word; n = the longest string.

Edge cases to test

  • Groups of 1 or 2 in s. They must match the word's group size exactly; they are too short to be stretched.
  • A longer group in the word. Stretching only adds letters, so a word group bigger than s's group fails.
  • Different group counts. A missing or extra group, as in "ye" against "yeeesss", always fails.
  • A word equal to s. Zero stretches is allowed, so it counts.

Common mistakes

  • Allowing a group of 2 in s to come from a group of 1 in the word.
  • Forgetting to check that both pointers reach the end, which accepts words missing a final group.
  • Comparing single letters instead of whole runs, so "hoot" vs "hot" looks fine.

How to talk through it

  1. 1Clarify. Can a stretch ever remove letters? Does an unchanged word count?
  2. 2Think in groups. Stretching acts on runs of one letter, so compare run by run.
  3. 3State the rule. Same letter, and the sizes are equal or s's size is at least 3 and at least the word's.
  4. 4Use two pointers. Skip each run in both strings at once; no extra lists, O(L) time and O(1) space.
  5. 5Test out loud. Try "hoot" with "hot", and a word whose group is longer than s's.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?