-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBruteForceSearch.java
More file actions
57 lines (48 loc) · 1.6 KB
/
BruteForceSearch.java
File metadata and controls
57 lines (48 loc) · 1.6 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
package LCS_LongestCommonSubsequence;
public class BruteForceSearch {
public static void plusOne(int[] binary) {
int size = binary.length - 1;
while(size >= 0 && binary[size] == 1) {
binary[size--] = 0;
}
if(size >= 0)
binary[size] = 1;
}
public static String[] subsets(String str) {
int array_size = ((int) (Math.pow(2, str.length())))-1;
String[] subsets = new String[array_size];
int[] binary = new int[str.length()];
for(int i = 0; i < subsets.length; i++) {
plusOne(binary);
String subset="";
for (int j = 0; j < binary.length; j++) {
if(binary[j] == 1)
subset+=str.charAt(j);
}
subsets[i] = subset;
}
return subsets;
}
public static String bruteForce(String X, String Y) {
String shortest = X, longest= Y, ans = "";
if(X.length() > Y.length()){
shortest = Y;
longest= X;
}
String[] shortestSet = subsets(shortest);
String[] longestSet = subsets(longest);
for(int i = 0; i < shortestSet.length; i++) {
for(int j = 0; j < longestSet.length; j++) {
if(shortestSet[i].equals(longestSet[j])) {
if(shortestSet[i].length() > ans.length())
ans = shortestSet[i];
}
}
}
return ans;
}
public static void main(String[] args) {
String X = "abcbdab", Y = "bdcaba";
System.out.println(bruteForce(X,Y));
}
}