Skip to content

Latest commit

 

History

History
123 lines (48 loc) · 2.1 KB

File metadata and controls

123 lines (48 loc) · 2.1 KB

中文文档

Description

We are stacking blocks to form a pyramid. Each block has a color which is a one letter string.

We are allowed to place any color block C on top of two adjacent blocks of colors A and B, if and only if ABC is an allowed triple.

We start with a bottom row of bottom, represented as a single string. We also start with a list of allowed triples allowed. Each allowed triple is represented as a string of length 3.

Return true if we can build the pyramid all the way to the top, otherwise false.

Example 1:

Input: bottom = "BCD", allowed = ["BCG", "CDE", "GEA", "FFF"]

Output: true

Explanation:

We can stack the pyramid like this:

    A

   / \

  G   E

 / \ / \

B   C   D



We are allowed to place G on top of B and C because BCG is an allowed triple.  Similarly, we can place E on top of C and D, then A on top of G and E.

 

Example 2:

Input: bottom = "AABA", allowed = ["AAA", "AAB", "ABA", "ABB", "BAC"]

Output: false

Explanation:

We can't stack the pyramid to the top.

Note that there could be allowed triples (A, B, C) and (A, B, D) with C != D.

 

Note:

    <li><code>bottom</code> will be a string with length in range <code>[2, 8]</code>.</li>
    
    <li><code>allowed</code> will have length in range <code>[0, 200]</code>.</li>
    
    <li>Letters in all strings will be chosen from the set <code>{&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;, &#39;F&#39;, &#39;G&#39;}</code>.</li>
    

 

Solutions

Python3

Java

...