Check if there exists a permutation of given string which doesn 't contain any monotonous substring | Function to check a string doesn 't contains a monotonous substring ; Loop to iterate over the string and check that it doesn 't contains  the monotonous substring ; Function to check that there exist a arrangement of string such that it doesn 't contains monotonous substring ; Loop to group the characters of the string into two buckets ; Sorting the two buckets ; Condition to check if the concatenation point doesn 't  contains the monotonous string ; Driver Code
def check ( s ) : NEW_LINE INDENT ok = True NEW_LINE for i in range ( 0 , len ( s ) - 1 , 1 ) : NEW_LINE INDENT ok = ( ok & ( abs ( ord ( s [ i ] ) - ord ( s [ i + 1 ] ) ) != 1 ) ) NEW_LINE DEDENT return ok NEW_LINE DEDENT def monotonousString ( s ) : NEW_LINE INDENT odd = " " NEW_LINE even = " " NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( ord ( s [ i ] ) % 2 == 0 ) : NEW_LINE INDENT odd += s [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT even += s [ i ] NEW_LINE DEDENT DEDENT odd = list ( odd ) NEW_LINE odd . sort ( reverse = False ) NEW_LINE odd = str ( odd ) NEW_LINE even = list ( even ) NEW_LINE even . sort ( reverse = False ) NEW_LINE even = str ( even ) NEW_LINE if ( check ( odd + even ) ) : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT elif ( check ( even + odd ) ) : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT return " No " NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " abcd " NEW_LINE ans = monotonousString ( str1 ) NEW_LINE print ( ans ) NEW_LINE DEDENT