Skip to content

Commit 0e424f7

Browse files
committed
Due to problem 925 test case change, so solution should modify
1 parent e02e370 commit 0e424f7

File tree

4 files changed

+41
-30
lines changed

4 files changed

+41
-30
lines changed

leetcode/0925.Long-Pressed-Name/925. Long Pressed Name.go

+11-13
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,18 @@ func isLongPressedName(name string, typed string) bool {
77
if (len(name) == 0 && len(typed) != 0) || (len(name) != 0 && len(typed) == 0) {
88
return false
99
}
10-
11-
j := 0
12-
for i := 0; i < len(name); i++ {
13-
if j < len(typed) && name[i] == typed[j] {
10+
i, j := 0, 0
11+
for i < len(name) && j < len(typed) {
12+
if name[i] != typed[j] {
13+
return false
14+
}
15+
for i < len(name) && j < len(typed) && name[i] == typed[j] {
16+
i++
17+
j++
18+
}
19+
for j < len(typed) && typed[j] == typed[j-1] {
1420
j++
15-
continue
16-
} else {
17-
if i > 0 && j < len(typed) && name[i-1] == typed[j] {
18-
j++
19-
i--
20-
} else {
21-
return false
22-
}
2321
}
2422
}
25-
return true
23+
return i == len(name) && j == len(typed)
2624
}

leetcode/0925.Long-Pressed-Name/925. Long Pressed Name_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ func Test_Problem925(t *testing.T) {
3232
ans925{true},
3333
},
3434

35+
{
36+
para925{"alex", "alexxr"},
37+
ans925{false},
38+
},
39+
40+
{
41+
para925{"alex", "alexxxxr"},
42+
ans925{false},
43+
},
44+
45+
{
46+
para925{"alex", "alexxxxx"},
47+
ans925{true},
48+
},
49+
3550
{
3651
para925{"saeed", "ssaaedd"},
3752
ans925{false},

leetcode/0925.Long-Pressed-Name/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ Note:
5353

5454
## 解题思路
5555

56-
这一题也可以借助滑动窗口的思想。2 个字符串一起比较,如果遇到有相同的字符串,窗口继续往后滑动。直到遇到了第一个不同的字符,如果遇到两个字符串不相等的情况,可以直接返回 false。具体实现见代码。
57-
56+
- 这一题也可以借助滑动窗口的思想。2 个字符串一起比较,如果遇到有相同的字符串,窗口继续往后滑动。直到遇到了第一个不同的字符,如果遇到两个字符串不相等的情况,可以直接返回 false。具体实现见代码。
57+
- 这一题的测试用例修改过一次,需要注意我这里写的第二组测试用例,当 name 结束以后,如果 typed 还有多余的不同的字符,这种情况要输出 false 的。具体见 test 文件里面的第二组,第三组,第四组测试用例。
5858

5959

6060

website/content/ChapterFour/0925.Long-Pressed-Name.md

+13-15
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ Explanation: It's not necessary to long press any character.
6161

6262
## 解题思路
6363

64-
这一题也可以借助滑动窗口的思想。2 个字符串一起比较,如果遇到有相同的字符串,窗口继续往后滑动。直到遇到了第一个不同的字符,如果遇到两个字符串不相等的情况,可以直接返回 false。具体实现见代码。
65-
64+
- 这一题也可以借助滑动窗口的思想。2 个字符串一起比较,如果遇到有相同的字符串,窗口继续往后滑动。直到遇到了第一个不同的字符,如果遇到两个字符串不相等的情况,可以直接返回 false。具体实现见代码。
65+
- 这一题的测试用例修改过一次,需要注意我这里写的第二组测试用例,当 name 结束以后,如果 typed 还有多余的不同的字符,这种情况要输出 false 的。具体见 test 文件里面的第二组,第三组,第四组测试用例。
6666

6767

6868

@@ -88,22 +88,20 @@ func isLongPressedName(name string, typed string) bool {
8888
if (len(name) == 0 && len(typed) != 0) || (len(name) != 0 && len(typed) == 0) {
8989
return false
9090
}
91-
92-
j := 0
93-
for i := 0; i < len(name); i++ {
94-
if j < len(typed) && name[i] == typed[j] {
91+
i, j := 0, 0
92+
for i < len(name) && j < len(typed) {
93+
if name[i] != typed[j] {
94+
return false
95+
}
96+
for i < len(name) && j < len(typed) && name[i] == typed[j] {
97+
i++
98+
j++
99+
}
100+
for j < len(typed) && typed[j] == typed[j-1] {
95101
j++
96-
continue
97-
} else {
98-
if i > 0 && j < len(typed) && name[i-1] == typed[j] {
99-
j++
100-
i--
101-
} else {
102-
return false
103-
}
104102
}
105103
}
106-
return true
104+
return i == len(name) && j == len(typed)
107105
}
108106

109107
```

0 commit comments

Comments
 (0)