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