Group consecutive characters, then compare groups pairwise. A group stretches only if the count in s is or more.
function getGroups(str):
groups = []
i = 0
while i < str.length:
ch = str[i]
count = 0
while i < str.length and str[i] == ch:
count += 1
i += 1
groups.append((ch, count))
return groups
function expressiveWords(s, words):
sGroups = getGroups(s)
result = 0
for word in words:
wGroups = getGroups(word)
if len(wGroups) != len(sGroups):
continue
stretchy = true
for i in range(len(sGroups)):
sc, sn = sGroups[i]
wc, wn = wGroups[i]
if sc != wc:
stretchy = false
break
if sn != wn and sn < 3:
stretchy = false
break
if wn > sn:
stretchy = false
break
if stretchy:
result += 1
return result
time, space.