leetcode 925. Long Pressed Name
class Solution {
public boolean isLongPressedName(String name, String typed) {
int i = 0, j = 0;
while (i < name.length()) {
if (j == typed.length() || name.charAt(i) != typed.charAt(j)) return false;
int i1 = i;
for (;i1 < name.length() && name.charAt(i1) == name.charAt(i); i1++);
int i2 = j;
for (;i2 < typed.length() && typed.charAt(i2) == typed.charAt(j);i2++);
if (i2 - j < i1 - i) return false;
i = i1;
j = i2;
}
if (j < typed.length()) return false;
return true;
}
}
A shorter solution
class Solution {
public boolean isLongPressedName(String name, String typed) {
int M = name.length(), N = typed.length();
int j = 0;
for (int i = 0; i < N; ++i) {
if (j < M && typed.charAt(i) == name.charAt(j)) {
++j;
}
else if (i == 0 || typed.charAt(i - 1) != typed.charAt(i)) {
return false;
}
}
return j == M;
}
}