##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/809.html

# 809. Expressive Words (Medium)

Sometimes people repeat letters to represent extra feeling, such as "hello" -> "heeellooo", "hi" -> "hiiii".  In these strings like "heeellooo", we have groups of adjacent letters that are all the same:  "h", "eee", "ll", "ooo".

For some given string S, a query word is stretchy if it can be made to be equal to S by any number of applications of the following extension operation: choose a group consisting of characters c, and add some number of characters c to the group so that the size of the group is 3 or more.

For example, starting with "hello", we could do an extension on the group "o" to get "hellooo", but we cannot get "helloo" since the group "oo" has size less than 3.  Also, we could do another extension like "ll" -> "lllll" to get "helllllooo".  If S = "helllllooo", then the query word "hello" would be stretchy because of these two extension operations: query = "hello" -> "hellooo" -> "helllllooo" = S.

Given a list of query words, return the number of words that are stretchy.

Example:
Input:
S = "heeellooo"
words = ["hello", "hi", "helo"]
Output: 1
Explanation:
We can extend "e" and "o" in the word "hello" to get "heeellooo".
We can't extend "helo" to get "heeellooo" because the group "ll" is not size 3 or more.


Notes:

• 0 <= len(S) <= 100.
• 0 <= len(words) <= 100.
• 0 <= len(words[i]) <= 100.
• S and all words in words consist only of lowercase letters

Companies:

Related Topics:
String

## Solution 1.

• class Solution {
public int expressiveWords(String S, String[] words) {
LetterCount[] letterCounts = getLetterCounts(S);
int length = letterCounts.length;
int expressiveWordsCount = 0;
for (String word : words) {
LetterCount[] curLetterCounts = getLetterCounts(word);
if (curLetterCounts.length == length) {
boolean flag = true;
for (int i = 0; i < length; i++) {
LetterCount letterCount1 = letterCounts[i];
LetterCount letterCount2 = curLetterCounts[i];
if (letterCount1.letter == letterCount2.letter) {
if (letterCount1.count < letterCount2.count || letterCount1.count > letterCount2.count && letterCount1.count < 3) {
flag = false;
break;
}
} else {
flag = false;
break;
}
}
if (flag)
expressiveWordsCount++;
}
}
return expressiveWordsCount;
}

public LetterCount[] getLetterCounts(String str) {
List<LetterCount> list = new ArrayList<LetterCount>();
char prevC = 0;
int curCount = 0;
int length = str.length();
for (int i = 0; i < length; i++) {
char c = str.charAt(i);
if (prevC == c)
curCount++;
else {
if (prevC != 0) {
LetterCount letterCount = new LetterCount(prevC, curCount);
}
curCount = 1;
}
prevC = c;
}
if (curCount > 0) {
LetterCount letterCount = new LetterCount(prevC, curCount);
}
int size = list.size();
LetterCount[] letterCounts = new LetterCount[size];
for (int i = 0; i < size; i++)
letterCounts[i] = list.get(i);
return letterCounts;
}
}

class LetterCount {
char letter;
int count;

public LetterCount(char letter, int count) {
this.letter = letter;
this.count = count;
}

public String toString() {
return "[" + letter + ", " + count + "]";
}
}

############

class Solution {
public int expressiveWords(String s, String[] words) {
int ans = 0;
for (String t : words) {
if (check(s, t)) {
++ans;
}
}
return ans;
}

private boolean check(String s, String t) {
int m = s.length(), n = t.length();
if (n > m) {
return false;
}
int i = 0, j = 0;
while (i < m && j < n) {
if (s.charAt(i) != t.charAt(j)) {
return false;
}
int k = i;
while (k < m && s.charAt(k) == s.charAt(i)) {
++k;
}
int c1 = k - i;
i = k;
k = j;
while (k < n && t.charAt(k) == t.charAt(j)) {
++k;
}
int c2 = k - j;
j = k;
if (c1 < c2 || (c1 < 3 && c1 != c2)) {
return false;
}
}
return i == m && j == n;
}
}

• class Solution:
def expressiveWords(self, s: str, words: List[str]) -> int:
def check(s, t):
m, n = len(s), len(t)
if n > m:
return False
i = j = 0
while i < m and j < n:
if s[i] != t[j]:
return False
k = i
while k < m and s[k] == s[i]:
k += 1
c1 = k - i
i, k = k, j
while k < n and t[k] == t[j]:
k += 1
c2 = k - j
j = k
if c1 < c2 or (c1 < 3 and c1 != c2):
return False
return i == m and j == n

return sum(check(s, t) for t in words)

############

class Solution(object):
def expressiveWords(self, S, words):
"""
:type S: str
:type words: List[str]
:rtype: int
"""
ans = 0
set_S = set(S)
S_list = []
pre_s, pre_index = S[0], 0
for i, s in enumerate(S):
if pre_s != s:
S_list.append(S[pre_index:i])
pre_s, pre_index = s, i
if i == len(S) - 1:
S_list.append(S[pre_index:])
for word in words:
if set(word) != set_S:
continue
word_list = []
pre_w, pre_index = word[0], 0
for i, w in enumerate(word):
if pre_w != w:
word_list.append(word[pre_index:i])
pre_w, pre_index = w, i
if i == len(word) - 1:
word_list.append(word[pre_index:])
if len(S_list) == len(word_list):
if all(S_list[i] == word_list[i] if len(S_list[i]) < 3 else len(S_list[i]) >= len(word_list[i]) for i in range(len(S_list))):
ans += 1
return ans

• func expressiveWords(s string, words []string) (ans int) {
check := func(s, t string) bool {
m, n := len(s), len(t)
if n > m {
return false
}
i, j := 0, 0
for i < m && j < n {
if s[i] != t[j] {
return false
}
k := i
for k < m && s[k] == s[i] {
k++
}
c1 := k - i
i, k = k, j
for k < n && t[k] == t[j] {
k++
}
c2 := k - j
j = k
if c1 < c2 || (c1 != c2 && c1 < 3) {
return false
}
}
return i == m && j == n
}
for _, t := range words {
if check(s, t) {
ans++
}
}
return ans
}

• class Solution {
public:
int expressiveWords(string s, vector<string>& words) {
auto check = [](string& s, string& t) -> int {
int m = s.size(), n = t.size();
if (n > m) return 0;
int i = 0, j = 0;
while (i < m && j < n) {
if (s[i] != t[j]) return 0;
int k = i;
while (k < m && s[k] == s[i]) ++k;
int c1 = k - i;
i = k, k = j;
while (k < n && t[k] == t[j]) ++k;
int c2 = k - j;
j = k;
if (c1 < c2 || (c1 < 3 && c1 != c2)) return 0;
}
return i == m && j == n;
};

int ans = 0;
for (string& t : words) ans += check(s, t);
return ans;
}
};