Make the string in AP by changing a character | Function to modify the given string and find the index where modification is needed ; Array to store the ASCII values of alphabets ; loop to compute the ASCII values of characters a - z ; Set to store all the possible differences between consecutive elements ; Loop to find out the differences between consecutive elements and storing them in the set ; Checks if any character of the string disobeys the pattern ; Constructing the strings with all possible values of consecutive difference and comparing them with staring string S . ; Driver Code
function string_modify ( s ) { var alphabets = Array ( 26 ) . fill ( 0 ) ; var flag = 0 , hold_i ; var hold_l ; var i ; for ( i = 0 ; i < 26 ; i ++ ) { alphabets [ i ] = String . fromCharCode ( i + ' ' . charCodeAt ( 0 ) ) ; } var difference = new Set ( ) ; var reconstruct = " " ; for ( var i = 1 ; i < s . length ; i ++ ) { difference . add ( s [ i ] . charCodeAt ( 0 ) - s [ i - 1 ] . charCodeAt ( 0 ) ) ; } if ( difference . size == 1 ) { document . write ( " " ) ; return ; } [ ... difference ] . sort ( ( a , b ) => a - b ) . forEach ( it => { if ( flag != 1 ) { var index = s [ 0 ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; reconstruct = " " ; flag = 0 ; for ( var i = 0 ; i < s . length && flag <= 1 ; i ++ ) { reconstruct += alphabets [ index ] ; index += it ; if ( index < 0 ) { index += 26 ; } index %= 26 ; if ( reconstruct [ i ] != s [ i ] ) { flag ++ ; hold_i = i ; hold_l = s [ i ] ; } } if ( flag == 1 ) { s [ hold_i ] = reconstruct [ hold_i ] ; } } } ) ; if ( flag > 1 ) { hold_i = 0 ; hold_l = s [ 0 ] ; var temp = ( s [ 1 ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) - ( s [ 2 ] . charCodeAt ( 0 ) - s [ 1 ] . charCodeAt ( 0 ) ) ) % 26 ; if ( temp < 0 ) { temp += 26 ; } s [ 0 ] = alphabets [ temp ] ; } document . write ( hold_i + " " + hold_l + " " + s . join ( ' ' ) + " " ) ; } var s = " " ; string_modify ( s . split ( ' ' ) ) ;