Remove three consecutive duplicates from string - GeeksforGeeks

06 Jul, 2021
Given a string, you have to remove the three consecutive duplicates from the string. If no three are consecutive then output the string as it is.Examples:  
Input : aabbbaccddddc
Output :ccdc

Input :aabbaccddc
Output :aabbaccddc
 
Explanation : We insert the characters of string one by one to vector and keep on checking the size of vector. If the size of vector is greater than 2, then we will check whether the last 3 characters of the string are same or not. If the characters are same then we will move three steps backwards in array using resize() else not.  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to remove three consecutive// duplicates#include <bits/stdc++.h>using namespace std; // function to remove three consecutive// duplicatesvoid remove3ConsecutiveDuplicates(string str){    vector<char> v;    for (int i = 0; i < str.size(); ++i) {        v.push_back(str[i]);         if (v.size() > 2) {            int sz = v.size();             // removing three consecutive duplicates            if (v[sz - 1] == v[sz - 2] &&                v[sz - 2] == v[sz - 3]) {                v.resize(sz - 3); // Removing three characters                                 // from the string            }        }    }     // printing the string final string    for (int i = 0; i < v.size(); ++i)        cout << v[i];} // driver codeint main(){    string str = "aabbbaccddddc";     remove3ConsecutiveDuplicates(str);    return 0;}
// Java program to remove three consecutive// duplicatesimport java.util.*; class GFG{ // function to remove three consecutive// duplicatesstatic void remove3ConsecutiveDuplicates(String str){    Vector<Character> v = new Vector<>();    for (int i = 0; i < str.length(); ++i)    {        v.add(str.charAt(i));         if (v.size() > 2)        {            int sz = v.size();             // removing three consecutive duplicates            if (v.get(sz - 1) == v.get(sz - 2) &&                v.get(sz - 2) == v.get(sz - 3))            {                v.setSize(sz - 3); // Removing three characters                                // from the string            }        }    }     // printing the string final string    for (int i = 0; i < v.size(); ++i)        System.out.print(v.get(i));} // Driver codepublic static void main(String[] args){    String str = "aabbbaccddddc";    remove3ConsecutiveDuplicates(str);}} // This code contributed by Rajput-Ji
# Python3 program to remove three consecutive duplicates # function to remove three consecutive duplicatesdef remove3ConsecutiveDuplicates(string):    val = ""    i = 0    while (i < len(string)):        if (i < len(string) - 2 and            string[i] * 3 == string[i:i + 3]):            i += 3        else:            val += string[i]            i += 1                 if (len(val) == len(string)):        return val    else:        return remove3ConsecutiveDuplicates(val) # Driver codestring = "aabbbaccddddc"val = remove3ConsecutiveDuplicates(string)print(val) # This code is contributed by# Shubham Singh(SHUBHAMSINGH10)
// C# program to remove three consecutive// duplicatesusing System;using System.Collections.Generic; class GFG{ // function to remove three consecutive// duplicatesstatic void remove3ConsecutiveDuplicates(String str){    List<char> v = new List<char>();    for (int i = 0; i < str.Length; ++i)    {        v.Add(str[i]);         if (v.Count > 2)        {            int sz = v.Count;             // removing three consecutive duplicates            if (v[sz - 1] == v[sz - 2] &&                v[sz - 2] == v[sz - 3])            {                v.RemoveRange(sz-3,3); // Removing three characters                                // from the string            }        }    }     // printing the string final string    for (int i = 0; i < v.Count; ++i)        Console.Write(v[i]);} // Driver codepublic static void Main(String[] args){    String str = "aabbbaccddddc";    remove3ConsecutiveDuplicates(str);}} // This code has been contributed by 29AjayKumar
<?php// PHP program to remove three consecutive// duplicates // function to remove three consecutive// duplicatesfunction remove3ConsecutiveDuplicates($str){    $v = array();    for ($i = 0; $i < strlen($str); ++$i)    {        array_push($v, $str[$i]);         if (count($v) > 2)        {            $sz = count($v);             // removing three consecutive duplicates            if ($v[$sz - 1] == $v[$sz - 2] &&                $v[$sz - 2] == $v[$sz - 3])            {                array_pop($v);                array_pop($v);                array_pop($v);                // Removing three characters                                // from the string            }        }    }     // printing the string final string    for ($i = 0; $i < count($v); ++$i)        echo $v[$i];}     // Driver code    $str = "aabbbaccddddc";     remove3ConsecutiveDuplicates($str); // This code is contributed by mits?>
<script>// Javascript program to remove three consecutive// duplicates // function to remove three consecutive// duplicatesfunction remove3ConsecutiveDuplicates(str){    let v = [];    for (let i = 0; i < str.length; ++i)    {        v.push(str[i]);           if (v.length > 2)        {            let sz = v.length;               // removing three consecutive duplicates            if (v[sz - 1] == v[sz - 2] &&                v[sz - 2] == v[sz - 3])            {                v.pop();                v.pop();                v.pop();                // Removing three characters                                // from the string            }        }    }       // printing the string final string    for (let i = 0; i < v.length; ++i)        document.write(v[i]);} // Driver codelet str = "aabbbaccddddc";remove3ConsecutiveDuplicates(str); // This code is contributed by rag2127</script>
Output:  
ccdc
This article is contributed by Roshni Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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. 
SHUBHAMSINGH10
Rajput-Ji
29AjayKumar
Mithun Kumar
rag2127
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Top 50 String Coding Problems for Interviews
Print all the duplicates in the input string
Vigenère Cipher
sprintf() in C
Convert character array to string in C++
Naive algorithm for Pattern Searching
Program to count occurrence of a given character in a string
How to Append a Character to a String in C
Boyer Moore Algorithm for Pattern Searching