-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path925.long-pressed-name.cpp
100 lines (99 loc) · 1.91 KB
/
925.long-pressed-name.cpp
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* @lc app=leetcode id=925 lang=cpp
*
* [925] Long Pressed Name
*
* https://leetcode.com/problems/long-pressed-name/description/
*
* algorithms
* Easy (44.35%)
* Total Accepted: 14.6K
* Total Submissions: 32.9K
* Testcase Example: '"alex"\n"aaleex"'
*
* 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:
*
*
* name.length <= 1000
* typed.length <= 1000
* The characters of name and typed are lowercase letters.
*
*
*
*
*
*
*
*
*
*
*
*/
class Solution {
public:
bool isLongPressedName(string name, string typed) {
int i = 0, j = 0;
while(i-1 < (int)name.size() && j < (int)typed.size()){
cout<<i<<":"<<j<<" ";
if(i < (int)name.size() && name[i] == typed[j])
i++, j++;
else if(name[i-1] == typed[j])
j++;
else
return false;
}
if(i == (int)name.size() && j == (int)typed.size())
return true;
return false;
}
};