[Leetcode] 809. Expressive Words 解题报告

题目

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

思路

这道题目的关键是比较words里面的每个单词word是不是相对于S来说是Stretchy的。为此我们设计函数makeHash,将每个字符串转换为一个由pair构成的向量,其中pair的第一个元素表示字符,第二个元素表示该元素出现的次数。这样问题就转换为两个vector<pair<char, int>>类型的hash之间的比较了,我在isStretchy函数中给出了详细的注释。

代码

class Solution {
public:
    int expressiveWords(string S, vector<string>& words) {
        if (S.length() == 0) {
            return 0;
        }
        vector<pair<char, int>> hash, word_hash;    // construct the hash of S for easier access
        makeHash(S, hash);
        int ret = 0;
        for (auto &word : words) {
            word_hash.clear();
            makeHash(word, word_hash);
            ret += isStretchy(hash, word_hash);
        }
        return ret;
    }
private:
    void makeHash(const string &s, vector<pair<char, int>> &hash) {
        hash.push_back(make_pair(s[0], 1));
        for (int i = 1; i < s.length(); ++i) {
            if (s[i] == hash.back().first) {
                ++hash.back().second;
            }
            else {
                hash.push_back(make_pair(s[i], 1));
            }
        }
    }
    bool isStretchy(const vector<pair<char, int>> &hash, const vector<pair<char, int>> &word_hash) {
        if (hash.size() != word_hash.size()) {              // if they have different size, definitely false
            return false;
        }
        for (int i = 0; i < hash.size(); ++i) {
            if (hash[i].first != word_hash[i].first) {      // do not have the same char, definitely false
                return false;
            }
            if (hash[i].second != word_hash[i].second) {    // take "heeellooo" <- "helo" and "aaa" <- "aaaa" as example
                if (hash[i].second <= 2 || hash[i].second < word_hash[i].second) {
                    return false;
                }
            }
        }
        return true;
    }
};


猜你喜欢

转载自blog.csdn.net/magicbean2/article/details/79861023