educative.io

Why is only the first subsequence a valid output?

for the test case
“abcdebdde” , “bde” why is only bcde a valid output? bbde should also be a valid output right? The question wants us to return any minimum subsequence not the first one?


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

Hello @HARSHITA_KRISHNA,

Thank you for your feedback. The string “bbde” cannot be a valid output because it does not appear as a contiguous string within str1. There are characters between the two “b”s, making “bbde” an invalid output. The only two valid subsequences are “bcde” and “bdde”, as they are both contiguous and have the shortest length.

For the second part of your query “The question wants us to return any minimum subsequence not the first one?” In the problem statement, if you take a look at the note at the end “If there are multiple minimum-length substrings that meet the subsequence requirement, return the one with the left-most starting index.” So following this rule, the returned substring would be “bcde”. You can have a look at the “Example” slides in the challenge lesson for more information.

I hope this clarifies your confusion regarding the lesson. Please feel free to share any further suggestions or feedback.

Happy learning!