Modify given string such that odd and even indices is lexicographically largest and smallest | Java program for the above approach ; Function to modify the given String satisfying the given criteria ; Traverse the String S ; If i is even ; If the S [ i ] is ' a ' , then change S [ i ] to ' b ' ; Otherwise , change S [ i ] to ' a ' ; If S [ i ] is ' z ' , then change S [ i ] to ' y ' ; Otherwise , change S [ i ] to ' z ' ; Return the result ; Driver Code
import java . util . * ; class GFG { static String performOperation ( char [ ] S , int N ) { for ( int i = 0 ; i < N ; i ++ ) { if ( i % 2 == 0 ) { if ( S [ i ] == ' a ' ) { S [ i ] = ' b ' ; } else { S [ i ] = ' a ' ; } } else { if ( S [ i ] == ' z ' ) { S [ i ] = ' y ' ; } else { S [ i ] = ' z ' ; } } } return String . valueOf ( S ) ; } public static void main ( String [ ] args ) { String S = " giad " ; int N = S . length ( ) ; System . out . print ( performOperation ( S . toCharArray ( ) , N ) ) ; } }