Expressive Words

Problem

Sometimes people repeat letters to represent extra feeling, such as “hello” -> “heeellooo”, “hi” -> “hiiii”. Here, we have groups, of adjacent letters that are all the same character, and adjacent characters to the group are different. A group is extended if that group is length 3 or more, so “e” and “o” would be extended in the first example, and “i” would be extended in the second example. As another example, the groups of “abbcccaaaa” would be “a”, “bb”, “ccc”, and “aaaa”; and “ccc” and “aaaa” are the extended groups of that string.

For some given string S, a query word is stretchy if it can be made to be equal to S by extending some groups. Formally, we are allowed to repeatedly choose a group (as defined above) of characters c, and add some number of the same character c to it so that the length of the group is 3 or more. Note that we cannot extend a group of size one like “h” to a group of size two like “hh” - all extensions must leave the group extended - ie., at least 3 characters long.

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 extended.

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

Analysis

We generate a mapping of character to counts and compare them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Solution {
using counts_t = pair<char, int>;
using counts_vec_t = vector<counts_t>;

void gen_counts(const string& s, counts_vec_t& v) {
v.push_back({s[0], 1});
char last = s[0];
for(int i = 0; i < (int) s.size(); i++) {
if (last == s[i]) {
v.back().second++;
}
else {
v.push_back({s[i], 1});
last = s[i];
}
}
}
public:
int expressiveWords(string s, vector<string>& words) {
counts_vec_t vec;
gen_counts(s, vec);
int matched_count = 0;
for (auto w: words) {
counts_vec_t wc;
gen_counts(w, wc);
bool matched = false;
for (int i = 0; i < vec.size(); i++) {
if (i >= wc.size()) break;
if (vec[i].first != wc[i].first) break;
if (vec[i].second >= 3) matched = true;
else if (vec[i].second == wc[i].second) matched = true;
}
if (matched) matched_count++;
}
return matched_count;
}
};

Accepted

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Solution {
using counts_t = pair<char, int>;
using counts_vec_t = vector<counts_t>;

void gen_counts(const string& s, counts_vec_t& v) {
v.push_back({s[0], 1});
char last = s[0];
for(int i = 1; i < (int) s.size(); i++) {
if (last == s[i]) {
v.back().second++;
}
else {
v.push_back({s[i], 1});
last = s[i];
}
}
}
public:
int expressiveWords(string s, vector<string>& words) {
counts_vec_t vec;
gen_counts(s, vec);
int matched_count = 0;
for (auto w: words) {
counts_vec_t wc;
gen_counts(w, wc);

bool matched = true;
for (int i = 0; i < vec.size(); i++) {
if ((i > wc.size()) ||
//cout << wc[i].first << ", " << wc[i].second << endl;
(vec[i].first != wc[i].first) ||

(vec[i].second < wc[i].second) ||
(wc[i].second == 1 && vec[i].second == 2) )
{ matched = false; break; }
}
if (matched) {
//cout << w << " matched\n";
matched_count++;
}
}
return matched_count;
}
};