Find All Anagrams in a String-(Amazon, Microsoft, Flipkart):Explanation ➕ Live Coding 🧑🏻‍💻👩🏻‍💻

preview_player
Показать описание
This is the 2nd video on Sliding Window Playlist .
We will be going through the Sliding Window in the easiest way possible and will make it one of the easiest topics.

Today, we will do our very first Qn 'Find All Anagrams in a String'

Problem Name : Find All Anagrams in a String

Company Tags 😱🤯 : Amazon, Intuit, Microsoft, Flipkart

╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝

#coding #helpajobseeker #easyrecipes
#interviewpreparation #interview_ds_algo #hinglish
Рекомендации по теме
Комментарии
Автор

you gave the best explanation man.
reach++

sakshiarora
Автор

Same sliding window approach using unordered_map:-
class Solution {
public:
vector<int> findAnagrams(string s, string p) {
unordered_map<char, int> mp;
for(int i=0;i<p.size();i++){
mp[p[i]]++;
}
int i=0, j=0;
vector<int> ans;
unordered_map<char, int> mp2;
while(j<s.size()){
mp2[s[j]]++;
if(j-i+1<p.size()) j++;
else{
if(mp==mp2) ans.push_back(i);
/*if there is only a single frequency of a character then remove it completely from the map otherwise it will store char->0
*/
if(mp2[s[i]]==1){
mp2.erase(s[i]);
}else{
mp2[s[i]]--;
}
i++;
j++;
}
}
return ans;
}
};

ankitshaw_
Автор

Hi Bhaiya, bhot hi clear solution tha kya ek video manacher's algorithm pe bnaoge kya. YT pe tutorials pade h but aapse achha koi ni smjha skta so please wo ek request thi

ayusharyan
Автор

Java Code:-
class Solution {
boolean allzero(int[] counter){
for(int i=0;i<26;i++){
if(counter[i]!=0) return false;
}
return true;
}
public List<Integer> findAnagrams(String s, String p) {
int[] counter=new int[26];
for(int i=0;i<p.length();i++){
counter[p.charAt(i)-'a']++;
}
int i=0, j=0;
List<Integer> ans=new ArrayList<>();
while(j<s.length()){
counter[s.charAt(j)-'a']--;
if(j-i+1==p.length()){
if(allzero(counter)) ans.add(i);
counter[s.charAt(i)-'a']++;
i++;
}
j++;
}
return ans;
}
}

ankitshaw_
Автор

def check(self, counter):
for i in counter:
if i!=0:
return 0
return 1
def findAnagrams(self, s: str, p: str) -> List[int]:
k=len(p)
ans=[]
counter=[0 for i in range(26)]
for i in p:
counter[ord(i)-ord('a')]+=1
i=0
for j in range(len(s)):

while(j-i+1==k):
if self.check(counter):
ans.append(i)

i+=1
return ans

jagadeeshp