关于我的 Leetcode 题目解答,代码前往 Github:https://github.com/chenxiangcyr/leetcode-answers
一个标准 Sliding Window 模板:
引用:https://leetcode.com/problems/minimum-window-substring/discuss/26808/here-is-a-10-line-template-that-can-solve-most-substring-problems
int findSubstring(string s){
vector map(128,0);
int counter; // check whether the substring is valid
int begin=0, end=0; //two pointers, one point to tail and one head
int d; //the length of substring
for() { /* initialize the hash map here */ }
while(end < s.size()){
if(map[s[end++]]-- ?){ /* modify counter here */ }
while(/* counter condition */){
/* update d here if finding minimum*/
//increase begin to make it invalid/valid again
if(map[s[begin++]]++ ?){ /*modify counter here*/ }
}
/* update d here if finding maximum*/
}
return d;
}
LeetCode题目:438. Find All Anagrams in a String
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s.
Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100.
The order of output does not matter.
Input:
s: "cbaebabacd" p: "abc"
Output:
[0, 6]
Explanation:
The substring with start index = 0 is "cba", which is an anagram of "abc".
The substring with start index = 6 is "bac", which is an anagram of "abc".
class Solution {
public List findAnagrams(String s, String t) {
List result = new LinkedList<>();
if(t.length()> s.length()) return result;
/* initialize the hash map here */
Map map = new HashMap<>();
for(char c : t.toCharArray()){
map.put(c, map.getOrDefault(c, 0) + 1);
}
// check whether the substring is valid
int counter = map.size();
//two pointers, one point to tail and one head
int begin = 0, end = 0;
while(end < s.length()) {
/* modify counter here */
char c = s.charAt(end);
if(map.containsKey(c)) {
map.put(c, map.get(c) - 1);
if(map.get(c) == 0) {
counter--;
}
}
end++;
/* counter condition */
while(counter == 0) {
// finding
if(end - begin == t.length()){
result.add(begin);
}
/* moving sliding window */
/* modify counter here */
c = s.charAt(begin);
if(map.containsKey(c)) {
map.put(c, map.get(c) + 1);
if(map.get(c) > 0){
counter++;
}
}
begin++;
}
}
return result;
}
}
LeetCode题目:76. Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
For example,
S = "ADOBECODEBANC"
T = "ABC"
Minimum window is "BANC".
class Solution {
public String minWindow(String s, String t) {
if(s == null || s.length() < t.length() || s.length() == 0){
return "";
}
/* initialize the hash map here */
HashMap map = new HashMap();
for(char c : t.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
// check whether the substring is valid
int counter = map.size();
//two pointers, one point to tail and one head
int begin = 0, end = 0;
int length = Integer.MAX_VALUE;
int head = 0;
while(end < s.length()) {
/* modify counter here */
char c = s.charAt(end);
if(map.containsKey(c)) {
map.put(c, map.get(c) - 1);
if(map.get(c) == 0) {
counter--;
}
}
end++;
/* counter condition */
while(counter == 0) {
// finding
/* update d here if finding minimum*/
if(end - begin < length) {
length = end - begin;
head = begin;
}
/* moving sliding window */
/* modify counter here */
c = s.charAt(begin);
if(map.containsKey(c)) {
map.put(c, map.get(c) + 1);
if(map.get(c) > 0){
counter++;
}
}
begin++;
}
}
return length == Integer.MAX_VALUE ? "" : s.substring(head, head + length);
}
}
LeetCode题目:3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
class Solution {
public int lengthOfLongestSubstring(String s) {
if(s == null || s.length() == 0){
return 0;
}
/* initialize the hash map here */
HashMap map = new HashMap();
// check whether the substring is valid
int counter = 0;
//two pointers, one point to tail and one head
int begin = 0, end = 0;
int length = Integer.MIN_VALUE;
while(end < s.length()) {
/* modify counter here */
char c = s.charAt(end);
map.put(c, map.getOrDefault(c, 0) + 1);
if(map.get(c) > 1) {
counter++;
}
end++;
/* counter condition */
while(counter > 0) {
/* moving sliding window */
/* modify counter here */
c = s.charAt(begin);
map.put(c, map.get(c) - 1);
if(map.get(c) > 0) {
counter--;
}
begin++;
}
/* update d here if finding maximum*/
if(end - begin > length) {
length = end - begin;
}
}
return length;
}
}