|
53 | 53 |
|
54 | 54 | <!-- 这里可写通用的实现逻辑 -->
|
55 | 55 |
|
| 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 | + |
56 | 64 | <!-- tabs:start -->
|
57 | 65 |
|
58 | 66 | ### **Python3**
|
59 | 67 |
|
60 | 68 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
61 | 69 |
|
62 | 70 | ```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] |
64 | 87 | ```
|
65 | 88 |
|
66 | 89 | ### **Java**
|
67 | 90 |
|
68 | 91 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
69 | 92 |
|
70 | 93 | ```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 | +``` |
71 | 179 |
|
| 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 | +} |
72 | 208 | ```
|
73 | 209 |
|
74 | 210 | ### **...**
|
|
0 commit comments