-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq16p18.java
More file actions
37 lines (36 loc) · 1.24 KB
/
Copy pathq16p18.java
File metadata and controls
37 lines (36 loc) · 1.24 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
public class q16p18{
public static void main(String[] args){
String value = "catcatgocatgo";
String pattern = "sb";
System.out.println("match >> "+ match(value, pattern));
}
public static boolean match(String value, String pattern){
int len = value.length();
for(int i = 0; i<len; i++){
String valueA = value.substring(0, i);
for(int j = i; j<=len; j++){
for(int k = j; k<=len; k++){
String valueB = value.substring(j,k);
String cand = build(pattern, valueA, valueB);
System.out.println("A: "+ valueA);
System.out.println("B: "+ valueB);
System.out.println(cand);
if(cand.equals(value))
return true;
}
}
}
return false;
}
public static String build(String pattern, String valueA, String valueB){
StringBuilder sb = new StringBuilder();
char a = pattern.charAt(0);
for(char c: pattern.toCharArray()){
if(c == a)
sb.append(valueA);
else
sb.append(valueB);
}
return sb.toString();
}
}