Reverse substrings of given string according to specified array indices | Function to perform the reversal operation on the given String ; Size of String ; Stores the count of indices ; Count the positions where reversals will begin ; Store the count of reversals beginning at position i ; Check if the count [ i ] is odd the swap the character ; Return the updated String ; Given String str ; Given array of reversing index ; Function Call
function modifyString ( A , str , K ) { str = str . split ( " " ) let N = str . length ; let count = new Array ( N + 1 ) . fill ( 0 ) ; for ( let i = 0 ; i < K ; i ++ ) { count [ A [ i ] ] += 1 ; } for ( let i = 1 ; i <= N / 2 ; i ++ ) { count [ i ] = count [ i ] + count [ i - 1 ] ; if ( count [ i ] & 1 ) { let temp = str [ i - 1 ] ; str [ i - 1 ] = str [ N - i ] ; str [ N - i ] = temp } } return str . join ( " " ) ; } let str = " " ; let arr = [ 1 , 2 , 3 ] ; let K = arr . length ; document . write ( modifyString ( arr , str , K ) ) ;