Sliding Window 3 Non fix size longest

Template of Longset Problem

int slow = 0;
int fast = 0;
[slow, fast]
while (fast < S.length()) {
    // 1. add S[fast] into the sliding window
    // 2. move slow to the correspoding position, update global min/max value;
    while (window not staisfes the property) {
        // 1. remove slow from the window
        slow++;
    }
    // here slow is the lestmost position satisfying the property
    // !! make sure you understand clearly about the termination of the while loop
    // 1. update global longest. [slow, fast]
    fast++;
}

fast:相当于是遍历

slow相当于是保证找到符合条件的

Q1 Find the longest substring withou duplciate characters

Q1.1 Given a string containing only lowercase English lettesr, how many of its substrings satisfy the requirement of 'containing duplicate characters'

Q2 Find the longest substring with at most k distinct characters.

Work with Stream

Q3 Desgin a counter class

常用design类似的题目

  1. understand the requirements and use cases, assumptions

  2. design the API -- public methods, define the input parameters, return values, corner case execptions

  3. determin the member fields - internal states - access modifiers

  4. implement APIs, decouple the internal logics with private method (helper methods)

  5. implement the constructor - corner cases. initialize the member fields.

Last updated