OJ get my solution wrong?? I thought my solution is correct


  • 0
    S

    I am using the following recursive solution. I felt it is correct, but the OJ always give me last test case wrong.

    Can anyone tell me what's wrong with my code?

    bool wordBreak(string s, unordered_set<string> &dict) {
    	if (dict.find(s) != dict.end())
    	{
    		return true;
    	}
    
    	for (size_t len = 1; len < s.size(); len++)
    	{
    		string firstPart = s.substr(0, len);
    		string secondPart = s.substr(len);
    
    		if (dict.find(firstPart) != dict.end())
    		{
    			return wordBreak(secondPart, dict);
    		}
    	}
    
    	return false;     
    }

  • 0
    S
    Input:	"aaaaaaa", ["aaaa","aaa"]
    Output:	false
    Expected:	true
    

    Is this the test case you're talking about? I am also having same issue. I believe, OJ got this one wrong :)


  • 0

    @srujan i'm confused as well. why is this false? the question asks for "one or more" words. "aaaaaaa" can be broken out here into the "word" "aa" or "aaaa" from the wordList


Log in to reply
 

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