-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathCamelcase Matching.py
39 lines (25 loc) · 933 Bytes
/
Camelcase Matching.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Solution:
def camelMatch(self, queries: List[str], pattern: str) -> List[bool]:
res, N = [], len(pattern)
for query in queries:
if self.upLetter(query) != self.upLetter(pattern) or self.LCS(query, pattern) != N:
res.append(False)
else:
res.append(True)
return res
def LCS(self, A, B):
N, M = len(A), len(B)
d = [[0 for _ in range(M+1)] for _ in range(N+1)]
for i in range(1, N+1):
for j in range(1, M+1):
if A[i - 1] == B[j - 1]:
d[i][j] = 1 + d[i-1][j-1]
else:
d[i][j] = max(d[i-1][j], d[i][j-1])
return d[-1][-1]
def upLetter(self, w):
count = 0
for c in w:
if c.isupper():
count += 1
return count