列表

详情


1023. 驼峰式匹配

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

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

 

示例 1:

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

示例 2:

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

示例 3:

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

 

提示:

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

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
class Solution { public: vector<bool> camelMatch(vector<string>& queries, string pattern) { } };

python3 解法, 执行用时: 52 ms, 内存消耗: 15.2 MB, 提交时间: 2022-12-08 10:14:16

class Solution:
    def camelMatch(self, queries: List[str], pattern: str) -> List[bool]:
        r = re.compile(f'{"[a-z]*".join(["", *pattern, ""])}$')
        k = map(r.match, queries)
        return [m != None for m in list(k)]

golang 解法, 执行用时: 0 ms, 内存消耗: 1.8 MB, 提交时间: 2022-12-08 10:08:14

func camelMatch(queries []string, p string) []bool {
	ans := []bool{}
	for _, q := range queries {
		ans = append(ans, isMatch(q, p))
	}
	return ans
}

func isMatch(q, p string) bool {
	//最后添加一个特殊字符串,因q中不可能出现特殊字符串所P-1匹配完后不可能匹配到
	p = p + "$"
	pi := 0
	for qi := range q {
		if q[qi] == p[pi] {
			pi++
		} else if q[qi] >= 'A' && q[qi] <= 'Z' {
			return false
		}
	}
	return pi == len(p)-1
}

python3 解法, 执行用时: 32 ms, 内存消耗: 15 MB, 提交时间: 2022-12-08 10:07:24

class Solution:
    def camelMatch(self, queries: List[str], pattern: str) -> List[bool]:
        def check(q: str) -> bool:
            i, j = 0, 0
            while i < len(q):
                if j < len(pattern) and q[i] == pattern[j]:
                    i += 1
                    j += 1
                elif q[i].isupper():
                    return False
                else:
                    i += 1
            return j == len(pattern)
        
        res = []
        for q in queries:
            res.append(check(q))
        return res

python3 解法, 执行用时: 44 ms, 内存消耗: 15 MB, 提交时间: 2022-12-08 10:01:55

# 关键在于query中大写字母必须match,如果没match直接返回false。
# 其他的跟check subsquence 一样。
class Solution(object):
    def camelMatch(self, queries, pattern):
        res = []
        def canMatch(q, pattern):
            i = 0;
            j = 0;
            M = len(q)
            N = len(pattern)
            while (i < M):
                if (j < N and q[i] == pattern[j]):
                    i += 1
                    j += 1
                elif q[i].isupper():
                    return False;
                else:
                    i += 1

            return i == M and j == N

        for q in queries:
            if canMatch(q, pattern):
                res.append(True)
            else:
                res.append(False)
        return res

java 解法, 执行用时: 0 ms, 内存消耗: 39.7 MB, 提交时间: 2022-12-08 10:00:57

// 双指针判断,每个query和pattern是否匹配
class Solution {
    public List<Boolean> camelMatch(String[] queries, String pattern) {
        List<Boolean> ans = new ArrayList<>();
        for (String query : queries) 
            ans.add(isMatch(query, pattern));
        return ans;
        
    }
    private boolean isMatch(String query, String pattern) {
        int idx1 = 0, idx2 = 0, n1 = query.length(), n2 = pattern.length();
        while (idx1 < n1 && idx2 < n2){
            char ch1 = query.charAt(idx1), ch2 = pattern.charAt(idx2);
            if (ch1 == ch2) {
                idx1++;idx2++;
            } else {
                if (ch1 >= 'A' && ch1 <= 'Z') return false;
                idx1++;
            }
        }
        if (idx2 != n2) return false;
        while (idx1 < n1) {
            char ch1 = query.charAt(idx1++);
            if (ch1 >= 'A' && ch1 <= 'Z') return false;
        }
        return true;
    }
}

java 解法, 执行用时: 1 ms, 内存消耗: 39.8 MB, 提交时间: 2022-12-08 09:59:07

// 从原串中去掉模式串,如果剩余的都是小写字母则为true
class Solution {
    public List<Boolean> camelMatch(String[] queries, String pattern) {
        List<Boolean> res = new ArrayList<>(queries.length);
        for (String query : queries) {
            String other = getOther(query, pattern);
            if (other.equals("")) {
                res.add(false);
            } else {
                res.add(other.toLowerCase().equals(other));
            }
        }
        return res;
    }

    private static String getOther(String query, String pattern) {
        int index = 0;
        StringBuilder sb = new StringBuilder("a"); // 避免两个串相等时返回""
        for (int i = 0; i < pattern.length(); i++) {
            char t = pattern.charAt(i);
            int index2 = query.indexOf(t, index);
            if (index2 < 0) {
                return "";
            }
            sb.append(query, index, index2);
            index = index2 + 1;
        }
        sb.append(query.substring(index));
        return sb.toString();
    }
}

上一题