[
string
dp
two pointers
]
Leetcode 1023. Camelcase Matching
Problem statement
https://leetcode.com/problems/camelcase-matching/
Solution
What we need to check for each pair (q, pattern)
is:
- If
q
is subsequence ofpattern
, reuse classical problem Leetcode 0392. Is Subsequence. - If number of capital letters in
q
andpattern
is the same.
Complexity
It is O(n)
for time and space, where n
is the total length of all queries
.
Code
class Solution:
def camelMatch(self, queries, pattern):
def isSubsequence(s, t):
t = iter(t)
return all(c in t for c in s)
ans, up_cnt = [], sum(i.isupper() for i in pattern)
for q in queries:
if not isSubsequence(pattern, q) or sum(i.isupper() for i in q) != up_cnt:
ans += [False]
else:
ans += [True]
return ans