Constructing the strings with all possible values of consecutive difference and comparing them with staring string S .
for ( auto it = difference . begin ( ) ; it != difference . end ( ) ; it ++ ) { int index = s [ 0 ] - ' a ' ; reconstruct = " " ; flag = 0 ; for ( int i = 0 ; i < s . size ( ) && 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 ] ; break ; } } if ( flag > 1 ) { hold_i = 0 ; hold_l = s [ 0 ] ; int temp = ( s [ 1 ] - ' a ' - ( s [ 2 ] - s [ 1 ] ) ) % 26 ; if ( temp < 0 ) { temp += 26 ; } s [ 0 ] = alphabets [ temp ] ; } cout << hold_i << " ▁ - > ▁ " << hold_l << endl << s << endl ; }