-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1544. Make The String Great.cpp
69 lines (65 loc) · 1.37 KB
/
1544. Make The String Great.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
// Stack
// Time complexity - O(N)
// Space complexity- O(N)
class Solution {
public:
string makeGood(string s) {
stack<char> st;
for(auto it:s)
{
int asic=int(it);
if(!st.empty() and (int(st.top())==asic+32 or int(st.top())==asic-32))
st.pop();
else
st.push(it);
}
string ans="";
while(!st.empty())
{
ans+=st.top();
st.pop();
}
reverse(ans.begin(),ans.end());
return ans;
}
};
// Iterative
// Time complexity - O(N)
// Space complexity- O(1)
class Solution {
public:
string makeGood(string s) {
int i=0;
int j=1;
while(j<s.length())
{
if(int(s[i])==int(s[j])+32 or int(s[i])==int(s[j])-32)
{
s[i]='#';
s[j]='#';
i--;
while(i>=0 and s[i]=='#')
i--;
if(i<0)
{
i=j+1;
j+=2;
}
else
j++;
}
else
{
i=j;
j++;
}
}
string ans="";
for(auto it:s)
{
if(it!='#')
ans+=it;
}
return ans;
}
};