Visual studio produced same results for the 'wrong' and 'accepted' answers


  • 0
    D

    I don't know why this code is wrong? It generated the same result as the 'accepted' version in VS.

    The only diff is that the 'accepted' one has lines commented out to use vector instead of bool *......

    bool wordBreak(string s, unordered_set<string> &dict) {
        int len = s.length();
        
       // bool *can =new bool[len+1];
        vector<bool> can(len+1, false); 
        can[0] = true;
        
      //  for (int i=1;i<len+1; i++)
     //   can[i]=false;
        
        for (int i=1; i<=len; i++)
           for (int j=0;j<i;j++)
           {
               if (can[j]&&dict.find(s.substr(j,i))!=dict.end())
               {
                   can[i]=true;
                   break;
               }
           }
        return can[len];
    

    }

    Input:
    "bccdbacdbdacddabbaaaadababadad", ["cbc","bcda","adb","ddca","bad","bbb","dad","dac","ba","aa","bd","abab","bb","dbda","cb","caccc","d","dd","aadb","cc","b","bcc","bcd","cd","cbca","bbd","ddd","dabb","ab","acd","a","bbcc","cdcbd","cada","dbca","ac","abacd","cba","cdb","dbac","aada","cdcda","cdc","dbc","dbcb","bdb","ddbdd","cadaa","ddbc","babb"]

              Output: 
              false 
            
    
            
              Expected: 
              true

Log in to reply
 

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