Skip to content

Commit d174258

Browse files
committed
Added Long Pressed Name.java
1 parent c7eca3a commit d174258

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Easy/Long Pressed Name.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public boolean isLongPressedName(String name, String typed) {
3+
int nameIdx = 0;
4+
int typeIdx = 0;
5+
int nameLen = name.length();
6+
int typeLen = typed.length();
7+
while (nameIdx < nameLen && typeIdx < typeLen) {
8+
if (name.charAt(nameIdx) != typed.charAt(typeIdx)) {
9+
return false;
10+
}
11+
while (nameIdx < nameLen && typeIdx < typeLen && name.charAt(nameIdx) == typed.charAt(typeIdx)) {
12+
nameIdx++;
13+
typeIdx++;
14+
}
15+
char prev = name.charAt(nameIdx - 1);
16+
while (typeIdx < typeLen && typed.charAt(typeIdx) == prev) {
17+
typeIdx++;
18+
}
19+
}
20+
return nameIdx == nameLen && typeIdx == typeLen;
21+
}
22+
}

0 commit comments

Comments
 (0)