Thursday, March 19, 2020

Expressive Words

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


class Solution {
public:
    int expressiveWords(string S, vector<string>& words) {
        int ans = 0;
        
        for (auto word : words) {
            if (stretch(S, word)) {
                ans++;
            }
        }
        return ans;
    }
    
    bool stretch(string S, string word) {
        int len_a = S.size();
        int len_b = word.size();
        
        int i = 1;
        int j = 1;
        while (1) {
            if (S[i - 1] != word[j - 1]) {
                return false;
            }
            if (i == len_a && j == len_b) {
                break;
            }
            int char_len_a = 1;
            while (i < len_a) {
                if (S[i] == S[i - 1]) {
                   char_len_a++;
                    i++;
                } else {
                    i++;
                    break;
                }
            }
            int char_len_b = 1;
            while (j < len_b) {
                if (word[j] == word[j - 1]) {
                    char_len_b++;
                    j++;
                } else {
                    j++;
                    break;
                }
            }
            if (char_len_a < char_len_b || (char_len_a == 2 && char_len_b !=2)) {
                return false;
            }
        }
        return true;
    }
};


========= Readable ======
int expressiveWords(string S, vector<string>& words) { int result = 0; for (string w : words) { int ptr1 = 0, ptr2 = 0; while (ptr1 < S.size() && ptr2 < w.size()) { char c = w[ptr2]; int cnt = 0; while(ptr2 < w.size() && w[ptr2] == c) { ++ptr2; ++cnt; } if (S[ptr1] != c) break; int cnt2 = 0; while (ptr1 < S.size() && S[ptr1] == c) { ++ptr1; ++cnt2; } if (cnt2 != cnt && (cnt2 < cnt || cnt2 < 3)) break; if (ptr1 == S.size() && ptr2 == w.size()) ++result; } } return result; }

No comments:

Post a Comment