糖逗 发表于 2021-1-26 11:49:51

C++刷LeetCode(1023. 驼峰式匹配)【编程逻辑】

题目描述:
如果我们可以将小写字母插入模式串 pattern 得到待查询项 query,那么待查询项与给定模式串匹配。(我们可以在任何位置插入每个字符,也可以插入 0 个字符。)

给定待查询列表 queries,和模式串 pattern,返回由布尔值组成的答案列表 answer。只有在待查项 queries 与模式串 pattern 匹配时, answer 才为 true,否则为 false。

 

示例 1:

输入:queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FB"
输出:
示例:
"FooBar" 可以这样生成:"F" + "oo" + "B" + "ar"。
"FootBall" 可以这样生成:"F" + "oot" + "B" + "all".
"FrameBuffer" 可以这样生成:"F" + "rame" + "B" + "uffer".
示例 2:

输入:queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBa"
输出:
解释:
"FooBar" 可以这样生成:"Fo" + "o" + "Ba" + "r".
"FootBall" 可以这样生成:"Fo" + "ot" + "Ba" + "ll".
示例 3:

输出:queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBaT"
输入:
解释:
"FooBarTest" 可以这样生成:"Fo" + "o" + "Ba" + "r" + "T" + "est".
 

提示:

1 <= queries.length <= 100
1 <= queries.length <= 100
1 <= pattern.length <= 100
所有字符串都仅由大写和小写英文字母组成。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/camelcase-matching
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。


class Solution {
public:
    bool solution(const string& temp1, const string & temp2) {
      int i = 0, j = 0;
      int len1 = temp1.size(),len2 = temp2.size();
      while(i < len1) {
            if(j < len2 && temp1 == temp2) {
                i++;
                j++;
            }else if(isupper(temp1)) {
                return false;
            }else{
                i++;
            }
      }
      return i == len1 && j == len2;
    }
    vector<bool> camelMatch(vector<string>& queries, string pattern) {
      vector<bool> res;
      for(auto& cha : queries){
            if(solution(cha, pattern))res.push_back(true);
            else{
                res.push_back(false);
            }
      }
      return res;
    }
};


参考链接:https://leetcode-cn.com/problems/camelcase-matching/solution/zhong-gui-zhong-ju-kuo-zhan-de-zi-fu-chu-nyvs/

天下有雪 发表于 2021-1-26 12:10:36

学习了。

糖逗 发表于 2021-1-26 12:20:16

{:10_255:}
页: [1]
查看完整版本: C++刷LeetCode(1023. 驼峰式匹配)【编程逻辑】