educative.io

Isn't using start_sub_array variable better approach than using the while loop to find the start of the minimum sub array?

def min_window(str1, str2):
# write your code here

index_s1, index_s2 = 0, 0

min_length = float('inf')
min_word = ''

while index_s1 < len(str1):

    if str1[index_s1] == str2[index_s2]:

        
        index_s2 += 1

        if index_s2 == 1:
            start_sub_array = index_s1


        if index_s2 == len(str2):

            # start,end = index_s1, index_s1 + 1

            


            # index_s2 -= 1

            # while index_s2 >= 0:
            #     if str1[start] == str2[index_s2]:
            #         index_s2 -= 1

            #     start -= 1

            # start = start + 1


            if min_length > index_s1-start_sub_array + 1:
                min_word = str1[start_sub_array:index_s1+1]
                
                min_length = index_s1-start_sub_array + 1
                print(min_length,min_word)
            index_s2 = 0

    index_s1 += 1

return min_word

Course: Grokking Coding Interview Patterns in Python - Learn Interactively
Lesson: Minimum Window Subsequence - Grokking Coding Interview Patterns in Python

Hey @Amit_Upreti

Hey, I have reviewed your code for the problem, and it appears it is not working correctly. While your approach is theoretically a more efficient way to solve the problem, it fails a significant chunk of test cases. An example test case is s1 =“bmgblbbpntpacpslgiiyk” and s2 = “bpa”, the output should be “bpntpa”, but the code provided is returning “bmgblbbpntpa”.

I understand that debugging the code can be frustrating, but it’s an integral part of the learning process. Keep in mind that the journey to understanding a problem and finding a solution is just as important as the solution itself, and you thinking of alternate solutions is delightful to see.Please let me know if you have further questions or need additional help. I am here to support you.

Happy Learning!