Python Solution BFS


  • 0
    Y
    def countAndSay(self, n):
            
            explored = ['1']
            frontier = []
            count = 0
            pointer = explored[0]
            
            while(n > 1):
                for i,v in enumerate(explored):
                    if v == pointer:
                        count += 1
                        if i == len(explored) - 1:
                            frontier.append(str(count))
                            frontier.append(str(explored[i]))
                    else:
                        pointer = explored[i]
                        frontier.append(str(count))
                        count = 1
                        frontier.append(str(explored[i-1]))
                        if i == len(explored) - 1:
                            frontier.append(str(count))
                            frontier.append(str(explored[i]))
                    
                n -= 1
                explored = frontier
                frontier = []
                count = 0
                pointer = explored[0]
            return ''.join(explored)

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.