Can anyone help me with my solution


  • 0
    D

    i try to use two index.

    class Solution {
    public:
    bool wordBreak(string s, unordered_set<string>& wordDict) {

     int i=0;
     int j=0;
    while(i<=s.size())
    {
     for(j=i;j<s.size();j++)
     {
         string word= s.substr(i,j+1-i);
         if(wordDict.find(word)!= wordDict.end())
         {
             i=j+1;
             if(i==s.size()) return true;
       
             break;
         }
         
           if(j==s.size()-1) return false;
     }
    
    }
     
    
    }
    

    };


Log in to reply
 

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