LeetCode_OJ【30】Substring with Concatenation of All Words
2015-09-23 17:06
417 查看
You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) ins that is a concatenation of each word in
words exactly once and without any intervening characters.
For example, given:
s:
words:
You should return the indices:
(order does not matter).
这道题目主要是运用滑动窗口的思想。
首先设置一个Map<String,int> (记作need)记录words中所有的字符串,Map<String,int> (记作has)则表示当前遍历的情况。
设words中的任一一个字符串的长度为len,则i从0到len,对于每个i,分别看s在[i,i+len] ,[i+len,i+2*len]........的子串,是否符合要求,符合要求就添加到has中。
内层循环的时间复杂度为O(n/len),外层循环的时间复杂度为O(len),所以总的时间复杂度为O(n)。
代码如下:
words exactly once and without any intervening characters.
For example, given:
s:
"barfoothefoobarman"
words:
["foo", "bar"]
You should return the indices:
[0,9].
(order does not matter).
这道题目主要是运用滑动窗口的思想。
首先设置一个Map<String,int> (记作need)记录words中所有的字符串,Map<String,int> (记作has)则表示当前遍历的情况。
设words中的任一一个字符串的长度为len,则i从0到len,对于每个i,分别看s在[i,i+len] ,[i+len,i+2*len]........的子串,是否符合要求,符合要求就添加到has中。
内层循环的时间复杂度为O(n/len),外层循环的时间复杂度为O(len),所以总的时间复杂度为O(n)。
代码如下:
public class Solution { public List<Integer> findSubstring(String s, String[] words) { List<Integer> res = new ArrayList<Integer>(); if(s == null || s.length() == 0 || words == null || words.length == 0) return res; HashMap<String, Integer> need = new HashMap<String, Integer>(); HashMap<String, Integer> has = new HashMap<String, Integer>(); for(int i = 0; i < words.length; i ++){ if(need.containsKey(words[i])) need.put(words[i], need.get(words[i])+1); else need.put(words[i],1); } int len = words[0].length(); for(int i = 0; i < len ; i++){ int start = i; int cur = i; while(start <= s.length() - words.length *len && cur <= s.length() -len){ String str = s.substring(cur,cur+len); if(need.containsKey(str)){ if(has.containsKey(str) && has.get(str) == need.get(str)){ String tmp = s.substring(start,start+len); has.put(tmp, has.get(tmp)-1); start += len; continue; } else if(!has.containsKey(str)){ has.put(str, 1); } else{ has.put(str, has.get(str)+1); } cur += len; if(cur - start == words.length*len){ res.add(start); String tmp = s.substring(start,start+len); has.put(tmp, has.get(tmp)-1); start += len; } } else{ has.clear(); cur += len; start = cur; } } has.clear(); } return res; } }
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解
- LeetCode [Day 5] Longest Palindromic Substring 题解
- LeetCode [Day 6] ZigZag Conversion 题解