-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlec6_4.cpp
57 lines (49 loc) Β· 1.11 KB
/
lec6_4.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
//{ Driver Code Starts
// Initial Template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
// User function Template for C++
class Solution {
public:
int removeConsecutiveSame(vector<string>& arr) {
// Your code goes here
stack<string>st;
for(int i = 0 ;i<arr.size();i++)
{
if(st.empty())
{
st.push(arr[i]);
}
else if(st.top() == arr[i])
{
st.pop();
}
else
{
st.push(arr[i]);
}
}
return st.size();
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
cin.ignore(); // to ignore the newline character after the integer input
while (t--) {
string line;
getline(cin, line);
stringstream ss(line);
vector<string> arr;
string s;
while (ss >> s) {
arr.push_back(s);
}
Solution ob;
cout << ob.removeConsecutiveSame(arr) << endl;
}
return 0;
}
// } Driver Code Ends