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
Pyramid Transition Matrix INPUT Bottom Row: "AABA" A A B A Allowed Patterns: "ABC" "ACA" "CAA" "AAB" Pattern "XYZ" means: X + Y at bottom --> Z on top Z X Y Hash Map (X,Y)-->[Z] ALGORITHM STEPS 1 Build Hash Map Map (left,right) --> possible tops (A,B)-->[C] (C,A)-->[A] (A,A)-->[B,C] 2 DFS + Backtrack Try each valid top block 3 Build Level by Level Complete row, then go up ? A A 4 Reach Single Top If 1 block left --> success! Return OK Backtrack if no valid pattern found at current position FINAL RESULT Valid Pyramid Built! A C A A C A A A B A Output: true All triangles are valid patterns from allowed Used: AAC, ACA, CAA, ABA Key Insight: Hash Map for O(1) Pattern Lookup 1. Pre-process allowed patterns into hash map: (left_block, right_block) --> list of possible top blocks 2. Use DFS with backtracking to explore all valid pyramid constructions level by level 3. For each adjacent pair, try all valid top blocks; backtrack if stuck, return true when single top reached TutorialsPoint - Pyramid Transition Matrix | Hash Map Approach
Asked in
Google 15 Meta 8 Amazon 6 Microsoft 4
28.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen