-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString925.java
More file actions
62 lines (61 loc) · 1.9 KB
/
String925.java
File metadata and controls
62 lines (61 loc) · 1.9 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package string;
/**
* @ProjectName: leetcode
* @Package: string
* @ClassName: String925
* @Author: markey
* @Description:
* 你的朋友正在使用键盘输入他的名字 name。偶尔,在键入字符 c 时,按键可能会被长按,而字符可能被输入 1 次或多次。
*
* 你将会检查键盘输入的字符 typed。如果它对应的可能是你的朋友的名字(其中一些字符可能被长按),那么就返回 True。
*
*
*
* 示例 1:
*
* 输入:name = "alex", typed = "aaleex"
* 输出:true
* 解释:'alex' 中的 'a' 和 'e' 被长按。
* 示例 2:
*
* 输入:name = "saeed", typed = "ssaaedd"
* 输出:false
* 解释:'e' 一定需要被键入两次,但在 typed 的输出中不是这样。
* 示例 3:
*
* 输入:name = "leelee", typed = "lleeelee"
* 输出:true
* 示例 4:
*
* 输入:name = "laiden", typed = "laiden"
* 输出:true
* 解释:长按名字中的字符并不是必要的。
*
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/long-pressed-name
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
* @Date: 2020/2/8 18:35
* @Version: 1.0
*/
public class String925 {
public boolean isLongPressedName(String name, String typed) {
int indexName = 0, indexTyped = 0;
while (indexName < name.length() && indexTyped < typed.length()) {
if (name.charAt(indexName) == typed.charAt(indexTyped)) {
indexName++;
indexTyped++;
continue;
}
if (indexTyped > 0 && typed.charAt(indexTyped) == typed.charAt(indexTyped - 1)) {
indexTyped++;
} else {
return false;
}
}
if (indexName == name.length()) {
return true;
} else {
return false;
}
}
}