Iterative Python Solution Beats 100%


  • 0
    B

    The idea is simple, we first construct naive results with invalid results like "00" or "080" in it and then we remove them at the end.

    Note that the invalid result only occurs when its first character is "0" and the length of the it is greater than 1.

    class Solution(object):
        def findStrobogrammatic(self, n):
            if n == 0: return []
            standard = ["00", "11", "88", "69", "96"]
            res = ["00", "11", "88", "69", "96"] if n % 2 == 0 else ["0", "1", "8"]
            while len(res[0]) < n:
                res = [(ss[0] + x + ss[1]) for ss in standard for x in res]
            return res if len(res[0]) == 1 else [s for s in res if s[0] != "0"]
    

Log in to reply
 

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