-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathLongPressedName.java
More file actions
35 lines (35 loc) · 1.04 KB
/
LongPressedName.java
File metadata and controls
35 lines (35 loc) · 1.04 KB
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
/**
* Your friend is typing his name into a keyboard. Sometimes, when typing a character c, the key might get long pressed, and the character will be typed 1 or more times.
*
* You examine the typed characters of the keyboard. Return True if it is possible that it was your friends name, with some characters (possibly none) being long pressed.
*
* Example 1:
*
* Input: name = "alex", typed = "aaleex"
* Output: true
* Explanation: 'a' and 'e' in 'alex' were long pressed.
*/
class Solution {
public boolean isLongPressedName(String name, String typed) {
int i = 0;
int j = 0;
int m = name.length();
int n = typed.length();
if (m > n) return false;
while(i < m) {
while (i < m && j < n && name.charAt(i) == typed.charAt(j)) {
i++;
j++;
}
while(j > 0 && j < n && typed.charAt(j - 1) == typed.charAt(j)) {
j++;
}
if (i == m) {
return j == n;
} else if (j == n || name.charAt(i) != typed.charAt(j)) {
return false;
}
}
return j == n;
}
}