Skip to content

精选leecode300题,双指针925题 #138

Open

Description

@tjl8787

秀哥好,925这道题,有个用例
输入name ="alex"typed ="aaleelx"预期结果false,参考网站解法时发现现在跑不通了。我的想法是在用双指针比较两个string时,如果typed对应元素与name对应元素不相等,多考虑一个if判断下当前typed元素是否和前一个typed元素相等,不相等直接返回false。
代码为:
bool isLongPressedName(string name, string typed) {
if(name.size()>typed.size()){
return false;
}
int len1=name.size();
int len2=typed.size();
unsigned i=0,j=0;
while(i<len1 and j<len2){
if(name[i]==typed[j]){
i++;
j++;
}
else{
// 如果当前字符与前一个字符相等,继续循环
if(j > 0 && typed[j] == typed[j - 1]){
j++;
}
// 否则,说明输入不符合要求,返回 false
else{
return false;
}
}
}
while (j < len2 && typed[j] == typed[j - 1]) {
j++;
}
return i == len1 && j == len2;
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions