1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int findSubstring(String s) {
int[] map = new int[256];
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.length()) {
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;
}

Related Problems:

Ref: https://discuss.leetcode.com/topic/30941/here-is-a-10-line-template-that-can-solve-most-substring-problems