Change the given string according to the given conditions | Java 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
import java . util . * ; class GFG { public static String proofreadSpell ( String str ) { Vector < Character > result = new Vector < Character > ( ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { result . add ( str . charAt ( i ) ) ; int n = result . size ( ) ; 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 ( ) ; 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 ) ; } } } String resultStr = " " ; for ( Character c : result ) { resultStr += c ; } return resultStr ; } public static void main ( String [ ] args ) { String str = " hello " ; System . out . println ( proofreadSpell ( str ) ) ; } }