-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeleteSameSequence.cpp
More file actions
42 lines (36 loc) · 976 Bytes
/
Copy pathdeleteSameSequence.cpp
File metadata and controls
42 lines (36 loc) · 976 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
// C++ implementation of above method
#include<bits/stdc++.h>
using namespace std;
// Function to find the size of manipulated sequence
int removeConsecutiveSame(vector <string> v)
{
stack<string> st;
// Start traversing the sequence
for (int i=0; i<v.size(); i++)
{
// Push the current string if the stack
// is empty
if (st.empty())
st.push(v[i]);
else
{
string str = st.top();
// compare the current string with stack top
// if equal, pop the top
if (str.compare(v[i]) == 0)
st.pop();
// Otherwise push the current string
else
st.push(v[i]);
}
}
// Return stack size
return st.size();
}
// Driver code
int main()
{
vector<string> V = { "ab", "aa", "aa", "bcd", "ab"};
cout << removeConsecutiveSame(V);
return 0;
}