forked from pratyushmp/code_opensource_2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringPermutation.java
More file actions
42 lines (34 loc) · 981 Bytes
/
StringPermutation.java
File metadata and controls
42 lines (34 loc) · 981 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
39
40
41
42
import java.util.ArrayList;
import java.util.Scanner;
public class StringPermutation {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String input = sc.next();
permute(input);
}
private static void permute(String input) {
int count = input.length();
ArrayList<String> stringPermut = new ArrayList<String>();
int max = 1 << count;
input = input.toLowerCase();
for (int i = 0; i < max; i++) {
char combination[] = input.toCharArray();
for (int j = 0; j < count; j++) {
if (!(combination[j] >= 'a' && combination[j] <= 'z')
&& !(combination[j] >= 'A' && combination[j] <= 'Z')) {
continue;
}
if(((i >> j) & 1) == 1) {
combination[j] = (char) (combination[j]-32);
}
}
String str = new String(combination);
if(!stringPermut.contains(str)) {
stringPermut.add(str);
}
}
for(String s: stringPermut) {
System.out.print(s+"\t");
}
}
}