[Leetcode] Word Break

来源:互联网 发布:java设计小游戏 编辑:程序博客网 时间:2024/06/05 14:38

public class Solution {        Map<String, Boolean> record;        public Solution() {        record = new HashMap<>();    }        public boolean wordBreak(String s, Set<String> wordDict) {        if(s.equals(""))            return true;                    if(record.get(s) == null){            for(int i = 0; i < s.length(); i++) {                String subString = s.substring(0, i+1);                if(wordDict.contains(subString)) {                    if(wordBreak(s.substring(i+1), wordDict)) {                        record.put(s, Boolean.TRUE);                        return Boolean.TRUE;                    }                }                else {                    continue;                }            }            record.put(s, Boolean.FALSE);            return false;        }        else{            return record.get(s);        }    }}

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".



0 0
原创粉丝点击