Run time error on "a", ["a"], but correct in my local machine gcc4.8.1


  • 0
    J

    I used gtest in my local machine, repeat 50 times, no crash. Could somebody please tell me what's wrong with my code? The algorithm is the same as others, dp with dfs.

    class Solution {
    public:
        vector<string> wordBreak(string s, unordered_set<string> &dict) {
            vector<string> vec;
            if(s.empty()) {
                return vec;
            }
            vector<bool> dp(s.size()+1, true);
            string str;
            helper(s, dict, str, 0, dp, vec);
            return vec;
            
        }
        void helper(const string &s, const unordered_set<string> &dict, string &str, int index, vector<bool> &dp, vector<string> &vec) {
            if(index==s.size()) {
                vec.push_back(str.substr(0, str.size()-1));
                return;
            }
            for(int i=index; i<s.size(); ++i ) {
                string str1 = s.substr(index, i-index+1);
                if(dict.find(str1)!=dict.end()&&dp[i+1]) {
                    str.append(str1).append(" ");
                    size_t size = vec.size();
                    helper(s, dict, str, i+1, dp, vec);
                    if(vec.size()==size) {
                        dp[i+1]=false;
                    }
                    str.resize(size-str1.size()-1);
                }
            }
            
        }
    };

Log in to reply
 

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