Constructing the strings with all possible values of consecutive difference and comparing them with staring string S .
[ ... 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 ( ' ' ) + " " ) ; }