1class Solution(object):2def pyramidTransition(self, bottom, allowed):3"""4:type bottom: str5:type allowed: List[str]6:rtype: bool7"""8dic = defaultdict(list)9for i in allowed:10dic[(i[0], i[1])].append(i[2])1112res = []1314def dfs(arr, nxt):15# base case second floor and check top exists16if len(arr) == 2 and dic[(arr[0], arr[1])]:17return True1819# go to the next row now20if len(arr) == len(nxt) + 1:21return dfs(nxt, [])2223# keep iterating the same row24if dic[(arr[len(nxt)], arr[len(nxt) + 1])]:25for val in dic[(arr[len(nxt)], arr[len(nxt) + 1])]:26if dfs(arr, nxt + [val]):27return True28return False2930return dfs(bottom, [])