String - 13: Find all anagrams present in String for a given pattern | Java Implementation

preview_player
Показать описание
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 now becomes 0, then we increase the matchedElement variable by 1
- If matchedElement becomes equal to size of map or unique characters in pattern then we've found our anagram & we add starting index in final List
- 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 list containing all starting indexes, where we get anagrams

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

CHECK OUT CODING SIMPLIFIED

★☆★ VIEW THE BLOG POST: ★☆★

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

★☆★ SUBSCRIBE TO ME ON YOUTUBE: ★☆★

★☆★ Send us mail at: ★☆★
Рекомендации по теме
Комментарии
Автор

Hi! great videos very one doubt is there a condition that the pattern given must have unique characters? This wont work if say the pattern is aabcd or something right?

vivekseelam
Автор

Hi CodingSimplified. Could you please add source code to all of your Playlists. The link provided below is showing "Connection closed" error. I have been following your course only for DSA preparation(I completed arrays 53 programs now in strings). It is well structured.

sureshMekarthi
Автор

This code will not work when input String is "abccbabc" and pattern is "abc"

kumudbhardwaj
Автор

hi again, you r doing great, im currently in binary tree. so could you please make a video on time and space complexity with all fundamentals, that would be very helpful.

krishnamohanty