Testcase "a", ["a"] failed in OJ but success in my local machine

  • 0

    I use python to implement it

    class Solution:
    # @param s, a string
    # @param dict, a set of string
    # @return a boolean
    def wordBreak(self, s, dict):
        for part in dict:
            if part in s:
        if s.strip():
            return True
            return False

    When I run it in my local machine using

    print x.wordBreak("a", ["a"])

    It return true, but in oj, it said I was return false.
    Does anyone know what's the type of input for testcase? How to solve this problem?

  • 0

    Out of curiosity, yanlingyin, what would happen with s="singingsing" dict = {"sing","singing"}? Would it return true or false?

  • 0

    Yes, that's how you test the code.
    No, your code returns false because empty string evaluates to false.
    You might pass the first test case by reversing T/F, but you can't solve the whole problem that way.

  • 0

    Follwing mike3's thinking, I test your code with the following input: s="ingsing", dict = {"sing","singing"}, it gives "True" output. I believe your code is not right. The main point is: even though the first word matches, and the remaining could not be split, this does not automatically render a 'false', you still need to move on to the next good word, and test the remaining part.

Log in to reply

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