你的朋友正在使用键盘输入他的名字 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
解释:长按名字中的字符并不是必要的。
solution:
public boolean isLongPressedName(String name, String typed) {
// 首字母不相等
if(typed.charAt(0) != name.charAt(0)){
return false;
}
int i = 0, j = 0;
while(j < typed.length()){
// 相等的情况
if (i < name.length() && typed.charAt(j) == name.charAt(i)) {
i++;
j++;
}
// i == name.length()和不相等的情况 处理方式相同
else if (typed.charAt(j) == typed.charAt(j - 1)) {
j++;
} else {
return false;
}
}
// 保证name被遍历完,这里很重要
return i == name.length();
}
执行结果:
执行用时:1 ms, 在所有 Java 提交中击败了86.83%的用户
内存消耗:36.6 MB, 在所有 Java 提交中击败了74.09%的用户