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.

// OJ: https://leetcode.com/problems/expressive-words/

// Time: O(SW) where S is size of string S, W is count of words
// Space: O(S)
class Solution {
private:
vector<pair<char, int>> s;
vector<pair<char, int>> get(string &s) {
vector<pair<char, int>> v;
for (int i = 0, N = s.size(); i < N;) {
int start = i;
while (i < N && s[i] == s[start]) ++i;
v.emplace_back(s[start], i - start);
}
return v;
}
bool isExpressiveWord(string &word) {
auto w = get(word);
if (s.size() != w.size()) return false;
for (int i = 0; i < s.size(); ++i) {
if (s[i].first != w[i].first
|| !((s[i].second >= 3 && w[i].second <= s[i].second) || (s[i].second < 3 && w[i].second == s[i].second))) return false;
}
return true;
}
public:
int expressiveWords(string S, vector<string>& words) {
s = get(S);
int ans = 0;
for (auto &word : words) {
if (isExpressiveWord(word)) ++ans;
}
return ans;
}
};


Java

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 + "]";
}
}