-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmpty the string.cpp
More file actions
79 lines (74 loc) · 1.76 KB
/
Empty the string.cpp
File metadata and controls
79 lines (74 loc) · 1.76 KB
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
/*Given a String s, In one operation we can delete any occurences of "geek"
as a substring in the String. Find the number of delete operations you have to
perform to make the String Empty.
Example 1:
Input:
s = "gegeekek"
Output:
2
Explanation:
gegeekek
We can first delete the bold characters
which form "geek", and then delete the
remaning string "geek" again from the string.
So, we will need 2 operations
Example 2:
Input:
s = "geekkgee"
Output:
-1
Explanation:
It's not possible to make the String empty.*/
#include<bits/stdc++.h>
using namespace std;
/*method:(1)push all the elements into stack until we get char 'k'
(2)if we get 'k' then start popping e,e,g;
(3) again repeat the process until we get char 'k' again then repeat the process*/
int makeStringEmpty(string s)
{
int i;
stack<char> st;
i=0;
int c=0;
while(i<s.size())
{
while(i<s.size() && s[i]!='k')
{
st.push(s[i]);
i++;
}
if(i>=s.size()||s[i]!='k')
{
return -1;
}
if(st.empty() || st.top()!='e')
{
return -1;
}
st.pop();
if(st.empty() || st.top()!='e')
{
return -1;
}
st.pop();
if(st.empty() || st.top()!='g')
{
return -1;
}
st.pop();
c++;
i++;
}
if(!st.empty())
{
return -1;
}
cout<< c;
}
int main()
{
string s;
cout<<"\n enter the string:";
cin>>s;
makeStringEmpty(s);
}