Generate all permutations of a string that follow given constraints | Simple Java program to print all permutations of a String that follow given constraint ; Check if current permutation is valid ; Recursively generate all permutation ; Driver Code
import java . util . * ; class GFG { static void permute ( char [ ] str , int l , int r ) { if ( l == r ) { if ( ! String . valueOf ( str ) . contains ( " AB " ) ) System . out . print ( String . valueOf ( str ) + " ▁ " ) ; return ; } for ( int i = l ; i <= r ; i ++ ) { char tmp = str [ l ] ; str [ l ] = str [ i ] ; str [ i ] = tmp ; permute ( str , l + 1 , r ) ; tmp = str [ l ] ; str [ l ] = str [ i ] ; str [ i ] = tmp ; } } public static void main ( String [ ] args ) { String str = " ABC " ; permute ( str . toCharArray ( ) , 0 , str . length ( ) - 1 ) ; } }