-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbacktracking.java
More file actions
51 lines (46 loc) · 1.27 KB
/
backtracking.java
File metadata and controls
51 lines (46 loc) · 1.27 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
package lec10;
// lexiography(dictionary)
import java.util.ArrayList;
import java.util.Arrays;
public class backtracking {
public static void main(String[] args) {
String str="abc";
int[] fre= freq(str);
System.out.println(Arrays.toString(fre));
permute("",fre,str.length());
}
public static int[] freq(String str){
// char chh = '0';
int[] ar = new int[26];
for (int i = 0; i <str.length() ; i++) {
char ch = str.charAt(i);
ar[ch-'a']++ ;
}
// for (int i = 0; i <ar.length ; i++) {
// int max = ar[0];
// if(max<ar[i]){
// chh = (char)(ar[i]+'a');
// }
// }
// System.out.println("max occur character");
// System.out.println(chh);
//// return ch;
return ar ;
}
public static void permute(String process, int fre[], int length){
if(length==0){
System.out.println(process);
return;
}
for (int i = 0; i <fre.length ; i++) {
if (fre[i]>0) {
fre[i]--;
permute(process+(char)(i+'a'),fre,length-1);
fre[i]++;
}
// if(i>2){
// break;
// } for debugging
}
}
}