Skip to content

Commit 7f8c34b

Browse files
refactor 756
1 parent 66cf508 commit 7f8c34b

File tree

1 file changed

+44
-79
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+44
-79
lines changed

src/main/java/com/fishercoder/solutions/_756.java

+44-79
Original file line numberDiff line numberDiff line change
@@ -5,89 +5,54 @@
55
import java.util.List;
66
import java.util.Map;
77

8-
/**
9-
* 756. Pyramid Transition Matrix
10-
*
11-
* We are stacking blocks to form a pyramid. Each block has a color which is a one letter string, like `'Z'`.
12-
* For every block of color `C` we place not in the bottom row,
13-
* we are placing it on top of a left block of color `A` and right block of color `B`.
14-
* We are allowed to place the block there only if `(A, B, C)` is an allowed triple.
15-
* We start with a bottom row of bottom,
16-
* represented as a single string. We also start with a list of allowed triples allowed.
17-
* Each allowed triple is represented as a string of length 3.
18-
* Return true if we can build the pyramid all the way to the top, otherwise false.
19-
20-
Example 1:
21-
Input: bottom = "XYZ", allowed = ["XYD", "YZE", "DEA", "FFF"]
22-
Output: true
23-
Explanation:
24-
We can stack the pyramid like this:
25-
A
26-
/ \
27-
D E
28-
/ \ / \
29-
X Y Z
30-
31-
This works because ('X', 'Y', 'D'), ('Y', 'Z', 'E'), and ('D', 'E', 'A') are allowed triples.
32-
33-
Example 2:
34-
Input: bottom = "XXYX", allowed = ["XXX", "XXY", "XYX", "XYY", "YXZ"]
35-
Output: false
36-
Explanation:
37-
We can't stack the pyramid to the top.
38-
Note that there could be allowed triples (A, B, C) and (A, B, D) with C != D.
39-
40-
Note:
41-
bottom will be a string with length in range [2, 8].
42-
allowed will have length in range [0, 200].
43-
Letters in all strings will be chosen from the set {'A', 'B', 'C', 'D', 'E', 'F', 'G'}.
44-
*/
458
public class _756 {
46-
public static class Solution1 {
47-
/**credit: https://discuss.leetcode.com/topic/116042/java-solution-map-backtracking*/
48-
public boolean pyramidTransition(String bottom, List<String> allowed) {
49-
Map<String, List<String>> map = new HashMap<>();
50-
for (String s : allowed) {
51-
String key = s.substring(0, 2);
52-
if (!map.containsKey(key)) {
53-
map.put(key, new ArrayList<>());
9+
public static class Solution1 {
10+
/**
11+
* credit: https://discuss.leetcode.com/topic/116042/java-solution-map-backtracking
12+
*/
13+
public boolean pyramidTransition(String bottom, List<String> allowed) {
14+
Map<String, List<String>> map = new HashMap<>();
15+
for (String s : allowed) {
16+
String key = s.substring(0, 2);
17+
if (!map.containsKey(key)) {
18+
map.put(key, new ArrayList<>());
19+
}
20+
map.get(key).add(s.substring(2));
21+
}
22+
23+
return helper(bottom, map);
5424
}
55-
map.get(key).add(s.substring(2));
56-
}
5725

58-
return helper(bottom, map);
59-
}
60-
61-
private boolean helper(String bottom, Map<String, List<String>> map) {
62-
if (bottom.length() == 1) {
63-
return true;
64-
}
65-
for (int i = 0; i < bottom.length() - 1; i++) {
66-
if (!map.containsKey(bottom.substring(i, i + 2))) {
67-
return false;
68-
}
69-
}
70-
List<String> ls = new ArrayList<>();
71-
getList(bottom, 0, new StringBuilder(), ls, map);
72-
for (String s : ls) {
73-
if (helper(s, map)) {
74-
return true;
26+
private boolean helper(String bottom, Map<String, List<String>> map) {
27+
if (bottom.length() == 1) {
28+
return true;
29+
}
30+
for (int i = 0; i < bottom.length() - 1; i++) {
31+
if (!map.containsKey(bottom.substring(i, i + 2))) {
32+
return false;
33+
}
34+
}
35+
List<String> ls = new ArrayList<>();
36+
getList(bottom, 0, new StringBuilder(), ls, map);
37+
for (String s : ls) {
38+
if (helper(s, map)) {
39+
return true;
40+
}
41+
}
42+
return false;
7543
}
76-
}
77-
return false;
78-
}
7944

80-
private void getList(String bottom, int idx, StringBuilder sb, List<String> ls,
81-
Map<String, List<String>> map) {
82-
if (idx == bottom.length() - 1) {
83-
ls.add(sb.toString());
84-
return;
85-
}
86-
for (String s : map.get(bottom.substring(idx, idx + 2))) {
87-
sb.append(s);
88-
getList(bottom, idx + 1, sb, ls, map);
89-
sb.deleteCharAt(sb.length() - 1);
90-
}
45+
private void getList(String bottom, int idx, StringBuilder sb, List<String> ls,
46+
Map<String, List<String>> map) {
47+
if (idx == bottom.length() - 1) {
48+
ls.add(sb.toString());
49+
return;
50+
}
51+
for (String s : map.get(bottom.substring(idx, idx + 2))) {
52+
sb.append(s);
53+
getList(bottom, idx + 1, sb, ls, map);
54+
sb.deleteCharAt(sb.length() - 1);
55+
}
56+
}
9157
}
92-
}
9358
}

0 commit comments

Comments
 (0)