educative.io

Not able to unsterstand meaning of below line

int start = indexS1;
int end = indexS1 + 1;

why not
int start = indexS1-1;
int end = indexS1;


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

Hi @Nikhil_Pandey
We can’t use start = indexS1 - 1 and end = indexS1.
This is because if we change it then we also have to change more of the logic of the solution. Otherwise, we some of the test cases will fail. For example, consider the test case where str1 = “azjskfzt” and str2 = “sz”.
When indexS1 reaches the index of z in str1, indexS2 will be pointing to z in str2. If we now assign start = indexS1 - 1 and end = indexS1, then start will point to f in str1 and index2 will point to z in str2, and after each iteration start = start - 1 (start is decremented). In the second iteration, we would compare k in str1 with z in str2, and so on.
So we would never find z because we start to check from an element that is before z, and we’re moving in the reverse direction.
Hope this helps. Happy to discuss more!