c_925_Long_Pressed_Name

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace std;

class Solution {
public:
bool isLongPressedName(string name, string typed) {
if (!name.length() && !typed.length()) {
return true;
} else if (!name.length() || !typed.length()) {
return false;
}
int idx = 0;
for (int i = 0; i < typed.length(); i++) {
if (typed[i] == name[idx]) {
idx++;
} else if (idx != 0 && typed[i] == name[idx - 1]) {
continue;
} else {
return false;
}
}
return idx == name.length();
}
};

int main() {
string name = "alex", typed = "aallexx";
Solution solution;
bool is_pressed = solution.isLongPressedName(name, typed);
cout << is_pressed << endl;
return 0;
}