Skip to main content

Posts

Showing posts with the label permutation

Find all permutations of a string

 Ex:- Permutations of string "abc" are           "abc"  "acb"   "bac"  "bca"  "cab"  "cba"  Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class StrPermutation{ public static void main (String []args){ String str = "abc" ; permute(str, "" ); } public static void permute (String str, String ans){ if (str. length ()== 0 ){ System. out . print (ans+ " " ); return ; } for ( int i= 0 ; i<str. length (); i++){ char ch = str. charAt (i); String rem = str. substring ( 0 ,i) + str. substring (i+ 1 ); permute(rem, ans+ch); } } } Python def permute( str , ans): if len ( str ) == 0 : print (ans, end = " " ) return for i in range ( 0 , len ( s...