Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
tjl8787 opened this issue Nov 25, 2023 · 0 comments
Open

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

tjl8787 opened this issue Nov 25, 2023 · 0 comments

Comments

@tjl8787
Copy link

tjl8787 commented Nov 25, 2023

秀哥好,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;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant