hashmap没看懂,先复制下来以后看

Maybe this is the so called window technique. The window was moved right by the while(){} loop. Since substring() is actually expensive in Java, each time I store the substring in a variable to avoid repeatedly calling the substring() function.

More specifically, there are three cases that the window changes:

1.The substring at the right of the window is not in words dictionary, then we move the whole window to the right side of this substring and set the window back to an empty window.

2.The substring at the right of window is a candidate word and not used out by the current window, then we add this substring into window. The window's right boundary extends. Now if the window is a valid solution, add the start index of window to result and cut off the head word of the window for further checking, the window's left boundary shrinks.

3.The substring at the right of window is a candidate word and is used out by the current window. Then we cut off the head word of the window, the window's left boundary shrinks (This would be done repeatedly by the while loop until the substring is not used out by the current window and could be added into the window).

public class Solution { public List<Integer> findSubstring(String s, String[] words) { List<Integer> res = new ArrayList<Integer>(); if(words.length==0||words[0].length()==0) return res; Map<String,Integer> wordDict = new HashMap<String,Integer>(); for(String word : words) { if(!wordDict.containsKey(word)) wordDict.put(word,1); else wordDict.put(word,wordDict.get(word) + 1); } Map<String,Integer> currWords = new HashMap<String,Integer>(); int len = words[0].length(); for(int i = 0; i < len; i++) { int k = i, j = i; //k is at the head of the window and j is the last. int addedCount = 0; //to indicate whether we add index to res. while(k<= s.length()-len*words.length&&j + len <= s.length()) { //make sure the remaining length is enough. String subWord = s.substring(j,j+len); if(!wordDict.containsKey(subWord)) { //the substring is not in words, head jumps to the right of this substring. addedCount = 0; currWords.clear(); j += len; k = j; continue; } if(!currWords.containsKey(subWord)||currWords.get(subWord)!=wordDict.get(subWord)) { if(!currWords.containsKey(subWord)) currWords.put(subWord,1); else currWords.put(subWord,currWords.get(subWord) + 1); //update the current words we used. addedCount++; if(addedCount == words.length) { //if get a index, add it to res. And we need to continue checking res.add(k); addedCount--; //remove the head and check new substring, so count-- and move head to new position. String preHead = s.substring(k,k+len); if(currWords.get(preHead)==1) currWords.remove(preHead); //update the currWords map. else currWords.put(preHead,currWords.get(preHead)-1); k += len; } j += len; } else { //the current substring was used out before. Move head len steps right. String preHead = s.substring(k,k+len); addedCount--; if(currWords.get(preHead)==1) currWords.remove(preHead); //update the currWords map. else currWords.put(preHead,currWords.get(preHead)-1); k += len; //don't move j this case. } } currWords.clear(); } return res; } }'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值