---
# System prepended metadata

title: LC 925. Long Pressed Name
tags: [easy, leedcode, Two Pointer, python, c++]

---

# LC 925. Long Pressed Name

### [Problem link](https://leetcode.com/problems/long-pressed-name/)

###### tags: `leedcode` `python` `c++` `easy` `Two Pointer`

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

You examine the <code>typed</code> characters of the keyboard. Return <code>True</code> 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 was not in the typed output.
```

 **Constraints:** 

- <code>1 <= name.length, typed.length <= 1000</code>
- <code>name</code> and <code>typed</code> consist of only lowercase English letters.

## Solution 1 - Two Pointer
#### Python
```python=
class Solution:
    def isLongPressedName(self, name: str, typed: str) -> bool:
        i = 0

        for j in range(len(typed)):
            if i < len(name) and typed[j] == name[i]:
                i += 1
            elif i == 0 or typed[j] != name[i - 1]:
                return False

        return i == len(name)
```
#### C++
```cpp=
class Solution {
public:
    bool isLongPressedName(string name, string typed) {
        int p1 = 0;
        int p2 = 0;

        while (p2 < typed.size()) {
            if (p1 < name.size() && typed[p2] == name[p1]) {
                p1++;
            } else if (p1 == 0 || typed[p2] != name[p1 - 1]) {
                return false;
            }
            p2++;
        }
        return p1 == name.size();
    }
};
```

>### Complexity
>n = typed.length
>|             | Time Complexity | Space Complexity |
>| ----------- | --------------- | ---------------- |
>| Solution 1  | O(n)            | O(1)             |


## Note
x
