Python 3 lines brute force


  • 1
        def restoreIpAddresses(self, s):
            if len(s)>12: return []
            def check(s): return 0 <= int(s) <= 255 and str(int(s)) == s
            return [s[:i] + "." + s[i:j] + "." + s[j:k] + "." + s[k:] 
                    for i,j,k in itertools.combinations(range(1, len(s)),3)
                    if check(s[:i]) and check(s[i:j]) and check(s[j:k]) and check(s[k:])]

Log in to reply
 

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