139. Word Break

网友投稿 524 2022-10-09

139. Word Break

139. Word Break

Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words. You may assume the dictionary does not contain duplicate words.

For example, given s = “leetcode”, dict = [“leet”, “code”].

Return true because “leetcode” can be segmented as “leet code”.

UPDATE (2017/1/4): The wordDict parameter had been changed to a list of strings (instead of a set of strings). Please reload the code definition to get the latest changes.

class Solution { public boolean wordBreak(String s, List wordDict) { if (s.length() == 0) return false; boolean[] dp = new boolean[s.length() + 1]; dp[0] = true; for (int i = 1; i < dp.length; i++) { for (int j = 0; j < i; j++) { if (dp[j] && wordDict.contains(s.substring(j, i))) { dp[i] = true; break; } } } return dp[s.length()]; }}

class Solution { HashMap map = new HashMap<>(); public boolean wordBreak(String s, List wordDict) { //split s into 2 parts for(String str: wordDict){ map.put(str, true); } return helper(s, wordDict); } public boolean helper(String s,List wordDict){ if(map.containsKey(s)) return map.get(s); for(int i=0;i

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Nevow- 小型Python Web框架(nevowechat)
下一篇:OpenAIS- 集群框架的应用程序接口规范(openAI生成图像软件)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~