当前位置:网站首页>Word splitting problem
Word splitting problem
2022-06-13 02:33:00 【Prodigal son's private dishes】
subject :
Give you a string s And a list of strings wordDict As a dictionary . Please judge whether you can use the words in the dictionary to splice s .
Be careful : It is not required to use all the words in the dictionary , And the words in the dictionary can be reused .
Example 1:
Input : s = “leetcode”, wordDict = [“leet”, “code”]
Output : true
explain : return true
because “leetcode” Can be “leet” and “code” Stitching into .
Example 2:Input : s = “applepenapple”, wordDict = [“apple”, “pen”]
Output : true
explain : return true
because “applepenapple” Can be “apple” “pen” “apple” Stitching into .
Be careful , You can reuse the words in the dictionary .
Example 3:
Input : s = “catsandog”, wordDict = [“cats”, “dog”, “sand”, “and”, “cat”]
Output : false
Code :
DP
class Solution {
public boolean wordBreak(String s, List<String> wordDict) {
boolean[] dp = new boolean[s.length()+1];
dp[0] = true;
for(int i = 1; i <= s.length(); i++){ // knapsack
for(int j = 0; j < i; j++){ // goods
if(wordDict.contains(s.substring(j,i)) && dp[j]){
dp[i] = true;
}
}
}
return dp[s.length()];
}
}
to flash back
public boolean backTrack(String s, Set<String> wordDictSet, int startIndex, int[] memory){
// Termination conditions
if(startIndex >= s.length()){
return true;
}
if(memory[startIndex] != 0){
return memory[startIndex] == 1 ? true:false;
}
// Processing results , recursive , to flash back
for(int i = startIndex; i < s.length(); i++){
String word = s.substring(startIndex, i+1);
if(wordDictSet.contains(word) && backTrack(s, wordDictSet, i+1, memory)){
memory[startIndex] = 1;
return true;
}
}
memory[startIndex] = -1;
return false;
}
边栏推荐
- Chapter7-12_ Controllable Chatbot
- [pytorch] kaggle image classification competition arcface + bounding box code learning
- Opencv 15 face recognition and eye recognition
- 拍拍贷母公司信也季报图解:营收24亿 净利5.3亿同比降10%
- 01 初识微信小程序
- Basic exercise of test questions decimal to hexadecimal
- Chapter7-13_ Dialogue State Tracking (as Question Answering)
- redis
- [work with notes] MFC solves the problem that pressing ESC and enter will automatically exit
- Automatic differential reference
猜你喜欢

L1 regularization and its sparsity
![[pytorch] kaggle large image dataset data analysis + visualization](/img/b0/7b8aff44d6bedd7ca2c705f13a8556.jpg)
[pytorch] kaggle large image dataset data analysis + visualization

Paper reading - group normalization

微信云开发粗糙理解
![[reading papers] visual convolution zfnet](/img/01/4181f19b2d24b842488522c2001970.jpg)
[reading papers] visual convolution zfnet
![[reading papers] deep learning face representation by joint identification verification, deep learning applied to optimization problems, deepid2](/img/a1/151d2afe6d7f0bd95fe93fc80f633e.jpg)
[reading papers] deep learning face representation by joint identification verification, deep learning applied to optimization problems, deepid2

Fast Color Segementation

After idea uses c3p0 connection pool to connect to SQL database, database content cannot be displayed

Queuing theory, game theory, analytic hierarchy process

Jump model between mirrors
随机推荐
L1 regularization and its sparsity
[pytorch] kaggle image classification competition arcface + bounding box code learning
[pytorch] kaggle large image dataset data analysis + visualization
Port mapping between two computers on different LANs (anydesk)
[learning notes] xr872 GUI littlevgl 8.0 migration (file system)
Leetcode 926. 将字符串翻转到单调递增 [前缀和]
[reading papers] deepface: closing the gap to human level performance in face verification. Deep learning starts with the face
Priority queue with dynamically changing priority
Principle and steps of principal component analysis (PCA)
Leetcode 450. 删除二叉搜索树中的节点 [二叉搜索树]
Hstack, vstack and dstack in numpy
Paper reading - beat tracking by dynamic programming
重定向设置参数-RedirectAttributes
Introduction to armv8/armv9 - learning this article is enough
Number of special palindromes in basic exercise of test questions
Opencvshare4 and vs2019 configuration
ROS learning -5 how function packs with the same name work (workspace coverage)
Linear, integer, nonlinear, dynamic programming
json,xml,txt
too old resource version,Code:410