Delete consecutive same words in a sequence

1.8

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.

Method 1:

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.

C++

// C++ program to remove consecutive same words
#include<bits/stdc++.h>
using namespace std;

// Function to find the size of manipulated sequence
int removeConsecutiveSame(vector <string > v)
{
    int n = v.size();

    // Start traversing the sequence
    for (int i=0; i<n-1; )
    {
        // Compare the current string with next one
        // Erase both if equal
        if (v[i].compare(v[i+1]) == 0)
        {
            // Erase function delete the element and
            // also shifts other element that's why
            // i is not updated
            v.erase(v.begin()+i);
            v.erase(v.begin()+i);

            // Update i, as to check from previous
            // element again
            if (i > 0)
                i--;

            // Reduce sequence size
            n = n-2;
        }

        // Increment i, if not equal
        else
            i++;
    }

    // Return modified size
    return v.size();
}

// Driver code
int main()
{
    vector<string> v = { "tom", "jerry", "jerry", "tom"};
    cout << removeConsecutiveSame(v);
    return 0;
}

Java

// Java program to remove consecutive same words

import java.util.Vector;

class Test
{
	// Method to find the size of manipulated sequence
	static int removeConsecutiveSame(Vector <String > v)
	{
	    int n = v.size();
	 
	    // Start traversing the sequence
	    for (int i=0; i<n-1; )
	    {
	        // Compare the current string with next one
	        // Erase both if equal
	        if (v.get(i).equals(v.get(i+1)))
	        {
	            // Erase function delete the element and
	            // also shifts other element that's why
	            // i is not updated
	        	v.remove(i);
	        	v.remove(i);
	 
	            // Update i, as to check from previous
	            // element again
	            if (i > 0)
	                i--;
	 
	            // Reduce sequence size
	            n = n-2;
	        }
	 
	        // Increment i, if not equal
	        else
	            i++;
	    }
	 
	    // Return modified size
	    return v.size();
	}
	
    // Driver method
    public static void main(String[] args) 
    {
    	Vector<String> v = new Vector<>();

    	v.addElement("tom"); v.addElement("jerry");
    	v.addElement("jerry");v.addElement("tom");

        System.out.println(removeConsecutiveSame(v));
    }
}


Output:

0

 

Method 2:(Using Stack)

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.

C++

//  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;
}

Java

//  Java implementation of above method

import java.util.Stack;
import java.util.Vector;

class Test
{
	// Method to find the size of manipulated sequence
	static int removeConsecutiveSame(Vector <String> v)
	{
	    Stack<String> st = new Stack<>();
	 
	    // 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.get(i));
	        else
	        {
	            String str = st.peek();
	 
	            // compare the current string with stack top
	            // if equal, pop the top
	            if (str.equals(v.get(i)))	
	                st.pop();
	 
	            // Otherwise push the current string
	            else
	                st.push(v.get(i));
	        }
	    }
	 
	    // Return stack size
	    return st.size();
	}
	
    // Driver method
    public static void main(String[] args) 
    {
    	Vector<String> v = new Vector<>();
    	
    	v.addElement("ab"); v.addElement("aa");
    	v.addElement("aa");v.addElement("bcd");
    	v.addElement("ab");
    	
        System.out.println(removeConsecutiveSame(v));
    }
}


Output:

3

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.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



1.8 Average Difficulty : 1.8/5.0
Based on 8 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.