Generate all permutations of a string that follow given constraints | Simple C ++ program to print all permutations of a string that follow given constraint ; Check if current permutation is valid ; Recursively generate all permutation ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void permute ( string & str , int l , int r ) { if ( l == r ) { if ( str . find ( " AB " ) == string :: npos ) cout << str << " ▁ " ; return ; } for ( int i = l ; i <= r ; i ++ ) { swap ( str [ l ] , str [ i ] ) ; permute ( str , l + 1 , r ) ; swap ( str [ l ] , str [ i ] ) ; } } int main ( ) { string str = " ABC " ; permute ( str , 0 , str . length ( ) - 1 ) ; return 0 ; }