Leetcode 139.单词拆分

单词拆分

给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词。

说明:

  • 拆分时可以重复使用字典中的单词。
  • 你可以假设字典中没有重复的单词。
 1 class Solution {
 2     public boolean wordBreak(String s, List<String> wordDict) {
 3         int n=s.length();
 4         boolean[] dp=new boolean[n+1];
 5         dp[0]=true;
 6         for(int i=1;i<=n;i++){
 7             dp[i]=false;
 8             for(int j=0;j<i;j++){
 9                 if(dp[j]&&wordDict.contains(s.substring(j,i))){
10                     dp[i]=true;
11                 }
12             }
13         }
14         return dp[n];
15     }
16 }

猜你喜欢

转载自www.cnblogs.com/kexinxin/p/10187872.html
今日推荐