educative.io

Why reverse parse?

Why do we need to parse S1 in reverse to get the length when we have found all characters of the S2?
We could have also stored the index when first matching character was found and subtract it from the index of last matching character.

Also, the console output does not always show when I try my code. For this question, when my code passed all the tests, it did not show the console output. Can you make sure the console output always shows?


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


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

1 Like

Hello,
The solution you’re referring to is only possible when there is no repeated letter in the S1, the one example we have in the slide. If there are repeated letter in the S1 let’s say:

S1 = "qwewerrty"
S2 = "werty"

When index1 traverse through the S1, it will find ‘w’ at index = 1, but in order to have a minimum subsequence, the ‘w’ we required is at index = 3. That’s the reason we need to parse S1 in reverse in order to cater duplicates and find the nearest letter in the string.

1 Like