当前位置:网站首页>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;
}
边栏推荐
- Image table solid line and dashed line detection
- 01 initial knowledge of wechat applet
- [reading papers] transformer miscellaneous notes, especially miscellaneous
- Leetcode 93 recovery IP address
- Chapter7-11_ Deep Learning for Question Answering (2/2)
- OpenCVSharpSample04WinForms
- speech production model
- Redirection setting parameters -redirectattributes
- Leetcode 926. 将字符串翻转到单调递增 [前缀和]
- SANs证书生成
猜你喜欢
![[pytorch] kaggle image classification competition arcface + bounding box code learning](/img/1e/5e921987754da1e1750acdadb36849.jpg)
[pytorch] kaggle image classification competition arcface + bounding box code learning

Laravel permission export

柏瑞凱電子沖刺科創板:擬募資3.6億 汪斌華夫婦為大股東
![Leetcode 450. Delete node in binary search tree [binary search tree]](/img/39/d5c4d424a160635791c4645d6f2e10.png)
Leetcode 450. Delete node in binary search tree [binary search tree]

Think about the possibility of attacking secure memory through mmu/tlb/cache

Understanding and thinking about multi-core consistency

Mbedtls migration experience

Cumulative tax law: calculate how much tax you have paid in a year

Stm32f4 DMA Da sine wave generator keil5 Hal library cubemx

Mean Value Coordinates
随机推荐
(novice to) detailed tutorial on machine / in-depth learning with colab from scratch
Impossible d'afficher le contenu de la base de données après que l'idée a utilisé le pool de connexion c3p0 pour se connecter à la base de données SQL
nn. Conv2d and nn Convtranspose2d differences
CV 06 demonstrates backgroundworker
Mean Value Coordinates
Opencv 15 face recognition and eye recognition
03 认识第一个view组件
[pytorch] kaggle image classification competition arcface + bounding box code learning
Matlab: find the inner angle of n-sided concave polygon
[keras] data of 3D u-net source code analysis py
Classification and summary of system registers in aarch64 architecture of armv8/arnv9
[reading papers] deepface: closing the gap to human level performance in face verification. Deep learning starts with the face
05 tabbar navigation bar function
[pytorch] kaggle large image dataset data analysis + visualization
Paper reading - beat tracking by dynamic programming
Paper reading - joint beat and downbeat tracking with recurrent neural networks
Surpass the strongest variant of RESNET! Google proposes a new convolution + attention network: coatnet, with an accuracy of 89.77%!
[Dest0g3 520迎新赛] 拿到WP还整了很久的Dest0g3_heap
在IDEA使用C3P0連接池連接SQL數據庫後卻不能顯示數據庫內容
Example 4 linear filtering and built-in filtering