You are stacking blocks to form a pyramid. Each block has a color, which is represented by a single letter. Each row of blocks contains one less block than the row beneath it and is centered on top.
To make the pyramid aesthetically pleasing, there are only specific
triangular patterns that are allowed. A triangular pattern consists of a
single block stacked on top of two blocks. The patterns are given as a list of three-letter strings allowed, where the first two characters of a pattern represent the left and right bottom blocks respectively, and the third character is the top block.
For example, "ABC" represents a triangular pattern with a 'C' block stacked on top of an 'A' (left) and 'B' (right) block. Note that this is different from "BAC" where 'B' is on the left bottom and 'A' is on the right bottom.
You start with a bottom row of blocks bottom, given as a single string, that you must use as the base of the pyramid.
Given bottom and allowed, return true _if you can build the pyramid all the way to the top such thatevery triangular pattern in the pyramid is in _allowed, orfalseotherwise.

Input: bottom ="BCD", allowed =["BCC","CDE","CEA","FFF"] Output:true Explanation: The allowed triangular patterns are shown on the right. Starting from the bottom(level 3), we can build "CE" on level 2 and then build "A" on level 1. There are three triangular patterns in the pyramid, which are "BCC","CDE", and "CEA". All are allowed.

Input: bottom ="AAAA", allowed =["AAB","AAC","BCD","BBE","DEF"] Output:false Explanation: The allowed triangular patterns are shown on the right. Starting from the bottom(level 4), there are multiple ways to build level 3, but trying all the possibilites, you will get always stuck before building level 1.
We can recursively try to build the pyramid from the bottom up, trying all possible valid blocks for each position using the allowed patterns. If we reach the top, we succeed; if we get stuck, we backtrack.
classSolution {
funpyramidTransition(bottom: String, allowed: List<String>): Boolean {
val mp = mutableMapOf<String, MutableList<Char>>()
for (s in allowed) mp.getOrPut(s.substring(0,2)) { mutableListOf() }.add(s[2])
fundfs(row: String): Boolean {
if (row.length ==1) returntruevar nexts = listOf("")
for (i in0 until row.length-1) {
val key = row.substring(i,i+2)
val vs = mp[key] ?:returnfalseval tmp = mutableListOf<String>()
for (pre in nexts)
for (c in vs) tmp.add(pre + c)
nexts = tmp
}
for (n in nexts) if (dfs(n)) returntruereturnfalse }
return dfs(bottom)
}
}
from typing import List
classSolution:
defpyramidTransition(self, bottom: str, allowed: List[str]) -> bool:
from collections import defaultdict
mp = defaultdict(list)
for s in allowed:
mp[s[:2]].append(s[2])
defdfs(row: str) -> bool:
if len(row) ==1:
returnTrue nexts = ['']
for i in range(len(row)-1):
key = row[i:i+2]
if key notin mp:
returnFalse tmp = []
for pre in nexts:
for c in mp[key]:
tmp.append(pre + c)
nexts = tmp
for n in nexts:
if dfs(n):
returnTruereturnFalsereturn dfs(bottom)
⏰ Time complexity: O(k^n), where n is the length of the bottom and k is the average number of allowed blocks per pair. The search space is exponential in the height of the pyramid.
🧺 Space complexity: O(n), for the recursion stack.