import java.io.*; class Combination { public static void main(String args[]) throws IOException { Combination call = new Combination(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter a String : "); String n = br.readLine(); call.comb("",n); } public void comb(String beg, String end) throws IOException { if (end.length()<=1) { System.out.println(beg+end); } else { for (int i=0;i<end.length();i++) { String n = end.substring(0,i)+end.substring(i+1); comb(beg + end.charAt(i),n); } } } }
ALGORITHM:-
1. Start
2. Accept a string from the user.
3. Using recursion find all the combinations of the string.
4. Print the combinations.
5. End
OUTPUT:-
Enter a String : TOP
TOP
TPO
OTP
OPT
PTO
POT
Related Programs:-
★ Convert Decimal to Octal
★ Convert Decimal to Binary
★ Print the Factorial of a number using recursion
★ Print the possible combination of a string
★ Generate Fibonacci series using recursion
I think this is called Permutations not combinations because in combintation order does not matter.
ReplyDeleterefer - http://www.mathsisfun.com/combinatorics/combinations-permutations.html
Combination for TOP would be T, TO, TOP, TP, OP, OP, P
Thanks for correction.
Deletecan you explain me tge line
ReplyDeletepublic void comb(string beg,string end)