Code seems to work fine except for the final test case


  • 0
    K
    class Solution {
    public:
        bool wordBreak(string s, unordered_set<string> &dict) {
            if(!s.length() || dict.find(s) != dict.end())
            return true;
            
            for(int i=1;i<s.length();i++)
            {
                if(dict.find(s.substr(0,i)) != dict.end())
                return wordBreak(s.substr(i,s.length()-i),dict);
            }
            return false;
        }
    };

Log in to reply
 

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