Delete consecutive same words in a sequence | C ++ implementation of above method ; Function to find the size of manipulated sequence ; Start traversing the sequence ; Push the current string if the stack is empty ; compare the current string with stack top if equal , pop the top ; Otherwise push the current string ; Return stack size ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int removeConsecutiveSame ( vector < string > v ) { stack < string > st ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { if ( st . empty ( ) ) st . push ( v [ i ] ) ; else { string str = st . top ( ) ; if ( str . compare ( v [ i ] ) == 0 ) st . pop ( ) ; else st . push ( v [ i ] ) ; } } return st . size ( ) ; } int main ( ) { vector < string > V = { " ab " , " aa " , " aa " , " bcd " , " ab " } ; cout << removeConsecutiveSame ( V ) ; return 0 ; }