139. Word Break

883 단어
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example,
given*s* = "leetcode",*dict* = ["leet", "code"].
Return true because "leetcode" can be segmented as "leet code".
public class Solution {
    public boolean wordBreak(String s, Set dict) {
        int len = s.length();
        if(len == 0){
            return false;
        }
        
        boolean f[] = new boolean[len];
        for(int i = 0; i=0; j--){
                if((j==0 || f[j-1]) && dict.contains(s.substring(j, i+1))){
                    f[i] = true;
                    break;
                }
            }
        }
        
        return f[len-1];
    }
}

좋은 웹페이지 즐겨찾기