Why this code give wrong answer in OJ but right in my laptop for "a" , ["a"]?


  • 0
    A
    class Solution:
    # @param s, a string
    # @param dict, a set of string
    # @return a boolean
    def wordBreak(self, s, dict):
        if len(s) == 0:
            return True
        wb = [False for i in range(0,len(s)+1)]
    
        for i in range(1,len(s)+1):
            if s[0:i] in dict and wb[i] == False:
                wb[i] = True
                
            if wb[i] == True:
                if i == len(s):
                    return True
                
                for j in range(i+1, len(s)+1):
                    if s[i:j-i] in dict and wb[j] == False:
                        wb[j] = True
                    
                    if j == len(s) and wb[j] == True:
                        return True
                    
        return False

Log in to reply
 

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