Modify a string by performing given shift operations | Java implementataion of above approach ; Function to find the string obtained after performing given shift operations ; If shift [ i ] [ 0 ] = 0 , then left shift Otherwise , right shift ; Stores length of the string ; Effective shift calculation ; Stores modified string ; Right rotation ; Left rotation ; Driver Code
import java . io . * ; class GFG { static void stringShift ( String s , int [ ] [ ] shift ) { int val = 0 ; for ( int i = 0 ; i < shift . length ; ++ i ) if ( shift [ i ] [ 0 ] == 0 ) val -= shift [ i ] [ 1 ] ; else val += shift [ i ] [ 1 ] ; int len = s . length ( ) ; val = val % len ; String result = " " ; if ( val > 0 ) result = s . substring ( len - val , ( len - val ) + val ) + s . substring ( 0 , len - val ) ; else result = s . substring ( - val , len + val ) + s . substring ( 0 , - val ) ; System . out . println ( result ) ; } public static void main ( String [ ] args ) { String s = " abc " ; int [ ] [ ] shift = new int [ ] [ ] { { 0 , 1 } , { 1 , 2 } } ; stringShift ( s , shift ) ; } }