Problem
Submissions

Solution: Minimum Window Subsequence

Statement

Naive approach

The naive approach would be to generate all possible substrings of str1 and then check which substrings contain str2 as a subsequence. Out of all the substrings in str1 that contain str2 as a subsequence, we’ll choose the one with the shortest length. Now, let’s look at the cost of this solution. We need two nested loops to get all possible substrings and another loop to check whether each substring contains all the required characters. This brings the time complexity to O(n3)O(n^{3}). Since we’re not using any extra space, the space complexity is O(1)O(1).

Optimized approach using sliding window

The algorithm finds the smallest subsequence in one string that contains all characters of another string in order. It works as follows:

  1. One pointer traverses through the first string character by character, while the second pointer tracks progress in the second string.

    I. When a character in the first string matches the current character in the second string, the second pointer moves to the next character in the second string.

    II. Regardless of whether a match is found, the first pointer continues traversing the first string.

    III. Once all characters of the second string are matched, the algorithm backtracks and locates the smallest valid subsequence.

  2. Once all the characters of the second string are matched, the algorithm backtracks. It moves the starting pointer leftward in the first string to find the smallest subsequence that contains all characters of the second string.

    I. Backtracking is necessary because, after finding all characters of the second string in order, the algorithm minimizes the window size by removing unnecessary characters from the beginning of the matched subsequence.

  3. After a subsequence is found, the algorithm continues searching for other possible subsequences by resetting the first pointer to the new starting position, resetting the second pointer to the beginning, and resuming the search for matches in the first string. The process repeats to find all possible valid subsequences.

  4. The algorithm keeps track of the smallest valid subsequence found so far, updating it whenever a smaller subsequence is located during the backtracking process. The smallest subsequence is determined by its length.

  5. The process continues until the entire string is traversed and the smallest subsequence is returned.

Note: In the following section, we will gradually build the solution. Alternatively, you can skip straight to just the code.

Problem
Submissions

Solution: Minimum Window Subsequence

Statement

Naive approach

The naive approach would be to generate all possible substrings of str1 and then check which substrings contain str2 as a subsequence. Out of all the substrings in str1 that contain str2 as a subsequence, we’ll choose the one with the shortest length. Now, let’s look at the cost of this solution. We need two nested loops to get all possible substrings and another loop to check whether each substring contains all the required characters. This brings the time complexity to O(n3)O(n^{3}). Since we’re not using any extra space, the space complexity is O(1)O(1).

Optimized approach using sliding window

The algorithm finds the smallest subsequence in one string that contains all characters of another string in order. It works as follows:

  1. One pointer traverses through the first string character by character, while the second pointer tracks progress in the second string.

    I. When a character in the first string matches the current character in the second string, the second pointer moves to the next character in the second string.

    II. Regardless of whether a match is found, the first pointer continues traversing the first string.

    III. Once all characters of the second string are matched, the algorithm backtracks and locates the smallest valid subsequence.

  2. Once all the characters of the second string are matched, the algorithm backtracks. It moves the starting pointer leftward in the first string to find the smallest subsequence that contains all characters of the second string.

    I. Backtracking is necessary because, after finding all characters of the second string in order, the algorithm minimizes the window size by removing unnecessary characters from the beginning of the matched subsequence.

  3. After a subsequence is found, the algorithm continues searching for other possible subsequences by resetting the first pointer to the new starting position, resetting the second pointer to the beginning, and resuming the search for matches in the first string. The process repeats to find all possible valid subsequences.

  4. The algorithm keeps track of the smallest valid subsequence found so far, updating it whenever a smaller subsequence is located during the backtracking process. The smallest subsequence is determined by its length.

  5. The process continues until the entire string is traversed and the smallest subsequence is returned.

Note: In the following section, we will gradually build the solution. Alternatively, you can skip straight to just the code.