Sliding Window algorithm template to solve all the Leetcode substring search problem
Sliding Window algorithm template to solve all the Leetcode substring search problem
Back
Sliding Window algorithm template to solve all the Leetcode substring search problem.
Among all leetcode questions, I find that there are at least 5 substring search
problem which could be solved by the sliding window algorithm.
so I sum up the algorithm template here. wish it will help you!
1. the template:
//Two Pointers: begin - left pointer of the window; end - right pointer of the window
int begin = 0, end = 0;
if( map.containsKey(c) ){
map.put(c, map.get(c)-1);// plus or minus one
if(map.get(c) == 0) counter--;//modify the counter according the requirement(different
condition).
1/7
}
end++;
char tempc = s.charAt(begin);//***be careful here: choose the char at begin pointer,
NOT the end pointer
if(map.containsKey(tempc)){
map.put(tempc, map.get(tempc) + 1);//plus or minus one
if(map.get(tempc) > 0) counter++;//modify the counter according the
requirement(different condition).
}
begin++;
}
}
return result;
}
}
1. Firstly, here is my sliding solution this question. I will sum up the template below this
code.
https://leetcode.com/problems/minimum-window-substring/
https://leetcode.com/problems/longest-substring-without-repeating-characters/
https://leetcode.com/problems/substring-with-concatenation-of-all-words/
https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/
https://leetcode.com/problems/find-all-anagrams-in-a-string/
3) I will give my solution for these questions use the above template one by
one
Minimum-window-substring
https://leetcode.com/problems/minimum-window-substring/
2/7
public class Solution {
public String minWindow(String s, String t) {
if(t.length()> s.length()) return "";
Map<Character, Integer> map = new HashMap<>();
for(char c : t.toCharArray()){
map.put(c, map.getOrDefault(c,0) + 1);
}
int counter = map.size();
while(counter == 0){
char tempc = s.charAt(begin);
if(map.containsKey(tempc)){
map.put(tempc, map.get(tempc) + 1);
if(map.get(tempc) > 0){
counter++;
}
}
if(end-begin < len){
len = end - begin;
head = begin;
}
begin++;
}
}
if(len == Integer.MAX_VALUE) return "";
return s.substring(head, head+len);
}
}
you may find that I only change a little code above to solve the question "Find All
Anagrams in a String":
change
3/7
to
if(end-begin == t.length()){
result.add(begin);
}
4/7
public class Solution {
public int lengthOfLongestSubstringTwoDistinct(String s) {
Map<Character,Integer> map = new HashMap<>();
int start = 0, end = 0, counter = 0, len = 0;
while(end < s.length()){
char c = s.charAt(end);
map.put(c, map.getOrDefault(c, 0) + 1);
if(map.get(c) == 1) counter++;//new char
end++;
while(counter > 2){
char cTemp = s.charAt(start);
map.put(cTemp, map.get(cTemp) - 1);
if(map.get(cTemp) == 0){
counter--;
}
start++;
}
len = Math.max(len, end-start);
}
return len;
}
}
5/7
public class Solution {
public List<Integer> findSubstring(String S, String[] L) {
List<Integer> res = new LinkedList<>();
if (L.length == 0 || S.length() < L.length * L[0].length()) return res;
int N = S.length();
int M = L.length; // *** length
int wl = L[0].length();
Map<String, Integer> map = new HashMap<>(), curMap = new HashMap<>();
for (String s : L) {
if (map.containsKey(s)) map.put(s, map.get(s) + 1);
else map.put(s, 1);
}
String str = null, tmp = null;
for (int i = 0; i < wl; i++) {
int count = 0; // remark: reset count
int start = i;
for (int r = i; r + wl <= N; r += wl) {
str = S.substring(r, r + wl);
if (map.containsKey(str)) {
if (curMap.containsKey(str)) curMap.put(str, curMap.get(str) + 1);
else curMap.put(str, 1);
}
if (count == M) {
res.add(start);
tmp = S.substring(start, start + wl);
curMap.put(tmp, curMap.get(tmp) - 1);
start += wl;
count--;
}
}else {
curMap.clear();
count = 0;
start = r + wl;//not contain, so move the start
}
}
curMap.clear();
}
return res;
}
}
6/7
Find All Anagrams in a String
https://leetcode.com/problems/find-all-anagrams-in-a-string/
while(counter == 0){
char tempc = s.charAt(begin);
if(map.containsKey(tempc)){
map.put(tempc, map.get(tempc) + 1);
if(map.get(tempc) > 0){
counter++;
}
}
if(end-begin == t.length()){
result.add(begin);
}
begin++;
}
}
return result;
}
}
7/7