Check if a string can be formed from another string by at most X circular clockwise shifts | Function to check that the string s1 can be converted to s2 by clockwise circular shift of all characters of str1 atmost X times ; Check for all characters of the strings whether the difference between their ascii values is less than X or not ; If both characters are the same ; Condition to check if the difference less than 0 then find the circular shift by adding 26 to it ; If difference between their ASCII values exceeds X ; Driver Code ; Function Call
def isConversionPossible ( s1 , s2 , x ) : NEW_LINE INDENT n = len ( s1 ) NEW_LINE s1 = list ( s1 ) NEW_LINE s2 = list ( s2 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT diff = ord ( s2 [ i ] ) - ord ( s1 [ i ] ) NEW_LINE if diff == 0 : NEW_LINE INDENT continue NEW_LINE DEDENT if diff < 0 : NEW_LINE INDENT diff = diff + 26 NEW_LINE DEDENT if diff > x : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s1 = " you " NEW_LINE s2 = " ara " NEW_LINE x = 6 NEW_LINE result = isConversionPossible ( s1 , s2 , x ) NEW_LINE if result : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT