-
Notifications
You must be signed in to change notification settings - Fork 0
/
isLongPressedName.java
37 lines (30 loc) · 1.02 KB
/
isLongPressedName.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.*;
class isLongPressedName{
public static void main(String args[]){
String name = "alex";
String typed = "aaleex";
System.out.println(IsLongPressedName(name,typed));
}
public static boolean IsLongPressedName(String name, String typed) {
int j = 0;
for (char c: name.toCharArray()) {
if (j == typed.length())
return false;
// If mismatch...
if (typed.charAt(j) != c) {
// If it's the first char of the block, ans is false.
if (j==0 || typed.charAt(j-1) != typed.charAt(j))
return false;
// Discard all similar chars
char cur = typed.charAt(j);
while (j < typed.length() && typed.charAt(j) == cur)
j++;
// If next isn't a match, ans is false.
if (j == typed.length() || typed.charAt(j) != c)
return false;
}
j++;
}
return true;
}
}