|
41 | 41 |
|
42 | 42 | <!-- 这里可写通用的实现逻辑 -->
|
43 | 43 |
|
| 44 | +**方法一:双指针** |
| 45 | + |
44 | 46 | <!-- tabs:start -->
|
45 | 47 |
|
46 | 48 | ### **Python3**
|
47 | 49 |
|
48 | 50 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
49 | 51 |
|
50 | 52 | ```python
|
51 |
| - |
| 53 | +class Solution: |
| 54 | + def isLongPressedName(self, name: str, typed: str) -> bool: |
| 55 | + m, n = len(name), len(typed) |
| 56 | + i = j = 0 |
| 57 | + while i < m and j < n: |
| 58 | + if name[i] != typed[j]: |
| 59 | + return False |
| 60 | + cnt1 = cnt2 = 0 |
| 61 | + c = name[i] |
| 62 | + while i + 1 < m and name[i + 1] == c: |
| 63 | + i += 1 |
| 64 | + cnt1 += 1 |
| 65 | + while j + 1 < n and typed[j + 1] == c: |
| 66 | + j += 1 |
| 67 | + cnt2 += 1 |
| 68 | + if cnt1 > cnt2: |
| 69 | + return False |
| 70 | + i, j = i + 1, j + 1 |
| 71 | + return i == m and j == n |
52 | 72 | ```
|
53 | 73 |
|
54 | 74 | ### **Java**
|
55 | 75 |
|
56 | 76 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
57 | 77 |
|
58 | 78 | ```java
|
| 79 | +class Solution { |
| 80 | + public boolean isLongPressedName(String name, String typed) { |
| 81 | + int m = name.length(), n = typed.length(); |
| 82 | + int i = 0, j = 0; |
| 83 | + for (; i < m && j < n; ++i, ++j) { |
| 84 | + if (name.charAt(i) != typed.charAt(j)) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + int cnt1 = 0, cnt2 = 0; |
| 88 | + char c = name.charAt(i); |
| 89 | + while (i + 1 < m && name.charAt(i + 1) == c) { |
| 90 | + ++i; |
| 91 | + ++cnt1; |
| 92 | + } |
| 93 | + while (j + 1 < n && typed.charAt(j + 1) == c) { |
| 94 | + ++j; |
| 95 | + ++cnt2; |
| 96 | + } |
| 97 | + if (cnt1 > cnt2) { |
| 98 | + return false; |
| 99 | + } |
| 100 | + } |
| 101 | + return i == m && j == n; |
| 102 | + } |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +### **C++** |
| 107 | + |
| 108 | +```cpp |
| 109 | +class Solution { |
| 110 | +public: |
| 111 | + bool isLongPressedName(string name, string typed) { |
| 112 | + int m = name.size(), n = typed.size(); |
| 113 | + int i = 0, j = 0; |
| 114 | + for (; i < m && j < n; ++i, ++j) |
| 115 | + { |
| 116 | + if (name[i] != typed[j]) return false; |
| 117 | + int cnt1 = 0, cnt2 = 0; |
| 118 | + char c = name[i]; |
| 119 | + while (i + 1 < m && name[i + 1] == c) |
| 120 | + { |
| 121 | + ++i; |
| 122 | + ++cnt1; |
| 123 | + } |
| 124 | + while (j + 1 < n && typed[j + 1] == c) |
| 125 | + { |
| 126 | + ++j; |
| 127 | + ++cnt2; |
| 128 | + } |
| 129 | + if (cnt1 > cnt2) return false; |
| 130 | + } |
| 131 | + return i == m && j == n; |
| 132 | + } |
| 133 | +}; |
| 134 | +``` |
59 | 135 |
|
| 136 | +### **Go** |
| 137 | +
|
| 138 | +```go |
| 139 | +func isLongPressedName(name string, typed string) bool { |
| 140 | + m, n := len(name), len(typed) |
| 141 | + i, j := 0, 0 |
| 142 | + for ; i < m && j < n; i, j = i+1, j+1 { |
| 143 | + if name[i] != typed[j] { |
| 144 | + return false |
| 145 | + } |
| 146 | + cnt1, cnt2 := 0, 0 |
| 147 | + c := name[i] |
| 148 | + for i+1 < m && name[i+1] == c { |
| 149 | + i++ |
| 150 | + cnt1++ |
| 151 | + } |
| 152 | + for j+1 < n && typed[j+1] == c { |
| 153 | + j++ |
| 154 | + cnt2++ |
| 155 | + } |
| 156 | + if cnt1 > cnt2 { |
| 157 | + return false |
| 158 | + } |
| 159 | + } |
| 160 | + return i == m && j == n |
| 161 | +} |
60 | 162 | ```
|
61 | 163 |
|
62 | 164 | ### **...**
|
|
0 commit comments