-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathStringPermutation.java
52 lines (42 loc) · 1.03 KB
/
StringPermutation.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
48
49
50
51
52
/*package whatever //do not write package name here */
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static void main (String[] args) {
//code
Scanner sc=new Scanner(System.in);
int tc=sc.nextInt();
for(int t=0;t<tc;t++){
List<String> set=new ArrayList<>();
String s=sc.next();
permute(s,0,s.length()-1,set);
Collections.sort(set);
for(String ans:set){
System.out.print(ans+" ");
}
System.out.println();
}
}
static void permute(String s,int l,int r,List<String> set){
if(l==r){
//System.out.print(s+" ");
set.add(s);
}
else{
for(int i=l;i<=r;i++){
s=swap(s,l,i);
permute(s,l+1,r,set);
s=swap(s,l,i);
}
}
}
public static String swap(String a,int i,int j){
char temp;
char[] arr=a.toCharArray();
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
return String.valueOf(arr);
}
}