LeetCode Substring with Concatenation of All Words
来源:互联网 发布:spss数据相关性分析 编辑:程序博客网 时间:2024/06/03 12:46
原题链接在这里: https://leetcode.com/problems/substring-with-concatenation-of-all-words/
这道题思路与Longest Substring Without Repeating Characters 相似。
不同的是这道题需要先生成并维护一个字典,检查新词是否是字典里的词。同时维护一个窗口[start,end], 外层循环是多种取词方法,以例子来说就是分成:
|bar|foo|the|foo|bar|man
b|arf|oot|hef|oob|arm|an
ba|rfo|oth|efo|oba|rma|n
当遇到字典里的词就移动右窗口end,同时用count计数,当遇到非词典里的词时,移动做窗口start,调到最新位置。双指针选取合适的窗口。在移动过程中,为避免重复的词冒充新词,需再生成一个HashMap来计数,若个别词的数量超过原有词的数量时,也需要移动做窗口start直到减掉了多余的词,同时更新count。最后若是出现了符合要求的count,保存start,然后右移一位start来计量新的start position.
Note: 1. 这道题花了好多时间,DP的题目好难。建立内层HashMap要在内层循环以外,这里刚开始错了。
2. 变量尽量少生成些,名字要尽量不同,这种bug真心简直了。
3. count++的条件是<=, 等于时也是要加的;count--的条件是<,等于时说明刚剪掉的是多余的, 减完才会正好等于,所以count不必减一。
AC Java:
public class Solution { public List<Integer> findSubstring(String s, String[] words) { List<Integer> res = new ArrayList<>(); if(s.length() == 0 || words.length == 0) return res; int wordLen = words[0].length(); int sLen = s.length(); HashMap<String, Integer> wordMap = new HashMap<>(); for(int i = 0; i < words.length; i++){ int num = 1; if(wordMap.get(words[i])!= null){ num += wordMap.get(words[i]); } wordMap.put(words[i],num); } for(int i = 0; i <wordLen; i++){ int start = i; int max = sLen-wordLen+1; int count = 0; HashMap<String,Integer> winMap = new HashMap<>(); //error for(int end = start;end<max;end+=wordLen){ String tempStr = s.substring(end,end+wordLen); if(!wordMap.containsKey(tempStr)){ winMap.clear(); start = end + wordLen; count = 0; continue; } if(!winMap.containsKey(tempStr)){ winMap.put(tempStr,1); }else{ int temp = winMap.get(tempStr); winMap.put(tempStr,temp+1); } if(winMap.get(tempStr) <= wordMap.get(tempStr)){ count++; } else{ while(winMap.get(tempStr) > wordMap.get(tempStr)){ String leftStr = s.substring(start,start+wordLen); winMap.put(leftStr,winMap.get(leftStr)-1); //error if(winMap.get(leftStr) < wordMap.get(leftStr)) //error count--; start+=wordLen; } } if(count == words.length){ res.add(start); tempStr = s.substring(start,start+wordLen); winMap.put(tempStr,winMap.get(tempStr)-1); count--; start+= wordLen; } } } return res; }}
0 0
- LeetCode: Substring with Concatenation of All Words
- LeetCode : Substring with Concatenation of All Words
- [Leetcode] Substring with Concatenation of All Words
- [LeetCode]Substring with Concatenation of All Words
- LeetCode-Substring with Concatenation of All Words
- [LeetCode] Substring with Concatenation of All Words
- LeetCode:Substring with Concatenation of All Words
- Leetcode: Substring with Concatenation of All Words
- [LeetCode] Substring with Concatenation of All Words
- leetcode Substring with Concatenation of All Words
- leetcode Substring with Concatenation of All Words
- LeetCode Substring with Concatenation of All Words
- LeetCode | Substring with Concatenation of All Words
- LeetCode - Substring with Concatenation of All Words
- Substring with Concatenation of All Words -- LeetCode
- Leetcode: Substring with Concatenation of All Words
- leetcode:Substring with Concatenation of All Words
- LeetCode|Substring with Concatenation of All Words
- 为eclipse 安装TestNG 插件的方法
- Java MongoDB 3.X 3.04 Crud 增删查改 deleteMany updateMany updateOne
- 关于printf的一点小注意
- 九月腾讯,创新工场,淘宝等公司最新面试三十题(第171-200题)
- 【poj 2068】Nim
- LeetCode Substring with Concatenation of All Words
- leetcode_Repeated DNA Sequences
- 360机试题:最后一个字符
- Spring集成Quartz定时任务的一些问题
- [leetcode-118]Pascal's Triangle(c++)
- HDU 5371 Hotaru's problem(Manacher算法)
- 第一段HTML5代码
- Java内存和垃圾回收无废话手册
- 十月上旬百度,阿里巴巴,迅雷搜狗最新面试七十题(第201-270题)