How to generate the res without using the list generator?

  • 0

    The following is the answers from Special thanks to @tusuzi. However, I am wondering if it is possible to generate the final res array without using the list generator. I have spend lots of time on it but still cannot find a solution. Any help will be deeply appreciated.

    class Solution:
        # @param start, a string
        # @param end, a string
        # @param dict, a set of string
        # @return a list of lists of string
        def findLadders(self, start, end, dic):
            level = {start}
            parents = collections.defaultdict(set)
            while level and end not in parents:
                next_level = collections.defaultdict(set)
                for node in level:
                    for char in string.ascii_lowercase:
                        for i in range(len(start)):
                            n = node[:i]+char+node[i+1:]
                            if n in dic and n not in parents:
                level = next_level
            res = [[end]]
            while res and res[0][0] != start:
                res = [[p]+r for r in res for p in parents[r[0]]]
            return res

Log in to reply

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