|
| 1 | +--- |
| 2 | +id: long-pressed-name |
| 3 | +title: Long Pressed Name |
| 4 | +sidebar_label: Long Pressed Name |
| 5 | +tags: |
| 6 | + - strings |
| 7 | + - two-pointers |
| 8 | +--- |
| 9 | + |
| 10 | +## Problem Description |
| 11 | + |
| 12 | +| Problem Statement | Solution Link | LeetCode Profile | |
| 13 | +| :------------------------------------------------------ | :------------------------------------------------------------------------- | :------------------------------------------------------ | |
| 14 | +| [Long Pressed Name](https://leetcode.com/problems/long-pressed-name/description/) | [Long Pressed Name Solution on LeetCode](https://leetcode.com/problems/Long-Pressed-Name/solutions/) | [Nikita Saini](https://leetcode.com/u/Saini_Nikita/) | |
| 15 | + |
| 16 | +## Problem Description |
| 17 | + |
| 18 | +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. |
| 19 | + |
| 20 | +You examine the typed characters of the keyboard. Return `True` if it is possible that it was your friend's name, with some characters (possibly none) being long pressed. |
| 21 | + |
| 22 | +### Example 1 |
| 23 | + |
| 24 | +**Input:** `name = "alex" typed = "aaleex"` |
| 25 | +**Output:** `true` |
| 26 | + |
| 27 | +**Explanation:** |
| 28 | +`'a' and 'e' in 'alex' were long pressed.` |
| 29 | + |
| 30 | +### Example 2 |
| 31 | + |
| 32 | +**Input:** `name = "saeed" typed = "ssaaedd"` |
| 33 | +**Output:** `false` |
| 34 | + |
| 35 | +**Explanation:** |
| 36 | +`'e' must have been pressed twice, but it was not in the typed output.` |
| 37 | + |
| 38 | +### Constraints |
| 39 | + |
| 40 | +- `1 <= name.length, typed.length <= 1000` |
| 41 | +- `name` and `typed` consist of only lowercase English letters. |
| 42 | + |
| 43 | +## Approach |
| 44 | + |
| 45 | +We can solve this problem using a two-pointer technique. We'll use two pointers to traverse both the `name` and `typed` strings. We'll compare characters at both pointers and handle long presses by ensuring the characters in `typed` match the corresponding characters in `name` in the correct order. |
| 46 | + |
| 47 | +## Solution |
| 48 | + |
| 49 | +### Python |
| 50 | + |
| 51 | +```python |
| 52 | +def isLongPressedName(name: str, typed: str) -> bool: |
| 53 | + i, j = 0, 0 |
| 54 | + while j < len(typed): |
| 55 | + if i < len(name) and name[i] == typed[j]: |
| 56 | + i += 1 |
| 57 | + elif j == 0 or typed[j] != typed[j - 1]: |
| 58 | + return False |
| 59 | + j += 1 |
| 60 | + return i == len(name) |
| 61 | +``` |
| 62 | + |
| 63 | +### Java |
| 64 | + |
| 65 | +```java |
| 66 | +class Solution { |
| 67 | + public boolean isLongPressedName(String name, String typed) { |
| 68 | + int i = 0, j = 0; |
| 69 | + while (j < typed.length()) { |
| 70 | + if (i < name.length() && name.charAt(i) == typed.charAt(j)) { |
| 71 | + i++; |
| 72 | + } else if (j == 0 || typed.charAt(j) != typed.charAt(j - 1)) { |
| 73 | + return false; |
| 74 | + } |
| 75 | + j++; |
| 76 | + } |
| 77 | + return i == name.length(); |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +### C++ |
| 83 | + |
| 84 | +```cpp |
| 85 | +class Solution { |
| 86 | +public: |
| 87 | + bool isLongPressedName(string name, string typed) { |
| 88 | + int i = 0, j = 0; |
| 89 | + while (j < typed.length()) { |
| 90 | + if (i < name.length() && name[i] == typed[j]) { |
| 91 | + i++; |
| 92 | + } else if (j == 0 || typed[j] != typed[j - 1]) { |
| 93 | + return false; |
| 94 | + } |
| 95 | + j++; |
| 96 | + } |
| 97 | + return i == name.length(); |
| 98 | + } |
| 99 | +}; |
| 100 | +``` |
| 101 | +
|
| 102 | +### C |
| 103 | +
|
| 104 | +```c |
| 105 | +bool isLongPressedName(char * name, char * typed){ |
| 106 | + int i = 0, j = 0; |
| 107 | + while (typed[j] != '\0') { |
| 108 | + if (name[i] != '\0' && name[i] == typed[j]) { |
| 109 | + i++; |
| 110 | + } else if (j == 0 || typed[j] != typed[j - 1]) { |
| 111 | + return false; |
| 112 | + } |
| 113 | + j++; |
| 114 | + } |
| 115 | + return name[i] == '\0'; |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +### JavaScript |
| 120 | + |
| 121 | +```javascript |
| 122 | +var isLongPressedName = function(name, typed) { |
| 123 | + let i = 0, j = 0; |
| 124 | + while (j < typed.length) { |
| 125 | + if (i < name.length && name[i] === typed[j]) { |
| 126 | + i++; |
| 127 | + } else if (j === 0 || typed[j] !== typed[j - 1]) { |
| 128 | + return false; |
| 129 | + } |
| 130 | + j++; |
| 131 | + } |
| 132 | + return i === name.length; |
| 133 | +}; |
| 134 | +``` |
| 135 | + |
| 136 | +## Step-by-Step Algorithm |
| 137 | + |
| 138 | +1. Initialize two pointers, `i` for `name` and `j` for `typed`, both set to 0. |
| 139 | +2. Iterate through `typed` using pointer `j`. |
| 140 | +3. If `name[i]` matches `typed[j]`, increment both `i` and `j`. |
| 141 | +4. If `name[i]` does not match `typed[j]` and `typed[j]` is not a long press of `typed[j-1]`, return `False`. |
| 142 | +5. Increment `j` in all cases. |
| 143 | +6. After the loop, check if all characters in `name` were matched (`i == name.length`). Return `True` if they were, otherwise return `False`. |
| 144 | + |
| 145 | +## Conclusion |
| 146 | + |
| 147 | +This approach efficiently checks whether the `typed` string can be derived from the `name` string considering possible long presses. The two-pointer technique ensures that we traverse both strings in linear time, making the solution optimal and easy to understand. |
0 commit comments