Sliding Window

https://github.com/liyin2015/Algorithms-and-Coding-Interviews/blob/master/two_pointer.pdf

Use sliding window technique and store all characters in a int[256] map. Outer while loop use to expand window, inner while loop to shrink window. Update every time.

public int lengthOfLongestSubstring(String s) {
    if (s == null || s.isEmpty()) {
        return 0;
    }
    int res = 0;
    int[] map = new int[256];
    int L = 0, R = 0; 
    while (R < s.length()) {
        map[s.charAt(R)]++;
        while (map[s.charAt(R)] > 1) {
            map[s.charAt(L)]--;
            L++;
        }
        res = Math.max(res, R - L + 1);
        R++;
    }
    return res;
}

159. Longest Substring with At Most Two Distinct Characters

Use a unique count variable to count how many unique characters are in the window. if map[s.charAt(R)] == 1, increase the count, when map[s.charAt(L)] == 0, decrease the count.

340. Longest Substring with At Most K Distinct Characters

Change 2 above to k.

76. Minimum Window Substring

Use sliding window to keep track of the records. HashMap to record char and its count. Preprocess target string with map. Use char count == 0 and length count == t.length() to represent when all records are found

String: ADOBECODEBANC

j i

  • If (map.contains(nums[i]) reduce count of that char, when count >=0 , foundCount++;

  • While foundCount == t.length(). Update result. And reduce window size by moving j.

  • When map.contains(nums[j]), count of char ++, when map.get(char[j]) > 1, foundCount--; and j++;

438. Find All Anagrams in a String

  • 先做一个Map来存P里面的character数,用一个count来存找到了多少个char

  • 右边一直走,把碰到的c在map里减少一个,每减少一个 (map[R]-- > 0) 说明找到一个p中的字符,count++

  • 在R - L + 1 == p.length()的情况下,说明找到了一个和P长度一样的Window,这时候看count也等于p.length()的话,说明是一个解

  • 左边if (map[L]++ >= 0) count--; left++;

1456. Maximum Number of Vowels in a Substring of Given Length

395. Longest Substring with At Least K Repeating Characters

求最长的,每个Char重复至少K次的substring。这个就不能按照普通的SlidingWindow来做,因为不仅要统计每个char重复的次数,而且要看有几个char达到了要求。而且要求是一直在变的。从1到26个char都有可能。

N从1到26循环,每次都要找一下有几个字符,并且字符个数 >= k而且字符的个数和 >= k字符个数相等。在这种情况下更新长度

Use sliding winder to get the count of K different elements. Minus the result by K - 1 different elements. That is exactly K different elements.

713. Subarray Product Less Than K

485. Max Consecutive Ones

1100. Find K-Length Substrings With No Repeated Characters

This problem is hard to interpret. It means you have to select k cards from begin or end or both. This is actually a two pointer + sliding window problem. LSum represents the sum on left side, then slide to the left, RSum is the sum on the right side. LSum + RSum is one of the result.

https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/discuss/597825/Simple-Clean-Intuitive-Explanation-with-Visualization

30. Substring with Concatenation of All Words

  1. Get all words into a set of string combinations by using backtracking. Slide window and compare substring. First step took very long since backtracking takes O(k^2) where k is size of words

  2. Slide window.

    1. Save target words and their count to a target map.

    2. Use i to control left bound, j as right bound, to slide window. J is the index in words[], which also control length

    3. Create a new map (“seen”) every move, count the words between i and j. If there are unseen words, break. Otherwise put seen words in the “seen” map.

    4. 就把该单词存到新的 HashMap 中,并判断新的 HashMap 中该单词的 value 是不是大于之前的 HashMap 该单词的 value ,如果大了,就代表该子串不是我们要找的,接着判断下一个子串就可以了。如果不大于,那么我们接着判断下一个单词的情况。子串扫描结束,如果子串的全部单词都符合,那么该子串就是我们找的其中一个。

    5. T:假设 s 的长度是 n,words 里有 m 个单词,那么时间复杂度就是 O(n * m). S:两个 HashMap,假设 words 里有 m 个单词,就是 O(m)。

567. Permutation in String

如果S的长度小于T说明不可能,Return false

做两个Map,把S, T都count一下。

i来循环T,并且update 这个window的字符。看两个map是不是相等:

int len = s1.length();

for (int i = 0; i + len < s2.length(); i++) {

if (isSame(s1map, s2map)) return true;

s2map[s2.charAt(i + len) - 'a']++;

s2map[s2.charAt(i) - 'a']--;

}

return isSame(s1map, s2map);

Number of Substrings Containing All Three Characters

Count Number of Nice Subarrays

Replace the Substring for Balanced String

Max Consecutive Ones III

Binary Subarrays With Sum

Subarrays with K Different Integers

Fruit Into Baskets

Shortest Subarray with Sum at Least K

Minimum Size Subarray Sum

424. Longest Repeating Character Replacement

2962. Count Subarrays Where Max Element Appears at Least K Times

2067. Number of Equal Count Substrings

2831. Find the Longest Equal Subarray

Last updated

Was this helpful?