-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutation of a string.java
More file actions
33 lines (31 loc) · 1022 Bytes
/
permutation of a string.java
File metadata and controls
33 lines (31 loc) · 1022 Bytes
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
public class PermuteString {
public static String swapString(String a, int i, int j) {
char[] b =a.toCharArray();
char ch;
ch = b[i];
b[i] = b[j];
b[j] = ch;
return String.valueOf(b);
}
public static void main(String[] args)
{
String str = "ABC";
int len = str.length();
System.out.println("All the permutations of the string are: ");
generatePermutation(str, 0, len);
}
public static void generatePermutation(String str, int start, int end)
{
if (start == end-1)
System.out.println(str);
else
{
for (int i = start; i < end; i++)
{
str = swapString(str,start,i);
generatePermutation(str,start+1,end);
str = swapString(str,start,i);
}
}
}
}