酷代码 AI
菜单
服务商

#include <iostream> #include <cstdlib> #include <string> using namespace std; bool isLongPressedStr(string origin,string typed) { //origin为原始字符串,typed为键盘键入的字符串 //在此处写入代码 } 本关任务: 键盘在长时间使用以后可能会产生粘连效应,即按压某个键位1次,但是会输入1个或多个重复的字符。 给你两个字符串origin和typed。origin为原始需要输入的单词,typed为实际上键盘键入的单词,请判断typed是否为origin所对应的单词,返回True或False 例如: origin="abcd",typed="aaabbcd" 返回True origin="aabcd",typed="abbbcccddd" 返回False。因为typed字符串中a应该至少出现2次

```cpp #include <iostream> #include <cstdlib> #include <string> using namespace std; bool isLongPressedStr(string origin, string typed) { int i = 0, j = 0; while (j < typed.length()) { if (i < origin.length() && origin[i] == typed[j]) { i++; j++; } else if (j > 0 && typed[j] == typed[j - 1]) { j++; } else { return false; } } return i == origin.length(); } ``` [2025-11-12 15:06:42 | AI问答 | 139点数解答]

相关提问
发财导航,免费问AI
实用工具查看更多