String - 14: Smallest Sub-string which contains all the characters of given Pattern | Java Code

Published: 27 May 2020
on channel: Coding Simplified
13,149
201

Source Code:https://thecodingsimplified.com/small...
Solution:
We create a map of frequency of characters present in pattern
Now we start from 1st index & if that character present in map, then we decrease the frequency by 1 & we check if the frequency is greater than 0, then we increase the matchedElement variable by 1
If matchedElement becomes equal to size of pattern then we've found our anagram & we store minimum length & starting point at this moment
Now we increase start & go until matchedElement is equal to pattern size. At every moment we update the starting point & min length if end - start + 1 is less than minLength
Now if any element is moving out of window, we check if it's part of map, then increase the frequency by 1 & if the frequency is 0 then decrease the matchedElement by 1
At last, we return substring starting from subStringStartIndex to subStringStartIndex + minLength

Time Complexity: O(n + m)
Space Complexity: O(m)

CHECK OUT CODING SIMPLIFIED
   / codingsimplified  

★☆★ VIEW THE BLOG POST: ★☆★
http://thecodingsimplified.com

I started my YouTube channel, Coding Simplified, during Dec of 2015.
Since then, I've published over 400+ videos.

★☆★ SUBSCRIBE TO ME ON YOUTUBE: ★☆★
https://www.youtube.com/codingsimplif...

★☆★ Send us mail at: ★☆★
Email: thecodingsimplified@gmail.com


On this page of the site you can watch the video online String - 14: Smallest Sub-string which contains all the characters of given Pattern | Java Code with a duration of hours minute second in good quality, which was uploaded by the user Coding Simplified 27 May 2020, share the link with friends and acquaintances, this video has already been watched 13,149 times on youtube and it was liked by 201 viewers. Enjoy your viewing!