当前位置:网站首页>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;
}
边栏推荐
- The precision of C language printf output floating point numbers
- Huffman tree and its application
- C # illustrated tutorial (Fourth Edition) chapter7-7.2 accessing inherited members
- Test questions basic exercise 01 string
- [Dest0g3 520迎新赛] 拿到WP还整了很久的Dest0g3_heap
- cmake_ example
- Leetcode 450. Delete node in binary search tree [binary search tree]
- 05 tabbar navigation bar function
- Solution of depth learning for 3D anisotropic images
- Paper reading - beat tracking by dynamic programming
猜你喜欢

微信云开发粗糙理解

Introduction to arm Cortex-M learning

Common web page status return code crawler

Understand speech denoising

Stm32f4 DMA Da sine wave generator keil5 Hal library cubemx

Jump model between mirrors

Priority queue with dynamically changing priority
![[reading point paper] deeplobv3 rethinking atlas revolution for semantic image segmentation ASPP](/img/4e/a5c6b1a8880209f89d6bf252ff889a.jpg)
[reading point paper] deeplobv3 rethinking atlas revolution for semantic image segmentation ASPP

What are the differences in cache/tlb?

Use of OpenCV 12 findcircuits and drawcircuits
随机推荐
[Dest0g3 520迎新赛] 拿到WP还整了很久的Dest0g3_heap
Laravel permission export
Principle and steps of principal component analysis (PCA)
[reading papers] transformer miscellaneous notes, especially miscellaneous
05 tabBar导航栏功能
ROS learning -5 how function packs with the same name work (workspace coverage)
Chapter7-11_ Deep Learning for Question Answering (2/2)
01 initial knowledge of wechat applet
redis
[reading papers] visual convolution zfnet
[pytorch] kaggle large image dataset data analysis + visualization
Paipai loan parent company Xinye quarterly report diagram: revenue of RMB 2.4 billion, net profit of RMB 530million, a year-on-year decrease of 10%
[pytorch]fixmatch code explanation (super detailed)
Use of OpenCV 12 findcircuits and drawcircuits
How to do Internet for small enterprises
Opencvshare4 and vs2019 configuration
Chapter7-13_ Dialogue State Tracking (as Question Answering)
SANs证书生成
Classification and summary of system registers in aarch64 architecture of armv8/arnv9
Paper reading - joint beat and downbeat tracking with recurrent neural networks