Skip to content

Commit cd70a60

Browse files
committed
feat: add solutions to lc problem: No.1023
No.1023.Camelcase Matching
1 parent c154d82 commit cd70a60

File tree

7 files changed

+381
-2
lines changed

7 files changed

+381
-2
lines changed

solution/1000-1099/1023.Camelcase Matching/README.md

+137-1
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,158 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
**方法一:双指针**
57+
58+
我们可以遍历 `queries` 中的每个字符串,判断其是否与 `pattern` 匹配,若匹配则将 `true` 加入答案数组,否则加入 `false`
59+
60+
判断两个字符串是否匹配,我们可以使用双指针 $i$ 和 $j$,分别指向两个字符串的首字符,然后遍历两个字符串,如果指针 $i$ 指向的字符与指针 $j$ 指向的字符不同,则判断指针 $i$ 指向的字符是否为小写字母,若是,则指针 $i$ 循环向后移动。如果指针 $i$ 移动到字符串末尾,或者指针 $i$ 指向的字符与指针 $j$ 指向的字符不同,说明两个字符串不匹配,返回 `false`。否则,指针 $i$ 和 $j$ 同时向后移动一位,继续判断。
61+
62+
时间复杂度 $O(\sum_{i=0}^{n-1}q_i + n \times m)$,空间复杂度 $O(1)$。其中 $n$ 和 $m$ 分别为 `queries``pattern` 的长度,而 $q_i$ 为 `queries[i]` 的长度。
63+
5664
<!-- tabs:start -->
5765

5866
### **Python3**
5967

6068
<!-- 这里可写当前语言的特殊实现逻辑 -->
6169

6270
```python
63-
71+
class Solution:
72+
def camelMatch(self, queries: List[str], pattern: str) -> List[bool]:
73+
def check(s, t):
74+
m, n = len(s), len(t)
75+
i = j = 0
76+
while j < n:
77+
while i < m and s[i] != t[j] and s[i].islower():
78+
i += 1
79+
if i == m or s[i] != t[j]:
80+
return False
81+
i, j = i + 1, j + 1
82+
while i < m and s[i].islower():
83+
i += 1
84+
return i == m
85+
86+
return [check(q, pattern) for q in queries]
6487
```
6588

6689
### **Java**
6790

6891
<!-- 这里可写当前语言的特殊实现逻辑 -->
6992

7093
```java
94+
class Solution {
95+
public List<Boolean> camelMatch(String[] queries, String pattern) {
96+
List<Boolean> ans = new ArrayList<>();
97+
for (var q : queries) {
98+
ans.add(check(q, pattern));
99+
}
100+
return ans;
101+
}
102+
103+
private boolean check(String s, String t) {
104+
int m = s.length(), n = t.length();
105+
int i = 0, j = 0;
106+
for (; j < n; ++i, ++j) {
107+
while (i < m && s.charAt(i) != t.charAt(j) && Character.isLowerCase(s.charAt(i))) {
108+
++i;
109+
}
110+
if (i == m || s.charAt(i) != t.charAt(j)) {
111+
return false;
112+
}
113+
}
114+
while (i < m && Character.isLowerCase(s.charAt(i))) {
115+
++i;
116+
}
117+
return i == m;
118+
}
119+
}
120+
```
121+
122+
### **C++**
123+
124+
```cpp
125+
class Solution {
126+
public:
127+
vector<bool> camelMatch(vector<string>& queries, string pattern) {
128+
vector<bool> ans;
129+
auto check = [](string& s, string& t) {
130+
int m = s.size(), n = t.size();
131+
int i = 0, j = 0;
132+
for (; j < n; ++i, ++j) {
133+
while (i < m && s[i] != t[j] && islower(s[i])) {
134+
++i;
135+
}
136+
if (i == m || s[i] != t[j]) {
137+
return false;
138+
}
139+
}
140+
while (i < m && islower(s[i])) {
141+
++i;
142+
}
143+
return i == m;
144+
};
145+
for (auto& q : queries) {
146+
ans.push_back(check(q, pattern));
147+
}
148+
return ans;
149+
}
150+
};
151+
```
152+
153+
### **Go**
154+
155+
```go
156+
func camelMatch(queries []string, pattern string) (ans []bool) {
157+
check := func(s, t string) bool {
158+
m, n := len(s), len(t)
159+
i, j := 0, 0
160+
for ; j < n; i, j = i+1, j+1 {
161+
for i < m && s[i] != t[j] && (s[i] >= 'a' && s[i] <= 'z') {
162+
i++
163+
}
164+
if i == m || s[i] != t[j] {
165+
return false
166+
}
167+
}
168+
for i < m && s[i] >= 'a' && s[i] <= 'z' {
169+
i++
170+
}
171+
return i == m
172+
}
173+
for _, q := range queries {
174+
ans = append(ans, check(q, pattern))
175+
}
176+
return
177+
}
178+
```
71179

180+
### **TypeScript**
181+
182+
```ts
183+
function camelMatch(queries: string[], pattern: string): boolean[] {
184+
const check = (s: string, t: string) => {
185+
const m = s.length;
186+
const n = t.length;
187+
let i = 0;
188+
let j = 0;
189+
for (; j < n; ++i, ++j) {
190+
while (i < m && s[i] !== t[j] && s[i].codePointAt(0) >= 97) {
191+
++i;
192+
}
193+
if (i === m || s[i] !== t[j]) {
194+
return false;
195+
}
196+
}
197+
while (i < m && s[i].codePointAt(0) >= 97) {
198+
++i;
199+
}
200+
return i == m;
201+
};
202+
const ans: boolean[] = [];
203+
for (const q of queries) {
204+
ans.push(check(q, pattern));
205+
}
206+
return ans;
207+
}
72208
```
73209

74210
### **...**

solution/1000-1099/1023.Camelcase Matching/README_EN.md

+129-1
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,141 @@
5252
### **Python3**
5353

5454
```python
55-
55+
class Solution:
56+
def camelMatch(self, queries: List[str], pattern: str) -> List[bool]:
57+
def check(s, t):
58+
m, n = len(s), len(t)
59+
i = j = 0
60+
while j < n:
61+
while i < m and s[i] != t[j] and s[i].islower():
62+
i += 1
63+
if i == m or s[i] != t[j]:
64+
return False
65+
i, j = i + 1, j + 1
66+
while i < m and s[i].islower():
67+
i += 1
68+
return i == m
69+
70+
return [check(q, pattern) for q in queries]
5671
```
5772

5873
### **Java**
5974

6075
```java
76+
class Solution {
77+
public List<Boolean> camelMatch(String[] queries, String pattern) {
78+
List<Boolean> ans = new ArrayList<>();
79+
for (var q : queries) {
80+
ans.add(check(q, pattern));
81+
}
82+
return ans;
83+
}
84+
85+
private boolean check(String s, String t) {
86+
int m = s.length(), n = t.length();
87+
int i = 0, j = 0;
88+
for (; j < n; ++i, ++j) {
89+
while (i < m && s.charAt(i) != t.charAt(j) && Character.isLowerCase(s.charAt(i))) {
90+
++i;
91+
}
92+
if (i == m || s.charAt(i) != t.charAt(j)) {
93+
return false;
94+
}
95+
}
96+
while (i < m && Character.isLowerCase(s.charAt(i))) {
97+
++i;
98+
}
99+
return i == m;
100+
}
101+
}
102+
```
103+
104+
### **C++**
105+
106+
```cpp
107+
class Solution {
108+
public:
109+
vector<bool> camelMatch(vector<string>& queries, string pattern) {
110+
vector<bool> ans;
111+
auto check = [](string& s, string& t) {
112+
int m = s.size(), n = t.size();
113+
int i = 0, j = 0;
114+
for (; j < n; ++i, ++j) {
115+
while (i < m && s[i] != t[j] && islower(s[i])) {
116+
++i;
117+
}
118+
if (i == m || s[i] != t[j]) {
119+
return false;
120+
}
121+
}
122+
while (i < m && islower(s[i])) {
123+
++i;
124+
}
125+
return i == m;
126+
};
127+
for (auto& q : queries) {
128+
ans.push_back(check(q, pattern));
129+
}
130+
return ans;
131+
}
132+
};
133+
```
134+
135+
### **Go**
136+
137+
```go
138+
func camelMatch(queries []string, pattern string) (ans []bool) {
139+
check := func(s, t string) bool {
140+
m, n := len(s), len(t)
141+
i, j := 0, 0
142+
for ; j < n; i, j = i+1, j+1 {
143+
for i < m && s[i] != t[j] && (s[i] >= 'a' && s[i] <= 'z') {
144+
i++
145+
}
146+
if i == m || s[i] != t[j] {
147+
return false
148+
}
149+
}
150+
for i < m && s[i] >= 'a' && s[i] <= 'z' {
151+
i++
152+
}
153+
return i == m
154+
}
155+
for _, q := range queries {
156+
ans = append(ans, check(q, pattern))
157+
}
158+
return
159+
}
160+
```
61161

162+
### **TypeScript**
163+
164+
```ts
165+
function camelMatch(queries: string[], pattern: string): boolean[] {
166+
const check = (s: string, t: string) => {
167+
const m = s.length;
168+
const n = t.length;
169+
let i = 0;
170+
let j = 0;
171+
for (; j < n; ++i, ++j) {
172+
while (i < m && s[i] !== t[j] && s[i].codePointAt(0) >= 97) {
173+
++i;
174+
}
175+
if (i === m || s[i] !== t[j]) {
176+
return false;
177+
}
178+
}
179+
while (i < m && s[i].codePointAt(0) >= 97) {
180+
++i;
181+
}
182+
return i == m;
183+
};
184+
const ans: boolean[] = [];
185+
for (const q of queries) {
186+
ans.push(check(q, pattern));
187+
}
188+
return ans;
189+
}
62190
```
63191

64192
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
vector<bool> camelMatch(vector<string>& queries, string pattern) {
4+
vector<bool> ans;
5+
auto check = [](string& s, string& t) {
6+
int m = s.size(), n = t.size();
7+
int i = 0, j = 0;
8+
for (; j < n; ++i, ++j) {
9+
while (i < m && s[i] != t[j] && islower(s[i])) {
10+
++i;
11+
}
12+
if (i == m || s[i] != t[j]) {
13+
return false;
14+
}
15+
}
16+
while (i < m && islower(s[i])) {
17+
++i;
18+
}
19+
return i == m;
20+
};
21+
for (auto& q : queries) {
22+
ans.push_back(check(q, pattern));
23+
}
24+
return ans;
25+
}
26+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
func camelMatch(queries []string, pattern string) (ans []bool) {
2+
check := func(s, t string) bool {
3+
m, n := len(s), len(t)
4+
i, j := 0, 0
5+
for ; j < n; i, j = i+1, j+1 {
6+
for i < m && s[i] != t[j] && (s[i] >= 'a' && s[i] <= 'z') {
7+
i++
8+
}
9+
if i == m || s[i] != t[j] {
10+
return false
11+
}
12+
}
13+
for i < m && s[i] >= 'a' && s[i] <= 'z' {
14+
i++
15+
}
16+
return i == m
17+
}
18+
for _, q := range queries {
19+
ans = append(ans, check(q, pattern))
20+
}
21+
return
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public List<Boolean> camelMatch(String[] queries, String pattern) {
3+
List<Boolean> ans = new ArrayList<>();
4+
for (var q : queries) {
5+
ans.add(check(q, pattern));
6+
}
7+
return ans;
8+
}
9+
10+
private boolean check(String s, String t) {
11+
int m = s.length(), n = t.length();
12+
int i = 0, j = 0;
13+
for (; j < n; ++i, ++j) {
14+
while (i < m && s.charAt(i) != t.charAt(j) && Character.isLowerCase(s.charAt(i))) {
15+
++i;
16+
}
17+
if (i == m || s.charAt(i) != t.charAt(j)) {
18+
return false;
19+
}
20+
}
21+
while (i < m && Character.isLowerCase(s.charAt(i))) {
22+
++i;
23+
}
24+
return i == m;
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def camelMatch(self, queries: List[str], pattern: str) -> List[bool]:
3+
def check(s, t):
4+
m, n = len(s), len(t)
5+
i = j = 0
6+
while j < n:
7+
while i < m and s[i] != t[j] and s[i].islower():
8+
i += 1
9+
if i == m or s[i] != t[j]:
10+
return False
11+
i, j = i + 1, j + 1
12+
while i < m and s[i].islower():
13+
i += 1
14+
return i == m
15+
16+
return [check(q, pattern) for q in queries]

0 commit comments

Comments
 (0)