# 925. Long Pressed Name

Your friend is typing his `name` into a keyboard.  Sometimes, when typing a character `c`, the key might get  long pressed , and the character will be typed 1 or more times.

You examine the `typed` characters of the keyboard.  Return `True` if it is possible that it was your friends name, with some characters (possibly none) being long pressed.

Example 1:

``````Input: name = "alex", typed = "aaleex"
Output: true
Explanation: 'a' and 'e' in 'alex' were long pressed.
``````

Example 2:

``````Input: name = "saeed", typed = "ssaaedd"
Output: false
Explanation: 'e' must have been pressed twice, but it wasn't in the typed output.
``````

Example 3:

``````Input: name = "leelee", typed = "lleeelee"
Output: true
``````

Example 4:

``````Input: name = "laiden", typed = "laiden"
Output: true
Explanation: It's not necessary to long press any character.
``````

Note:

1. `name.length <= 1000`
2. `typed.length <= 1000`
3. The characters of `name` and `typed` are lowercase letters.

``````class Solution {
public:
bool isLongPressedName(string name, string typed) {
int i = 0, j = 0, m = name.size(), n = typed.size();
while (i < m || j < n) {
if (name[i] != typed[j]) return false;
int start1 = i, start2 = j;
while (i < m - 1 && name[i] == name[i + 1]) ++i;
while (j < n - 1 && typed[j] == typed[j + 1]) ++j;
if (j - start2 + 1 < i - start1 + 1) return false;
++i; ++j;
}
return i == m && j == n;
}
};
``````

``````class Solution {
public:
bool isLongPressedName(string name, string typed) {
int i = 0, m = name.size(), n = typed.size();
for (int j = 0; j < n; ++j) {
if (i < m && name[i] == typed[j]) ++i;
else if (!j || typed[j] != typed[j - 1]) return false;
}
return i == m;
}
};
``````

Github 同步地址:

https://github.com/grandyang/leetcode/issues/925

https://leetcode.com/problems/long-pressed-name/

https://leetcode.com/problems/long-pressed-name/discuss/183994/C%2B%2BJavaPython-Two-Pointers

https://leetcode.com/problems/long-pressed-name/discuss/183929/C%2B%2B-2-lines-accepted-and-5-lines-accurate

LeetCode All in One 题目讲解汇总(持续更新中…)

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation