Can someone tell me why I get "wrong answer" while i am correct locally?


  • 0
    E

    Input: "a", ["b"]
    Output: true
    Expected: false

    class Solution {
    public:
    bool wordBreak(string s, unordered_set<string>& wordDict)
    {
    int len = s.length();
    bool reach[len+1];
    reach[0] = true;

    for(int i = 1; i <= len; i++)
    {
        for(int j = 1; j <= i; j++)
        {
            if(reach[j-1])
            {
                string temp = s.substr(j-1,i-j+1);
                if(wordDict.find(temp) != wordDict.end())
                {
                    reach[i] = true;
                    break;
                }
            }
        }
    }
    
    
    return reach[len];
    

    }
    };


  • 0
    E

    solved.
    Leetcode complier does not initialize "bool reach[len+1]" as "false";
    get accepted after add code as below.

    bool reach[len+1] = {false};

Log in to reply
 

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