leetcode 30. Substring with Concatenation of All Words
来源:互联网 发布:nginx多端口配置 编辑:程序博客网 时间:2024/05/22 05:10
leetcode 30. Substring with Concatenation of All Words
又一个TLE :
import java.util.ArrayList;import java.util.HashMap;import java.util.HashSet;import java.util.List;import java.util.Map;import java.util.Set;public class Solution {public static void main(String[] args){String s = "barfoofoobarthefoobarman";Solution k = new Solution();String[] words = {"foo","bar","the"};List<Integer> ans = k.findSubstring(s,words);for(int a :ans){System.out.println(a);}} public List<Integer> findSubstring(String s, String[] words) { List<Integer> pos = new ArrayList<Integer>(); List<Integer> ans = new ArrayList<Integer>(); Map<String,Integer> wordsSet = new HashMap<String,Integer>(); int slen = s.length(); int i = 0; for(String word :words){ if(wordsSet.containsKey(word)){ int val = wordsSet.get(word); wordsSet.put(word, val+1); }else{ wordsSet.put(word, 1); } } Map<String,Integer> copy = new HashMap<String,Integer>(wordsSet); int wlen = words[0].length(); while(i+wlen<=slen){ String part = s.substring(i,i+wlen); if(wordsSet.containsKey(part)){ pos.add(i); } i++; } int k =0; int j = 0; while(k<pos.size()){ j = pos.get(k); boolean isfind = false; boolean isfailure = false; while(!isfailure&&!isfind&&j+wlen<=slen){ String curWord = s.substring(j,j+wlen); int cnt = 0; if(wordsSet.containsKey(curWord)){ cnt = wordsSet.get(curWord); } if(pos.contains(j)&&wordsSet.containsKey(curWord)){ wordsSet.put(curWord, --cnt); if(cnt==0){ wordsSet.remove(curWord); } if(wordsSet.size()==0){ isfind = true; ans.add(pos.get(k)); }else{ j = j+ wlen; } }else{ isfailure = true; } } wordsSet = new HashMap<String,Integer>(copy); k++; } return ans; }}
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
- Python基础——内存管理
- Python matplotlib 画图窗口显示到gui或者控制台的方法
- 图解SSL/TLS协议
- WebAssembly,Web的新时代
- 你总要一个人,走过一些艰辛
- leetcode 30. Substring with Concatenation of All Words
- maven下载jar包改用阿里云maven库
- base标签
- runloop和runtime的区别
- Java矩阵包
- Local declaration of 'XXX' hides instance variable
- 314. Binary Tree Vertical Order Traversal
- (4)FPQuant JHipster_使用数据库
- 自定义控件的方式