For most substring problem, we are given a string and need to find a substring of it which satisfy some restrictions. A general way is to use a hashmap assisted with two pointers. The template is given below.

int findSubstring(string s){
        vector<int> 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;
  }

Longest Substring with At Most

Minimum Window Substring

Permutation in String

Find All Anagrams in a String

Substring with Concatenation of All Words

Longest Substring with At Most 2 Distinct Characters

Longest Substring with At Most K Distinct Characters

Longest Substring Without Repeating Characters

Longest Substring with At Least K Repeating Characters--->different


when you have a S and a dict

think about preprocess the dict : you may use trie, but the code is quite long so it has low frequency appearing in the interviews.

think about preprocess the S

results matching ""

    No results matching ""