-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPatternMatcher.java
52 lines (50 loc) · 1.58 KB
/
PatternMatcher.java
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
import java.util.*;
class Program {
// O(C(s, p)) time | O(max(p, s)) space - where p is the length of pattern and s is the length of string
public static String[] patternMatcher(String pattern, String str) {
// Write your code here.
if (pattern.length() > str.length()) {
return new String[] {};
}
Map<Character, String> patternMap = new HashMap<>();
Set<String> mappedStrings = new HashSet<>();
if (isMatch(0, pattern, 0, str, patternMap, mappedStrings)) {
String[] res = new String[2];
res[0] = (patternMap.containsKey('x')) ? patternMap.get('x') : "";
res[1] = (patternMap.containsKey('y')) ? patternMap.get('y') : "";
return res;
}
return new String[] {};
}
private static boolean isMatch(int i, String pattern, int j, String str, Map<Character, String> patternMap, Set<String> mappedStrings) {
if (i == pattern.length() && j == str.length()) {
return true;
}
if (i >= pattern.length() || j >= str.length()) {
return false;
}
char ch = pattern.charAt(i);
if (patternMap.containsKey(ch)) {
String s = patternMap.get(ch);
if (!str.startsWith(s, j)) {
return false;
}
return isMatch(i + 1, pattern, j + s.length(), str, patternMap, mappedStrings);
} else {
for (int k = j + 1; k <= str.length(); k++) {
String s = str.substring(j, k);
if (mappedStrings.contains(s)) {
continue;
}
patternMap.put(ch, s);
mappedStrings.add(s);
if (isMatch(i + 1, pattern, k, str, patternMap, mappedStrings)) {
return true;
}
patternMap.remove(ch);
mappedStrings.remove(s);
}
return false;
}
}
}