-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutation.java
More file actions
41 lines (36 loc) · 1.16 KB
/
permutation.java
File metadata and controls
41 lines (36 loc) · 1.16 KB
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
import java.util.ArrayList;
public class permutation {
public static void main(String[] args) {
// permut("","abc");
ArrayList<String> ans=permutlist(" ", "abc");
System.out.println(ans);
}
static void permut(String p, String up){
if(up.isEmpty()){
System.out.println(p);
return;
}
char ch=up.charAt(0);
for(int i=0;i<p.length();i++){
String fir=p.substring(0, i);
String end=p.substring(i,p.length());
permut(fir+ch+end, up.substring(1));
}
}
// returning all the permute in th earraylist
static ArrayList<String> permutlist(String p, String up){
if(up.isEmpty()){
ArrayList<String> list=new ArrayList<>();
list.add(p);
return list;
}
char ch=up.charAt(0);
ArrayList<String> ans=new ArrayList<>();
for(int i=0;i<p.length();i++){
String fir=p.substring(0, i);
String end=p.substring(i, p.length());
ans.addAll( permutlist(fir+ch+end, up.substring(1)));
}
return ans;
}
}