-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpermutationsstring.java
More file actions
63 lines (50 loc) · 1.38 KB
/
permutationsstring.java
File metadata and controls
63 lines (50 loc) · 1.38 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package june30;
import java.util.ArrayList;
import java.util.Scanner;
public class permutationsstring {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
String str = s.next();
ArrayList<String> ans = permutations1(str);
System.out.println(ans.size());
System.out.println(ans);
permutations(str, "");
}
public static ArrayList<String> permutations1(String str) {
if (str.equals("")) {
ArrayList<String> bres = new ArrayList<>();
bres.add("");
return bres;
}
char ch = str.charAt(0);
String rs = str.substring(1);
ArrayList<String> list1 = permutations1(rs);
ArrayList<String> mres = new ArrayList<>();
for (String rstr : list1) {
for (int i = 0; i <= rstr.length(); i++) {
String left = rstr.substring(0, i);
String right = rstr.substring(i);
mres.add(left + ch + right);
}
}
return mres;
}
public static void permutations(String ques, String ans) {
if (ques.length() == 0) {
System.out.println(ans);
return;
}
boolean[] arr = new boolean[26];
for (int i = 0; i < ques.length(); i++) {
char ch = ques.charAt(i);
int j = ch - 'a';
if (arr[j]) {
continue;
}
arr[j] = true;
String ros = ques.substring(0, i) + ques.substring(i + 1);
permutations(ros, ans + ch);
}
}
}