-
Notifications
You must be signed in to change notification settings - Fork 568
Expand file tree
/
Copy pathRemove Consecutive Characters.cpp
More file actions
48 lines (41 loc) · 986 Bytes
/
Copy pathRemove Consecutive Characters.cpp
File metadata and controls
48 lines (41 loc) · 986 Bytes
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
/*
Remove Consecutive Characters
=============================
Given a string S delete the characters which are appearing more than once consecutively.
Example 1:
Input:
S = aabb
Output: ab
Explanation: 'a' at 2nd position is
appearing 2nd time consecutively.
Similiar explanation for b at
4th position.
Example 2:
Input:
S = aabaa
Output: aba
Explanation: 'a' at 2nd position is
appearing 2nd time consecutively.
'a' at fifth position is appearing
2nd time consecutively.
Your Task:
You dont need to read input or print anything. Complete the function removeConsecutiveCharacter() which accepts a string as input parameter and returns modified string.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=105
All characters are lowercase alphabets.
*/
string removeConsecutiveCharacter(string S)
{
string ans;
char prev = '\0';
for (auto &i : S)
{
if (prev == i)
continue;
ans += i;
prev = i;
}
return ans;
}