1023. Camelcase Matching
- Camelcase Matching python solution
题目描述
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.
解析
注意一种情况是匹配失败,就是
“ForceFeedBack” 和 “FoBaT”
如果存在不相等,且query当前元素为大写,直接判定失败。
所以需要同时遍历两个数组,检测是否相等。若query[i] == pattern[j]时,pattern数组向后移一个,++j。若不相等,且query[i]为大写字母,直接判定失败。
class Solution:
def camelMatch(self, queries: List[str], pattern: str) -> List[bool]:
res = []
for query in queries:
res.append(self.isSubsequence(pattern, query))
return res
def isSubsequence(self, p,q):
if p is None:
return True
if q is None:
return False
i,j=0,0
while j<len(q):
if i<len(p) and p[i]==q[j]:
i+=1
elif q[j].isupper():
return False
j+=1
return True if i==len(p) else False
Reference
https://leetcode.com/problems/camelcase-matching/discuss/270029/JavaC%2B%2BPython-Check-Subsequence-and-Regax