Check if there exists a permutation of given string which doesn 't contain any monotonous substring | Java implementation such that there are no monotonous string in given string ; 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
import java . io . * ; import java . util . * ; class GFG { static boolean check ( String s ) { boolean ok = true ; for ( int i = 0 ; i + 1 < s . length ( ) ; ++ i ) ok &= ( Math . abs ( s . charAt ( i ) - s . charAt ( i + 1 ) ) != 1 ) ; return ok ; } static String monotonousString ( String s ) { String odd = " " , even = " " ; for ( int i = 0 ; i < s . length ( ) ; ++ i ) { if ( s . charAt ( i ) % 2 == 0 ) odd += s . charAt ( i ) ; else even += s . charAt ( i ) ; } char oddArray [ ] = odd . toCharArray ( ) ; Arrays . sort ( oddArray ) ; odd = new String ( oddArray ) ; char evenArray [ ] = even . toCharArray ( ) ; Arrays . sort ( evenArray ) ; even = new String ( evenArray ) ; if ( check ( odd + even ) ) return " Yes " ; else if ( check ( even + odd ) ) return " Yes " ; return " No " ; } public static void main ( String [ ] args ) { String str = " abcd " ; String ans ; ans = monotonousString ( str ) ; System . out . println ( ans ) ; } }