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
def string_modify ( s ) : NEW_LINE INDENT alphabets = [ ] NEW_LINE flag , hold_i = 0 , 0 NEW_LINE hold_l = s [ 0 ] NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT alphabets . append ( chr ( i + ord ( ' a ' ) ) ) NEW_LINE DEDENT difference = set ( ) NEW_LINE reconstruct = " " NEW_LINE for i in range ( 1 , len ( s ) ) : NEW_LINE INDENT difference . add ( ord ( s [ i ] ) - ord ( s [ i - 1 ] ) ) NEW_LINE DEDENT if ( len ( difference ) == 1 ) : NEW_LINE INDENT print ( " No ▁ modifications ▁ required " ) NEW_LINE return NEW_LINE DEDENT for it in difference : NEW_LINE INDENT index = ord ( s [ 0 ] ) - ord ( ' a ' ) NEW_LINE reconstruct = " " NEW_LINE flag = 0 NEW_LINE i = 0 NEW_LINE while ( ( i < len ( s ) ) and ( flag <= 1 ) ) : NEW_LINE INDENT reconstruct += alphabets [ index ] NEW_LINE index += it NEW_LINE if ( index < 0 ) : NEW_LINE INDENT index += 26 NEW_LINE DEDENT index %= 26 NEW_LINE if ( reconstruct [ i ] != s [ i ] ) : NEW_LINE INDENT flag += 1 NEW_LINE hold_i = i NEW_LINE hold_l = s [ i ] NEW_LINE DEDENT i += 1 NEW_LINE DEDENT if ( flag == 1 ) : NEW_LINE INDENT s [ hold_i ] = reconstruct [ hold_i ] NEW_LINE break NEW_LINE DEDENT DEDENT if ( flag > 1 ) : NEW_LINE INDENT hold_i = 0 NEW_LINE hold_l = s [ 0 ] NEW_LINE temp = ( ord ( s [ 1 ] ) - ord ( ' a ' ) - ( ord ( s [ 2 ] ) - ord ( s [ 1 ] ) ) ) % 26 NEW_LINE if ( temp < 0 ) : NEW_LINE INDENT temp += 26 NEW_LINE DEDENT s [ 0 ] = alphabets [ temp ] NEW_LINE DEDENT print ( hold_i , " - > " , hold_l ) NEW_LINE print ( " " . join ( s ) ) NEW_LINE DEDENT s = list ( " aeimqux " ) NEW_LINE string_modify ( s ) NEW_LINE