Skip to content

Commit e255f87

Browse files
committed
feat(每日一题): ✨双指针解决
1 parent 34244e6 commit e255f87

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* 双指针
3+
* 时间复杂度 O(n + m)
4+
* 空间复杂度 O(1)
5+
*/
6+
const isLongPressedName = function(name, typed) {
7+
let x = 0;
8+
let y = 0;
9+
const len1 = name.length;
10+
const len2 = typed.length;
11+
while (x < len1 || y < len2) {
12+
if (name[x] === typed[y]) {
13+
x++;
14+
y++;
15+
} else if (y > 0 && typed[y] === typed[y - 1]) {
16+
y++;
17+
} else {
18+
return false;
19+
}
20+
}
21+
22+
return true;
23+
};

0 commit comments

Comments
 (0)