Python Regex solution - won't pass OJ though (too slow) ...


  • 0
    G
    class Solution:
        # @param s, a string
        # @param dict, a set of string
        # @return a boolean
        def wordBreak(self, s, dict):
            if not dict:
                return False
    
            pattern = '^(' + '|'.join(dict) + ')+$'
            
            if re.findall(pattern, s):
                return True
            else:
                return False

  • 0

    Woah, what is this? Are you actually different people, like you recently made it sound like somewhere? Because this not on par with your other solutions. This wrongly returns False for wordBreak("", []) and for wordBreak("", ["leet", "code"]), because of two different bugs. And findall is odd, since this is really a match job (closest to the desired boolean). And if-X-return-True-else-return-False is such noobish code.

    Here's my attempt. Sadly, though not surprisingly, also too slow.

    def wordBreak(self, s, words):
        return bool(re.match('(' + '|'.join(words) + ')*$', s))

Log in to reply
 

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