Group Shifted String | Total lowercase letter ; Method to a difference string for a given string . If string is " adf " then difference string will be " cb " ( first difference3 then difference 2 ) ; Representing the difference as char ; This string will be 1 less length than str ; Method for grouping shifted string ; map for storing indices of string which are in same group ; Iterating through map to print group ; Driver code
ALPHA = 26 NEW_LINE def getDiffString ( str ) : NEW_LINE INDENT shift = " " NEW_LINE for i in range ( 1 , len ( str ) ) : NEW_LINE INDENT dif = ( ord ( str [ i ] ) - ord ( str [ i - 1 ] ) ) NEW_LINE if ( dif < 0 ) : NEW_LINE INDENT dif += ALPHA NEW_LINE DEDENT shift += chr ( dif + ord ( ' a ' ) ) NEW_LINE DEDENT return shift NEW_LINE DEDENT def groupShiftedString ( str , n ) : NEW_LINE INDENT groupMap = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT diffStr = getDiffString ( str [ i ] ) NEW_LINE if diffStr not in groupMap : NEW_LINE INDENT groupMap [ diffStr ] = [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT groupMap [ diffStr ] . append ( i ) NEW_LINE DEDENT DEDENT for it in groupMap : NEW_LINE INDENT v = groupMap [ it ] NEW_LINE for i in range ( len ( v ) ) : NEW_LINE INDENT print ( str [ v [ i ] ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT str = [ " acd " , " dfg " , " wyz " , " yab " , " mop " , " bdfh " , " a " , " x " , " moqs " ] NEW_LINE n = len ( str ) NEW_LINE groupShiftedString ( str , n ) NEW_LINE