SubString Question Tip

Key for Substring Questions is to use to pointer start & end til the substr = str[start:end] meets the condition.

So, you can use the template like below.



String minWindow(String givenStr, String target)


{

    //charCount is a map that holds counts of every character of target

    //it also means the numbers of each character that should be found in the substring

    Map<Character, Integer> charCount = new HashMap<Character, Integer>();


    for (int  i = 0;  i < 128; i++)

    {

        charCount.put((char)i, 0)

    }


    for(char c : givenStr.toCharArray())

    {

        charCount.put(c, charCount.get(c) + 1);

    }


    int start = 0; //pointer used for iteration


    int end = 0; //pointer used for iteration


    int counter = 0; //counter used to determine whether the window is valid, can vary(by the condition)

    //in this case, number of chars of target found in givenStr


    int subStart = 0; //variable that saves the head of substring to return


    int subLen = Integer.MAX_VALUE; //variable that saves the length of substring to return, can vary(minimun length or maximum length)

    //in this case, Integer.MAX_VALUE


    int size = givenStr.length();



    //outer loop moves the 'end' forward to find the valid window

    //if the window is valid, it pauses and proceeds into the inner loop

    while (end < size)

    {

    // manipulate counter properly

    // in this case, increase counter if a character of end exists belongs to the target

    Character endChar = givenStr.charAt(end);


    if (charCount.get(endChar) > 0)

    {

        counter++;

    }


    //change the charCount relevantly

    charCount.put(endChar, charCount.get(endChar) - 1);


    //change end for the next iteration

    end++;


    


    //when the window is valid, proceed into the inner loop(loop for 'start')

    //inner loop moves start forward to find optimal substring

    // in this case, take off the characters of the front to make the minimum length substring

    while(counter  == target.length())

    {

        //if current (start/end) has shorter length than the previous subLen, update the subLen

        //if this is about maximum length, it should be located in the line 'A'

        if ((end - start) < subLen) 

        {

            subStart = start;

            subLen = end - start;

        }    


       //change the charCount relevantly

        Character startChar = givenStr.charAt(start);

        charCount.put(startChar, charCount.get(startChar) + 1);


        //manipulate counter properly

        //in this case, decrease counter if a character of start belongs to the target

        if (charCount.get(startChar) > 0)

        {

            counter--;

        }


        //change start for the next iteration

        start++;

    }

    //A

    //For maximum length, the update should happen after the inner loop ends.

    }


    // if the iterations over, return the substring by acquired subStart and subLen

    if (subLen != Integer.MAX_VALUE)

        return givenStr.substring(subStart, subStart + subLen);

    else

        return "";

}


댓글

이 블로그의 인기 게시물

Interface of Java

Data Analytics Overview(OLTP vs OLAP)

Leetcode