-
Notifications
You must be signed in to change notification settings - Fork 2
/
Recursively remove all adjacent duplicates.cpp
140 lines (110 loc) · 3.11 KB
/
Recursively remove all adjacent duplicates.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
Given a string s, remove all its adjacent duplicate characters recursively.
Example 1:
Input:
S = "geeksforgeek"
Output: "gksforgk"
Explanation:
g(ee)ksforg(ee)k -> gksforgk​
Example 2:
Input:
S = "acaaabbbacdddd"
Output: "acac"
Explanation:
ac(aaa)(bbb)ac(dddd) -> acac
Your Task:
You don't need to read input or print anything. Your task is to complete the function remove() which takes the string S as input parameter and returns the resultant string.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(|S|)
Constraints:
1<=|S|<=105
*/
// C/C++ program to remove all
// adjacent duplicates from a string
#include <iostream>
#include <string.h>
using namespace std;
// Recursively removes adjacent
// duplicates from str and returns
// new string. las_removed is a
// pointer to last_removed character
char* removeUtil(char *str, char *last_removed)
{
// If length of string is 1 or 0
if (str[0] == '\0' || str[1] == '\0')
return str;
// Remove leftmost same characters
// and recur for remaining
// string
if (str[0] == str[1])
{
*last_removed = str[0];
while (str[1] && str[0] == str[1])
str++;
str++;
return removeUtil(str, last_removed);
}
// At this point, the first character
// is definiotely different
// from its adjacent. Ignore first
// character and recursively
// remove characters from remaining string
char* rem_str = removeUtil(str+1,
last_removed);
// Check if the first character
// of the rem_string matches with
// the first character of the
// original string
if (rem_str[0] && rem_str[0] == str[0])
{
*last_removed = str[0];
// Remove first character
return (rem_str+1);
}
// If remaining string becomes
// empty and last removed character
// is same as first character of
// original string. This is needed
// for a string like "acbbcddc"
if (rem_str[0] == '\0' &&
*last_removed == str[0])
return rem_str;
// If the two first characters
// of str and rem_str don't match,
// append first character of str
// before the first character of
// rem_str.
rem_str--;
rem_str[0] = str[0];
return rem_str;
}
// Function to remove
char *remove(char *str)
{
char last_removed = '\0';
return removeUtil(str, &last_removed);
}
// Driver program to test
// above functions
int main()
{
char str1[] = "geeksforgeeg";
cout << remove(str1) << endl;
char str2[] = "azxxxzy";
cout << remove(str2) << endl;
char str3[] = "caaabbbaac";
cout << remove(str3) << endl;
char str4[] = "gghhg";
cout << remove(str4) << endl;
char str5[] = "aaaacddddcappp";
cout << remove(str5) << endl;
char str6[] = "aaaaaaaaaa";
cout << remove(str6) << endl;
char str7[] = "qpaaaaadaaaaadprq";
cout << remove(str7) << endl;
char str8[] = "acaaabbbacdddd";
cout << remove(str8) << endl;
char str9[] = "acbbcddc";
cout << remove(str9) << endl;
return 0;
}