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
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static string proofreadSpell ( string str ) { ArrayList result = new ArrayList ( ) ; List < char > result = new List < char > ( ) ; foreach ( char c in str ) { result . Add ( c ) ; int n = result . Count ; if ( n >= 3 ) { if ( result [ n - 1 ] == result [ n - 2 ] && result [ n - 1 ] == result [ n - 3 ] ) { result . RemoveAt ( n - 1 ) ; } } n = result . Count ; if ( n >= 4 ) { if ( result [ n - 1 ] == result [ n - 2 ] && result [ n - 3 ] == result [ n - 4 ] ) { result . RemoveAt ( n - 1 ) ; } } } string resultStr = " " ; foreach ( char c in result ) { resultStr += c ; } return resultStr ; } public static void Main ( string [ ] args ) { string str = " hello " ; Console . Write ( proofreadSpell ( str ) ) ; } }