[word break] case failing for case "ab",["a", "b"], language cpp


  • 0
    Y

    my code is

    class Solution {
    public:
        bool wordBreak(string s, unordered_set<string> &dict) {
            static bool found = false;
            bool bWordBreak = false;
            for (auto iter = dict.begin(); iter != dict.end(); iter++) {
                string sub = *iter;
                auto index = s.find(sub);
                if (index != -1) {  
                    bWordBreak = true;
                    s.erase(index, sub.length());
                    if (s.length() > 0) {
                        bWordBreak &= wordBreak(s, dict);
                    } else {
                        found = true;
                    }
                }
                if (found) {
                    break;
                }
            }
            return bWordBreak;
        }
    };
    

    i run it in xcode, or on shell "g++ -gnu=c++11 -o hello main.cpp", the return value is true, but leetcode tells me

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

    can anyone tell me why ? thanks!


Log in to reply
 

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