Skip to content

Commit 4fd68f2

Browse files
committed
feat: add solutions to lc problems: No.0925,0926
* No.0925.Long Pressed Name * No.0926.Flip String to Monotone Increasing
1 parent 9b63834 commit 4fd68f2

File tree

9 files changed

+485
-16
lines changed

9 files changed

+485
-16
lines changed

solution/0900-0999/0925.Long Pressed Name/README.md

+103-1
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,124 @@
4141

4242
<!-- 这里可写通用的实现逻辑 -->
4343

44+
**方法一:双指针**
45+
4446
<!-- tabs:start -->
4547

4648
### **Python3**
4749

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

5052
```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
5272
```
5373

5474
### **Java**
5575

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

5878
```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+
```
59135
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+
}
60162
```
61163

62164
### **...**

solution/0900-0999/0925.Long Pressed Name/README_EN.md

+101-1
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,113 @@
4040
### **Python3**
4141

4242
```python
43-
43+
class Solution:
44+
def isLongPressedName(self, name: str, typed: str) -> bool:
45+
m, n = len(name), len(typed)
46+
i = j = 0
47+
while i < m and j < n:
48+
if name[i] != typed[j]:
49+
return False
50+
cnt1 = cnt2 = 0
51+
c = name[i]
52+
while i + 1 < m and name[i + 1] == c:
53+
i += 1
54+
cnt1 += 1
55+
while j + 1 < n and typed[j + 1] == c:
56+
j += 1
57+
cnt2 += 1
58+
if cnt1 > cnt2:
59+
return False
60+
i, j = i + 1, j + 1
61+
return i == m and j == n
4462
```
4563

4664
### **Java**
4765

4866
```java
67+
class Solution {
68+
public boolean isLongPressedName(String name, String typed) {
69+
int m = name.length(), n = typed.length();
70+
int i = 0, j = 0;
71+
for (; i < m && j < n; ++i, ++j) {
72+
if (name.charAt(i) != typed.charAt(j)) {
73+
return false;
74+
}
75+
int cnt1 = 0, cnt2 = 0;
76+
char c = name.charAt(i);
77+
while (i + 1 < m && name.charAt(i + 1) == c) {
78+
++i;
79+
++cnt1;
80+
}
81+
while (j + 1 < n && typed.charAt(j + 1) == c) {
82+
++j;
83+
++cnt2;
84+
}
85+
if (cnt1 > cnt2) {
86+
return false;
87+
}
88+
}
89+
return i == m && j == n;
90+
}
91+
}
92+
```
93+
94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
bool isLongPressedName(string name, string typed) {
100+
int m = name.size(), n = typed.size();
101+
int i = 0, j = 0;
102+
for (; i < m && j < n; ++i, ++j)
103+
{
104+
if (name[i] != typed[j]) return false;
105+
int cnt1 = 0, cnt2 = 0;
106+
char c = name[i];
107+
while (i + 1 < m && name[i + 1] == c)
108+
{
109+
++i;
110+
++cnt1;
111+
}
112+
while (j + 1 < n && typed[j + 1] == c)
113+
{
114+
++j;
115+
++cnt2;
116+
}
117+
if (cnt1 > cnt2) return false;
118+
}
119+
return i == m && j == n;
120+
}
121+
};
122+
```
49123
124+
### **Go**
125+
126+
```go
127+
func isLongPressedName(name string, typed string) bool {
128+
m, n := len(name), len(typed)
129+
i, j := 0, 0
130+
for ; i < m && j < n; i, j = i+1, j+1 {
131+
if name[i] != typed[j] {
132+
return false
133+
}
134+
cnt1, cnt2 := 0, 0
135+
c := name[i]
136+
for i+1 < m && name[i+1] == c {
137+
i++
138+
cnt1++
139+
}
140+
for j+1 < n && typed[j+1] == c {
141+
j++
142+
cnt2++
143+
}
144+
if cnt1 > cnt2 {
145+
return false
146+
}
147+
}
148+
return i == m && j == n
149+
}
50150
```
51151

52152
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
bool isLongPressedName(string name, string typed) {
4+
int m = name.size(), n = typed.size();
5+
int i = 0, j = 0;
6+
for (; i < m && j < n; ++i, ++j)
7+
{
8+
if (name[i] != typed[j]) return false;
9+
int cnt1 = 0, cnt2 = 0;
10+
char c = name[i];
11+
while (i + 1 < m && name[i + 1] == c)
12+
{
13+
++i;
14+
++cnt1;
15+
}
16+
while (j + 1 < n && typed[j + 1] == c)
17+
{
18+
++j;
19+
++cnt2;
20+
}
21+
if (cnt1 > cnt2) return false;
22+
}
23+
return i == m && j == n;
24+
}
25+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func isLongPressedName(name string, typed string) bool {
2+
m, n := len(name), len(typed)
3+
i, j := 0, 0
4+
for ; i < m && j < n; i, j = i+1, j+1 {
5+
if name[i] != typed[j] {
6+
return false
7+
}
8+
cnt1, cnt2 := 0, 0
9+
c := name[i]
10+
for i+1 < m && name[i+1] == c {
11+
i++
12+
cnt1++
13+
}
14+
for j+1 < n && typed[j+1] == c {
15+
j++
16+
cnt2++
17+
}
18+
if cnt1 > cnt2 {
19+
return false
20+
}
21+
}
22+
return i == m && j == n
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public boolean isLongPressedName(String name, String typed) {
3+
int m = name.length(), n = typed.length();
4+
int i = 0, j = 0;
5+
for (; i < m && j < n; ++i, ++j) {
6+
if (name.charAt(i) != typed.charAt(j)) {
7+
return false;
8+
}
9+
int cnt1 = 0, cnt2 = 0;
10+
char c = name.charAt(i);
11+
while (i + 1 < m && name.charAt(i + 1) == c) {
12+
++i;
13+
++cnt1;
14+
}
15+
while (j + 1 < n && typed.charAt(j + 1) == c) {
16+
++j;
17+
++cnt2;
18+
}
19+
if (cnt1 > cnt2) {
20+
return false;
21+
}
22+
}
23+
return i == m && j == n;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def isLongPressedName(self, name: str, typed: str) -> bool:
3+
m, n = len(name), len(typed)
4+
i = j = 0
5+
while i < m and j < n:
6+
if name[i] != typed[j]:
7+
return False
8+
cnt1 = cnt2 = 0
9+
c = name[i]
10+
while i + 1 < m and name[i + 1] == c:
11+
i += 1
12+
cnt1 += 1
13+
while j + 1 < n and typed[j + 1] == c:
14+
j += 1
15+
cnt2 += 1
16+
if cnt1 > cnt2:
17+
return False
18+
i, j = i + 1, j + 1
19+
return i == m and j == n

0 commit comments

Comments
 (0)