【题目】925. 长按键入
你的朋友正在使用键盘输入他的名字 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
解释:长按名字中的字符并不是必要的。
提示:
name.length <= 1000
typed.length <= 1000
name 和 typed 的字符都是小写字母。
【解题思路1】双指针
主要是这个例子
"alex"
"aaleelx"
class Solution {
public boolean isLongPressedName(String name, String typed) {
int len_n = name.length();
int len_t = typed.length();
if(len_n > len_t){
return false;
}
int i = 0, j = 0;
while(i < len_n && j < len_t){
if(name.charAt(i) == typed.charAt(j)){
i++;
j++;
}else if(j > 0 && typed.charAt(j) == typed.charAt(j-1)){
j++;
}else{
return false;
}
}
while(j < len_t){ //t 有剩余,且剩余字母和最后一个不一样,false
if(typed.charAt(j) != typed.charAt(j-1))
return false;
j++;
}
return i == len_n;
}
}
更详细的写法:
class Solution {
public boolean isLongPressedName(String name, String typed) {
int lenA = name.length();
int lenB = typed.length();
if(lenB < lenA){
return false;
}
int i = 0;
int j = 0;
while(i < lenA && j < lenB){
// 如果对应位置上的字符相等,匹配,双指针同时前进1
if(name.charAt(i) == typed.charAt(j)){
i++;
j++;
}else{
if(j==0){ //如果是第一个字符不相等 直接返回false
return false;
}else{
//j的位置和前一个位置如果相等,说明是长按了
if(typed.charAt(j-1) == typed.charAt(j)){
j++;
}else{ //如果不相等 说明这个位置是不是长按的,又不匹配name,false
return false;
}
}
}
}
//情况1 如果i到末尾了,但是j没到末尾 ,则判断 typed字符串剩余的字符是否跟name最后字符一样
if(i == lenA && j < lenB){
for(int m = j; m < lenB; m++){
if(typed.charAt(m) != name.charAt(lenA - 1)){
return false;
}
}
}
//情况2 如果j到末尾了,那么如果i没到末尾 false
if(j == lenB && i < lenA){
return false;
}
return true;
}
}
【解题思路2】转换成a1b5c6的形式再比较
参考面试题 01.06. 字符串压缩的思想,都转化为 a1b5c6 的形式在比较字母顺序是否相同,并且typed拥有的字母个数要 >= name的
class Solution {
public static boolean isLongPressedName(String name, String typed) {
char[] c1 = change(name).toCharArray();
char[] c2 = change(typed).toCharArray();
int i = 0, j = 0;
int num1 = 0, num2 = 0;
while(i < c1.length && j < c2.length){
//对应字母相同,继续遍历
if (c1[i] == c2[j]) {
i++;
j++;
} else {
return false;
}
//c1中遇到数字,一直遍历到不是数字,并计算出数值,c2同理
while(i < c1.length && Character.isDigit(c1[i])) {
num1 = 10 * num1 + Integer.valueOf(c1[i] - '0');
i++;
}
while(j < c2.length && Character.isDigit(c2[j])) {
num2 = 10 * num2 + Integer.valueOf(c2[j] - '0');
j++;
}
//如果c1的某字母个数大于c2的,false
if(num1 > num2){
return false;
}
num1 = 0;
num2 = 0;
}
// 两个字符串若有剩余
if(i < c1.length || j < c2.length) return false;
return true;
}
public static String change(String word){
int count = 1;
StringBuilder sb = new StringBuilder().append(word.charAt(0));
for(int i = 1; i < word.length(); i++){
if(word.charAt(i) == word.charAt(i-1)){
count++;
}else{
sb.append(count).append(word.charAt(i));
count = 1;
}
}
sb.append(count);
return sb.toString();
}
}
按块分组,使用Group 类型
对于字符串 S = ‘aabbbbccc’,可以把表示成这种分组形式 - groupify(S) = [(‘a’, 2), (‘b’, 4), (‘c’, 3)],其中 ‘abc’ 为 键值,[2, 4, 3] 为 连续出现的次数。
对于一个长按键入的 typed 来说,依次每个字母连续出现的次数一定大于等于 name 中连续字母出现的次数。
class Solution {
public boolean isLongPressedName(String name, String typed) {
Group g1 = groupify(name);
Group g2 = groupify(typed);
if (!g1.key.equals(g2.key))
return false;
for (int i = 0; i < g1.count.size(); ++i)
if (g1.count.get(i) > g2.count.get(i))
return false;
return true;
}
public Group groupify(String S) {
StringBuilder key = new StringBuilder();
List<Integer> count = new ArrayList();
int anchor = 0;
int N = S.length();
for (int i = 0; i < N; ++i) {
if (i == N-1 || S.charAt(i) != S.charAt(i+1)) { // end of group
key.append(S.charAt(i));
count.add(i - anchor + 1);
anchor = i+1;
}
}
return new Group(key.toString(), count);
}
}
class Group {
String key;
List<Integer> count;
Group(String k, List<Integer> c) {
key = k;
count = c;
}
}