Pyramid Transition Matrix - Problem
Imagine you're an ancient architect tasked with building a sacred pyramid using colored stone blocks. Each block has a unique color represented by a single letter (A, B, C, etc.).
The pyramid follows strict architectural rules:
- Each level has one fewer block than the level below it
- Blocks are perfectly centered on the level beneath
- Most importantly, only specific triangular patterns are allowed by the ancient building codes
You're given:
bottom: A string representing the bottom row of blocks (e.g., "AABA")allowed: A list of 3-character strings representing valid patterns (e.g., ["ABC", "ABD", "BAC"])
Each pattern like "ABC" means: if you have block 'A' on the left and block 'B' on the right at the bottom, you can place block 'C' on top of them.
Goal: Determine if you can build the complete pyramid from bottom to top (ending with a single block) such that every triangular pattern used is in the allowed list.
Return true if the pyramid can be completed, false otherwise.
Input & Output
example_1.py — Simple Valid Pyramid
$
Input:
bottom = "AABA", allowed = ["ABC", "ACA", "CAA", "AAB"]
›
Output:
false
💡 Note:
Starting with AABA, we need patterns for AA, AB, and BA pairs. We have AAB (A+A→B), ABC (A+B→C), but no pattern for BA (B+A→?). Since we cannot find a valid pattern for the BA pair, pyramid construction fails.
example_2.py — No Valid Path
$
Input:
bottom = "AABA", allowed = ["ABC", "BAC"]
›
Output:
false
💡 Note:
Starting with AABA, we need patterns for AA, AB, and BA pairs. We have ABC (A+B→C) and BAC (B+A→C), but no pattern for AA. Cannot build the first level up from bottom.
example_3.py — Single Block Base
$
Input:
bottom = "A", allowed = ["ABC", "DEF"]
›
Output:
true
💡 Note:
Edge case: If bottom already has only one block, the pyramid is already complete. No building required, so return true regardless of allowed patterns.
Constraints
- 2 ≤ bottom.length ≤ 6
- 0 ≤ allowed.length ≤ 216
- allowed[i].length == 3
- The letters in all input strings are from the set {'A', 'B', 'C', 'D', 'E', 'F'}
- All values of allowed are unique
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code