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

Constraints:

  • 0 <= len(S) <= 100.
  • 0 <= len(words) <= 100.
  • 0 <= len(words[i]) <= 100.
  • S and all words in words consist only of lowercase letters

题目链接:https://leetcode.com/problems/expressive-words/

思路:对于例子,

S = "heeellooo"
words = ["hello", "hi", "helo"]

先把S根据不同字母分成不同的放进一个vector<string>

判断条件:

1.str1.size()==str2.size()      2个字符串  分割成的 组数相同,也就是 按顺序的  不同字母一一对应的上。

2.str1[i][0]==str2[i][0]            保证每个小组的   字母  是    一样

3.str1[i].size()==str2.size()  ||  { str1[i].size()>str2[i].size()  &&   str1[i].size()>=3 }  每个小组长度一样,要么str1组更大于3

class Solution {
public:
    
    void fun(string str,vector<string>& vc)
    {
        if(str.size()>0)
        {
            // vc.swap(vector<string>());
            vc.clear();
            string tmp="";
            tmp+=str[0];
            for(int i=1;i<str.size();i++)
            {
                if(str[i]==str[i-1])
                {
                    tmp+=str[i];
                }
                else
                {
                    vc.push_back(tmp);
                    tmp=str[i];
                }
            }
            if(tmp!="")
            {
                vc.push_back(tmp);
            }
        }
    }
    int expressiveWords(string S, vector<string>& words) {
        int res=0;
        vector<string> str1,str2;
        fun(S,str1);
        for(int k=0;k<words.size();k++)
        {
            fun(words[k],str2);
            if(str1.size()!=str2.size())
            {
                continue;
            }
            for(int i=0;i<str1.size();i++)
            {
                int a=str1[i].size(),b=str2[i].size();
                // cout<<str1[i].size()<<" "<<str2[i].size()<<endl;
                // cout<<(str1[i].size()-str2[i].size())<<endl;
                // if(!(str1[i][0]==str2[i][0]&&(a==b||(a>=3&&(a>b)))))
                // if(!(str1[i][0]==str2[i][0]&&(a>=b||a>=3)))这个逻辑不对
                if((str1[i][0]==str2[i][0]&&(a==b||(a>=3&&(a>b)))))
                {
                    // cout<<"bad";
                    // break;,可以用
                    // continue;不能用continue,用了就不会计数了
                }
                else
                {
                    // cout<<words[k]<<endl;
                    break;
                }
                // else
                // {
                //     break;
                // }
                // if(str1[i][0]!=str2[i][0])
                // {
                //     cout<<"aaa";
                //     break;
                // }
                // if(a-b<2)
                // {
                //     cout<<"bbb";
                //     break;
                // }
                if(i==str1.size()-1)
                {
                    // cout<<"ccc";
                    res++;
                    // cout<<words[k]<<endl;
                }
                // if(str1[i][0]==str2[i][0])
                // {
                //     if(str1[i].size()==str2[i].size()||(str1[i].size()-str2[i].size()>=2))
                //     {
                //         res++;
                //     }
                // }
            }
            // res++;
            // cout<<words[i]<<endl;
        }
        return res;
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值