I have recently come across an interesting question on strings. Suppose you are given following:
Input string1: "this is a test string"
Input string2: "tist"
Output string: "t stri"
So, given above, how can I approach towards finding smallest substring of string1 that contains all the characters from string 2?
You can do a histogram sweep in
O(N+M)time andO(1)space whereNis the number of characters in the first string andMis the number of characters in the second.It works like this:
hist2[ s2[i] ]++).Note that by varying the check you use on the histogram condition, you can choose either to have the same set of characters as the second string, or at least as many characters of each type. (Its just the difference between
a[i]>0 && b[i]>0anda[i]>=b[i].)You can speed up the histogram checks if you keep a track of which condition is not satisfied when you’re trying to satisfy it, and checking only the thing that you decrement when you’re trying to break it. (On the initial buildup, you count how many items you’ve satisfied, and increment that count every time you add a new character that takes the condition from false to true.)