I am not getting the correct window
Hello @akh.chakraborty11,
There are multiple issues that you are required to correct:
-
Is there only one condition when the substring containing all characters of string2 does not exists:
s.length()<pattern.length()
Write logic for other possibilities also, when No String should be printed. -
The logic you have applied is not correct to find minimum substring
Approach:
First check if the length of string is less than the length of the given pattern, if yes then “no such window can exist “. -
Store the occurrence of characters of the given pattern in a freqOfPattern.
-
Start matching the characters of pattern with the characters of string i.e. increment count if a character matches.
2.1 start traversing the string
2.2 count occurrence of characters of string using freqOfString
2.3 If string’s char matches with pattern’s char then increment count -
Check if (count == length of pattern ) this means a window is found.
-
If such window found, try to minimize it by removing extra characters from the beginning of the current window.
-
Update min_length.
-
Print the minimum length window, if exist.
-
if no window found i.e. start_index=-1, then print No String.
Note:
start_index keeps record of the starting index of the shortest window, initialised with -1.
Update it whenever a window smaller than earlier detected window is found.
Hope, this would help.
Give a like, if you are satisfied.
Consider the below figure for better understanding:
Input: string = “this is a test string”, pattern = “tist”
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.