Delete consecutive same words in a sequence

Given a sequence of n strings, the task is to check if any two similar words come together then they destroy each other then print the number of words left in the sequence after this pairwise destruction.

Examples:

Input : ab aa aa bcd ab
Output : 3
As aa, aa destroys each other so, ab bcd ab is the
new sequence.
Input : tom jerry jerry tom
Output : 0
As first both jerry will destroy each other.
Then sequence will be tom, tom they will also destroy
each other. So, the final sequence doesn't contain any
word.

1- Start traversing the sequence by storing it in vector.
a) Check if the current string is equal to next string
if yes, erase both from the vector.
b) And check the same till last.
2- Return vector size.

1- Start traversing the strings and push into stack.
a) Check if the current string is same as the string
at the top of the stack
i) If yes, pop the string from top.
ii) Else push the current string.
2- Return stack size if the whole sequence is traversed.

This article is contributed by Sahil Chhabra. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.