# Change the given string according to the given conditions

Given a string S, the task is to change the string id it doesn’t follow any of the rules given below and print the updated string. The rules for the proofreading are:

1. If there are three consecutive characters, then its a wrong spell. Remove one of the character. For Example: string “ooops” can be change to “oops”.
2. If two pairs of same character (AABB) are connected together, it’s a wrong spell. Delete one of the character of the second pair.For Example: string “helloo” can be changed to “hello”.
3. The rules follow the priority from left to right.

Examples:

Input: S = “helloo”
Output: hello
Explanation:
As per the Rule #2
helloo => hello

Input: S = “woooow”
Output: woow
Explanation:
As per the Rule #2
woooow => wooow
As per the Rule #1
wooow => woow

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The idea is to traverse the string and if there is a wrong spelling, remove the extra characters according to the given conditions. As the priority of errors is from left to right, and according to the rules given, it can be seen that the judgment of spelling errors will not conflict. Consider traversing from left to right, adding the already legal characters to the result. Below are the steps:

• Initialize a stack to store the characters and to compare the last characters of the string.
• Traverse the string and add the character to the stack.
• Check the last 3 characters of the stack, if the same then pop the character at the top of the stack.
• Check the last 4 characters of the stack, if the same then pop the character at the top of the stack.
• Finally return the characters of the stack.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to proofread the spells ` `string proofreadSpell(string& str) ` `{ ` `    ``vector<``char``> result; ` ` `  `    ``// Loop to iterate over the ` `    ``// characters of the string ` `    ``for` `(``char` `c : str) { ` ` `  `        ``// Push the current character c ` `        ``// in the stack ` `        ``result.push_back(c); ` ` `  `        ``int` `n = result.size(); ` ` `  `        ``// Check for Rule 1 ` `        ``if` `(n >= 3) { ` `            ``if` `(result[n - 1] ` `                    ``== result[n - 2] ` `                ``&& result[n - 1] ` `                       ``== result[n - 3]) { ` `                ``result.pop_back(); ` `            ``} ` `        ``} ` `        ``n = result.size(); ` ` `  `        ``// Check for Rule 2 ` `        ``if` `(n >= 4) { ` `            ``if` `(result[n - 1] ` `                    ``== result[n - 2] ` `                ``&& result[n - 3] ` `                       ``== result[n - 4]) { ` `                ``result.pop_back(); ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// To store the resultant string ` `    ``string resultStr = ``""``; ` ` `  `    ``// Loop to iterate over the ` `    ``// characters of stack ` `    ``for` `(``char` `c : result) { ` `        ``resultStr += c; ` `    ``} ` ` `  `    ``// Return the resultant string ` `    ``return` `resultStr; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``// Given string str ` `    ``string str = ``"helloo"``; ` ` `  `    ``// Function Call ` `    ``cout << proofreadSpell(str); ` `} `

## Java

 `// Java program for the above approach  ` `import` `java.util.*; ` ` `  `class` `GFG{ ` `     `  `// Function to proofread the spells  ` `public` `static` `String proofreadSpell(String str)  ` `{  ` `    ``Vector result = ``new` `Vector();  ` ` `  `    ``// Loop to iterate over the  ` `    ``// characters of the string  ` `    ``for``(``int` `i = ``0``; i < str.length(); i++)  ` `    ``{  ` `         `  `        ``// Push the current character c  ` `        ``// in the stack  ` `        ``result.add(str.charAt(i));  ` ` `  `        ``int` `n = result.size();  ` ` `  `        ``// Check for Rule 1  ` `        ``if` `(n >= ``3``)  ` `        ``{  ` `            ``if` `(result.get(n - ``1``) ==  ` `                ``result.get(n - ``2``) &&  ` `                ``result.get(n - ``1``) ==  ` `                ``result.get(n - ``3``)) ` `            ``{  ` `                ``result.remove(result.size() - ``1``);  ` `            ``}  ` `        ``}  ` `        ``n = result.size();  ` ` `  `        ``// Check for Rule 2  ` `        ``if` `(n >= ``4``)  ` `        ``{  ` `            ``if` `(result.get(n - ``1``) ==  ` `                ``result.get(n - ``2``) &&  ` `                ``result.get(n - ``3``) ==  ` `                ``result.get(n - ``4``)) ` `            ``{  ` `                ``result.remove(result.size() - ``1``);  ` `            ``}  ` `        ``}  ` `    ``}  ` ` `  `    ``// To store the resultant string  ` `    ``String resultStr = ``""``;  ` ` `  `    ``// Loop to iterate over the  ` `    ``// characters of stack  ` `    ``for``(Character c : result) ` `    ``{  ` `        ``resultStr += c;  ` `    ``}  ` `     `  `    ``// Return the resultant string  ` `    ``return` `resultStr;  ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` `     `  `    ``// Given string str  ` `    ``String str = ``"helloo"``;  ` ` `  `    ``// Function call  ` `    ``System.out.println(proofreadSpell(str)); ` `} ` `} ` ` `  `// This code is contributed by divyeshrabadiya07 `

## C#

 `// C# program for the above approach   ` `using` `System; ` `using` `System.Collections;  ` `using` `System.Collections.Generic; ` ` `  `class` `GFG{ ` ` `  `// Function to proofread the spells ` `static` `string` `proofreadSpell(``string` `str) ` `{ ` `     `  `    ``// ArrayList result=new ArrayList(); ` `    ``List<``char``> result = ``new` `List<``char``>(); ` `     `  `    ``// Loop to iterate over the ` `    ``// characters of the string ` `    ``foreach``(``char` `c ``in` `str) ` `    ``{ ` ` `  `        ``// Push the current character c ` `        ``// in the stack ` `        ``result.Add(c); ` ` `  `        ``int` `n = result.Count; ` ` `  `        ``// Check for Rule 1 ` `        ``if` `(n >= 3)  ` `        ``{ ` `            ``if` `(result[n - 1] == result[n - 2] &&  ` `                ``result[n - 1] == result[n - 3]) ` `            ``{ ` `                ``result.RemoveAt(n - 1); ` `            ``} ` `        ``} ` `         `  `        ``n = result.Count; ` ` `  `        ``// Check for Rule 2 ` `        ``if` `(n >= 4)  ` `        ``{ ` `            ``if` `(result[n - 1] == result[n - 2] &&  ` `                ``result[n - 3] == result[n - 4])  ` `            ``{ ` `                ``result.RemoveAt(n - 1); ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// To store the resultant string ` `    ``string` `resultStr = ``""``; ` ` `  `    ``// Loop to iterate over the ` `    ``// characters of stack ` `    ``foreach``(``char` `c ``in` `result) ` `    ``{ ` `        ``resultStr += c; ` `    ``} ` ` `  `    ``// Return the resultant string ` `    ``return` `resultStr; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(``string``[] args) ` `{ ` `     `  `    ``// Given string str ` `    ``string` `str = ``"helloo"``; ` ` `  `    ``// Function call ` `    ``Console.Write(proofreadSpell(str)); ` `} ` `} ` ` `  `// This code is contributed by rutvik_56 `

Output:

```hello
```

Time Complexity: O(N)
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.