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:
- 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”.
- 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”.
- The rules follow the priority from left to right.
Input: S = “helloo”
As per the Rule #2
helloo => hello
Input: S = “woooow”
As per the Rule #2
woooow => wooow
As per the Rule #1
wooow => woow
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:
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.
- Expand the string according to the given conditions
- Find the last remaining Character in the Binary String according to the given conditions
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Minimum number of substrings the given string can be splitted into that satisfy the given conditions
- Generate a String from given Strings P and Q based on the given conditions
- Sort a string according to the order defined by another string
- Reverse the substrings of the given String according to the given Array of indices
- Generate a string consisting of characters 'a' and 'b' that satisfy the given conditions
- Construct the Cypher string based on the given conditions
- Generate a string from an array of alphanumeric strings based on given conditions
- Sum of indices of Characters removed to obtain an Empty String based on given conditions
- Decrypt a string according to given rules
- Decrypt the String according to given algorithm
- Encryption and Decryption of String according to given technique
- Rearrange a string according to the given indices
- Generate an array B from the given array A which satisfies the given conditions
- Number of K's such that the given array can be divided into two sets satisfying the given conditions
- Find an integer in the given range that satisfies the given conditions
- Generate an array using given conditions from a given array
- Sort an array of strings according to string lengths
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.