Wednesday, 11 January 2017

Program to print the Permutation of a given string or number

public class Permutation3 {
public static void main(String args[])
{
permutation(“123”);
}
public static void permutation(String input)
{
permutation(“”,input);
}
private static void permutation(String perm, String word)
{
if (word.isEmpty())
System.out.println(perm + word);
}
else
{
for (int i = 0; i< word.length(); i++)
{
permutation(perm + word.charAt(i), word.substring(0, i) + word.substring(i + 1, word.length()));
}
}
}
}
Output :- 
123
132
213
231
312
321

No comments:

Post a Comment