-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringPermutation.java
More file actions
38 lines (34 loc) · 933 Bytes
/
StringPermutation.java
File metadata and controls
38 lines (34 loc) · 933 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
34
35
36
37
38
package strings;
import java.util.Scanner;
public class StringPermutation {
public static String swapString(String a, int i, int j) {
char[] b =a.toCharArray();
char ch = b[i];
b[i] = b[j];
b[j] = ch;
return String.valueOf(b);
}
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);
}
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String : ");
String str = sc.nextLine();
int len = str.length();
System.out.println("All the permutations of the string are: ");
generatePermutation(str, 0, len);
}
}