Java implementation using loop and recursion


  • 0
    M
        public boolean wordBreak(String s, List<String> wordDict) {
            int x=-1;
            int start=0;
            while(x!=s.length()){ 
                x=findTheSubstringThatExistInTheDict(s,start,wordDict);
                if(x==Integer.MAX_VALUE){
                    return false;
                }
                System.out.println("Got x " + x);
                start=x;
            }
            return true;
            
        }
        
        public int findTheSubstringThatExistInTheDict(String s,int start,List<String> wordDict) {
            int j=start+1;
            while(j<s.length()+1){
                String str1=s.substring(start,j);
                if(wordDict.contains(str1)){
                    return j;
                }
                j++;
            }
            return Integer.MAX_VALUE;
        }
    }```
    
    Give me the feedback

Log in to reply
 

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