forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPyramid Transition Matrix.java
56 lines (43 loc) · 1.57 KB
/
Pyramid Transition Matrix.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
53
54
55
56
class Solution {
HashMap<String, List<Character>> map = new HashMap<>();
HashMap<String, Boolean> dp = new HashMap<>();
public boolean pyramidTransition(String bottom, List<String> allowed) {
for(String s:allowed){
String sub = s.substring(0,2);
char c = s.charAt(2);
if(!map.containsKey(sub))
map.put(sub, new ArrayList<>());
map.get(sub).add(c);
}
return dfs(bottom, "", 0);
}
boolean dfs(String currBottom, String newBottom, int index){
if(currBottom.length()==1)
return true;
if(index+1>=currBottom.length())
return false;
String sub = currBottom.substring(index,index+2);
String state = currBottom+" "+newBottom+" "+index;
if(dp.containsKey(state))
return dp.get(state);
if(map.containsKey(sub)){
List<Character> letters = map.get(sub);
for(char c:letters){
if(index==currBottom.length()-2){
if(dfs(newBottom+c, "", 0))
{
dp.put(state, true);
return true;
}
}
else if(dfs(currBottom, newBottom+c, index+1))
{
dp.put(state, true);
return true;
}
}
}
dp.put(state, false);
return false;
}
}