-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpermutation.java
47 lines (39 loc) · 885 Bytes
/
permutation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package datastructures;
import bridgelabz.utility;
public class permutation
{
public class Permutation
{
public void main(String[] args)
{
System.out.println("enter the string");
String str = utility.getString();
int n = str.length();
Permutation permutation = new Permutation();
permutation.permute(str, 0, n-1);
}
private void permute(String str, int start, int end)
{
if (start == end)
System.out.println(str);
else
{
for (int i = start; i <= end; i++)
{
str = swap(str,start,i);
permute(str, start+1, end);
str = swap(str,start,i);
}
}
}
public String swap(String a, int i, int j)
{
char temp;
char[] charArray = a.toCharArray();
temp = charArray[i] ;
charArray[i] = charArray[j];
charArray[j] = temp;
return String.valueOf(charArray);
}
}
}