> For the complete documentation index, see [llms.txt](https://ucan.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ucan.gitbook.io/notes/sliding-window-and-and-substring-problem.md).

# Sliding Window && Substring Problem

76\. Minimum Window Substring as example

<https://leetcode.com/problems/minimum-window-substring/discuss/26808/Here-is-a-10-line-template-that-can-solve-most-'substring'-problems>

```java
class Solution {
    public String minWindow(String s, String t) {
        int[] memo = new int[128];
        for(int i = 0; i < t.length(); i++){
            memo[t.charAt(i)]++;
        }
        
        int count = t.length(), left = 0, right = 0;
        int p1=0, p2=Integer.MAX_VALUE;
        while(right<s.length()){
            // first check if the char at right is in t
            // then check if count == 0 
            // (means the window contians all characters in t)
            if(memo[s.charAt(right++)]-->0) count--;
            /* counter condition */
            while(count == 0){
                /* update result here if finding minimum*/
                if (right-left<p2-p1) {
                    //left is inclusive
                    p1=left;
                    // right is exclusive because of right++
                    p2=right;
                }
                if (memo[s.charAt(left++)]++==0) count++;
            }
            /* update d here if finding maximum*/      
        } 
        return p2==Integer.MAX_VALUE ? "" : s.substring(p1, p2);
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ucan.gitbook.io/notes/sliding-window-and-and-substring-problem.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
