-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathLongPressedName.java
More file actions
21 lines (21 loc) · 832 Bytes
/
LongPressedName.java
File metadata and controls
21 lines (21 loc) · 832 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class LongPressedName {
public boolean isLongPressedName(String name, String typed) {
char current = name.charAt(0);
for (int i = 0, j = 0, freq = 0, typedFreq = 0 ; i < name.length() && j < typed.length() ; ) {
while (i < name.length() && name.charAt(i) == current) {
i++;
freq++;
}
while (j < typed.length() && typed.charAt(j) == current) {
j++;
typedFreq++;
}
if (typedFreq < freq) return false;
if (i == name.length() && j < typed.length()) return false;
if (j == typed.length() && i < name.length()) return false;
current = i >= name.length() ? ' ' : name.charAt(i);
freq = typedFreq = 0;
}
return true;
}
}