G++ returns false leetcode returns true when expected is false


  • 0
    P

    Test Case that fails

    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab

    "a","aa","aaa","aaaa","aaaaa","aaaaaa","aaaaaaa","aaaaaaaa","aaaaaaaaa","aaaaaaaaaa"

    class Solution {
        bool fun(string s, int start,unordered_set<string>& wordDict,vector<bool>&dp,vector<bool>&flag) {
                if(start>=s.length())
                return true;
                if(flag[start]==true)
                  return dp[start];
                for(int i=1;i<=s.length();i++)
                {
                    if((wordDict.find(s.substr(start,i))!=wordDict.end())&&fun(s,start+i,wordDict,dp,flag))
                    {
                      flag[start]=true;
                      dp[start]=true;
                      return true;
                    }
                }
                flag[start]=true;
                dp[start]=false;
                return false;
            }
        public:
         bool wordBreak(string s,unordered_set<string>& wordDict) {
             wordDict.insert("");
                vector<bool>dp(false,s.length()+1);
                vector<bool>flag(false,s.length()+1);
                //for(int i=0;i<s.length();i++)
        
                fun(s,0,wordDict,dp,flag);
                return dp[0];
            }
        };

Log in to reply
 

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