zoukankan      html  css  js  c++  java
  • Weekly Contest 78-------->809. Expressive Words (stack)

    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

    Approach #1: C++.

    class Solution {
    public:
        int expressiveWords(string S, vector<string>& words) {
            int ans = 0;
            stack<char> stk1;
            for (int i = 0; i < S.length(); ++i)
                stk1.push(S[i]);
            for (int i = 0; i < words.size(); ++i) {
                stack<char> stk2;
                for (int j = 0; j < words[i].length(); ++j) 
                    stk2.push(words[i][j]);
                ans += helper(stk1, stk2);
            }
            return ans;
        }
    
    private:
        int helper(stack<char> stk1, stack<char> stk2) {
            int t1, t2;
            if (stk1.size() < stk2.size()) return 0;
            while (!stk1.empty() && !stk2.empty()) {
                t1 = 0, t2 = 0;
                char c1 = stk1.top();
                char c2 = stk2.top();
                stk1.pop(), stk2.pop();
                if (c1 != c2) return 0;
                while (!stk1.empty() && stk1.top() == c2) {
                    t1++;
                    stk1.pop();
                }
                while (!stk2.empty() && stk2.top() == c1) {
                    t2++;
                    stk2.pop();
                }
                if (t1 == t2 || t1 >= 2) continue;
                else return 0;
            }
            if (stk2.empty())
                return 1;      
        }
    };
    

      

    By this problem I learned the importance of appropriate data structure.

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    简化SpringBoot框架打包体积
    深究1.8版本HashMap源码
    一次面试题,将 字符串 保存在 Byte 数组中
    记一次linux磁盘清理
    Alibaba Cloud Toolkit 使用心得(IDEA版)
    Mybatis 一对多分页踩坑 对collection的分析
    MySQL中update修改数据与原数据相同会再次执行吗?
    与 MySQL 因“CST” 时区协商误解导致时间差了13 小时
    Druid数据源
    mybatis自动生成代码 mybatis-generator
  • 原文地址:https://www.cnblogs.com/h-hkai/p/9973462.html
Copyright © 2011-2022 走看看