This post is about generating permutation of a string using a java program.This program has a static method genPerm() method which is used for generation of permutation of the string. genPerm() method is recursive and takes two string arguments and prints the permutations of the second string.
Source Code:
public class GeneratePerm { /** * @param args */ public static void genPerm(String pString,String sb){ if(pString.length()==3) System.out.println(pString); for(int i=0;i<sb.length();i++){ genPerm(pString+sb.charAt(i),sb.replaceFirst(sb.charAt(i)+"","")); } } public static void main(String[] args) { String sb=new String("abc"); genPerm("",sb); } }
Output:abc
acb
bac
bca
cab
cba
No comments:
Post a Comment
Your comment may wait for moderation....