Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LeetCode] 1023. Camelcase Matching #1023

Open
grandyang opened this issue May 30, 2019 · 0 comments
Open

[LeetCode] 1023. Camelcase Matching #1023

grandyang opened this issue May 30, 2019 · 0 comments

Comments

@grandyang
Copy link
Owner

grandyang commented May 30, 2019

A query word matches a given pattern if we can insert lowercase letters to the pattern word so that it equals the query. (We may insert each character at any position, and may insert 0 characters.)

Given a list of queries, and a pattern, return an answer list of booleans, where answer[i] is true if and only if queries[i] matches the pattern.

Example 1:

Input: queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FB"
Output: [true,false,true,true,false]
Explanation:
"FooBar" can be generated like this "F" + "oo" + "B" + "ar".
"FootBall" can be generated like this "F" + "oot" + "B" + "all".
"FrameBuffer" can be generated like this "F" + "rame" + "B" + "uffer".

Example 2:

Input: queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBa"
Output: [true,false,true,false,false]
Explanation:
"FooBar" can be generated like this "Fo" + "o" + "Ba" + "r".
"FootBall" can be generated like this "Fo" + "ot" + "Ba" + "ll".

Example 3:

Input: queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBaT"
Output: [false,true,false,false,false]
Explanation:
"FooBarTest" can be generated like this "Fo" + "o" + "Ba" + "r" + "T" + "est".

Note:

  1. 1 <= queries.length <= 100
  2. 1 <= queries[i].length <= 100
  3. 1 <= pattern.length <= 100
  4. All strings consists only of lower and upper case English letters.

这道题说是若要一个 query 单词可以匹配一个模式 pattern 串的话,需要在在 pattern 中加入若干小写字母使其和 query 单词完全相同,现在给了一个单词数组和一个 pattern 串,问数组中的每个 query 单词是否都可以成功匹配。根据题目中的给的例子可以分析出,pattern 串中是可以有小写字母的,但主要是要其中的每个大写字母能匹配上,所以核心是要匹配对应位置的大写字母。由于 query 单词之间没有什么联系,所以每个 query 单词和 pattern 串的匹配方式都是相同的。用一个变量i来指向当前检测到 pattern 串中的位置,用个布尔变量 valid 来标记当前 query 是否能匹配。遍历 query 单词中的字符,若 i 小于 pattern 的长度,且当前字符等于 pattern[i],则i自增1;否则若当前字符是个大写字符,则说明无法匹配,标记 valid 为 false,并 break 掉 for 循环。最后若 valid 为 true,且i正好等于n时,加入 true 到结果 res,否则加入 false,参见代码如下:

解法一:

class Solution {
public:
    vector<bool> camelMatch(vector<string>& queries, string pattern) {
        vector<bool> res;
        for (string query : queries) {
            int i = 0, n = pattern.size();
            bool valid = true;
            for (char c : query) {
                if (i < n && c == pattern[i]) {
                    ++i;
                } else if (c >= 'A' && c <= 'Z') {
                    valid = false;
                    break;
                }
            }
            res.push_back(valid && i == n);
        }
        return res;
    }
};

我们也可以将验证部分放入一个子函数中,整体结构更加清晰一些,但整体思路都是一样的,参见代码如下:

解法二:

class Solution {
public:
    vector<bool> camelMatch(vector<string>& queries, string pattern) {
        vector<bool> res;
        for (string query : queries) {
            res.push_back(helper(query, pattern));
        }
        return res;
    }
    bool helper(string query, string pattern) {
        int i = 0, n = pattern.size();
        for (char c : query) {
            if (i < n && c == pattern[i]) {
                ++i;
            } else if (c >= 'A' && c <= 'Z') {
                return false;
            }
        }
        return i == n;
    }
};

Github 同步地址:

#1023

参考资料:

https://leetcode.com/problems/camelcase-matching/

https://leetcode.com/problems/camelcase-matching/discuss/270006/Java-Easy-Two-Pointers

https://leetcode.com/problems/camelcase-matching/discuss/270029/JavaC%2B%2BPython-Check-Subsequence-and-Regax

LeetCode All in One 题目讲解汇总(持续更新中...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant