Count Occurrences of Anagrams | Made Super Easy | Amazon | Microsoft | Flipkart | codestorywithMIK
HTML-код
- Опубликовано: 11 сен 2024
- This is the first video on Sliding Window Playlist .
We will be going through Sliding Window in the easiest way possible and will make it one of the easiest topics.
Today, we will do our very first Qn 'Count Occurrences of Anagrams'
Problem Name : Count Occurences of Anagrams
GfG Link : practice.geeks...
My solutions on Github : github.com/MAZ...
Company Tags 😱🤯 : Amazon, Intuit, Microsoft, Flipkart
My GitHub Repo for interview preparation : github.com/MAZ...
Subscribe to my channel : / @codestorywithmik
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
#coding #helpajobseeker #easyrecipes
#interviewpreparation #interview_ds_algo #hinglish
this channel is a hidden gem
It means a lot ❤️🙏😇
No one explaining like you bro in RUclips...mind blowing..I m fan of you bro..
Thank you so much Parwez ❤️❤️❤️
Means a lot
amazing explanation, i watched around 3-4 videos but this explanation hits my mind thanks! keep uploading!!
i am now addicted to your channel bro. Thanks for providing all this
Means a lot 😇❤️
Already solved the problem by my own just came here to listen to your story😅😅
Add more Videos MIK Bro in this playlist❤
I know u upload on the basis of daily challenges but still try if u can 😄😊
Was able to solve it on my own
Thank you so much ❤️🙏
MOST UNDERRATED channel ever ❣❣
Started this playlist today. Will binge watch 🫡❤️
Wow! kudos bhai badhiya pdh rhe ho
This is the best explanation man. Thanks a lot
froxx mai counter array mai x ki bhi frequecy bdri hai wo kaise handle hoora hai
best explanation of this problem 💯
Thanks a lot Gaurang ❤️
That's very easy to understand. Thankyou
You are welcome! ❤️😇
Very good explanation! Please create a video of how to calculate the TC of different data structures and its application in coding problems
bhaiya done with leetcode easy and 2 pointers starting with sliding window playlist .
what a explanation broooo❤❤❤❤❤❤
Thank you so much 😊
Nicely explained
Thanks a lot ❤️🙏
Thank you 👍
Another Code: Same Approach (Sliding window)
int search(string pat, string txt) {
vector pcnt(26,0);
vector tcnt(26,0);
int k = pat.length();
for(char &ch: pat){
pcnt[ch-'a']++;
}
int i=0,j=0,n=txt.length();
int ans=0;
while(j
Legend ❤
reach++
Tysm ❤️❤️❤️
sir pta kse chala that its sliding window ques pls include these concepts in videos
Why i am Getting TLE on using unordered map instead of array??
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)):
counter[ord(s[j])-ord('a')]-=1
while(j-i+1==k):
if self.check(counter):
ans.append(i)
counter[ord(s[i])-ord('a')]+=1
i+=1
return ans
using o(26) frequency array done ✅
Good explanation , but not select colour.. or edit that part .. it will be good for eyes
Sure 😇❤️🙏
as per this logic, Here you are updating the counter even for chars which are not in the pattern. should we check if that character is part of pattern first
We need to decrement counter even if that character isn't in pattern since that window is invalid. The character not in pattern will make the freq/counter for that character negative. Hence, that window will never contribute to ans since all zero will return false in that case which is right since that window isn't anagram of pattern. A window of size patternSize (k) is valid anagram only if allZero returns true for it.
Please add java code in all videos most of us are using c++ and java in implementing in DSA .
Noted Abhishek.
Generally me or one of my subscribers J J put the Java code from now on always in the comment section
@@codestorywithMIK Thanks you always put java code also 🙏
import java.util.Arrays;
class Solution {
public static boolean allZero(int[] count) {
return Arrays.equals(count, new int[26]);
}
public static int search(String pat, String txt) {
int k = pat.length();
int[] count = new int[26];
for (char ch : pat.toCharArray()) {
count[ch - 'a']++;
}
int i = 0, j = 0;
int n = txt.length();
int result = 0;
while (j < n) {
int idx = txt.charAt(j) - 'a';
count[idx]--;
if (j - i + 1 == k) {
if (allZero(count)) {
result++;
}
count[txt.charAt(i) - 'a']++;
i++;
}
j++;
}
return result;
}
}
d={}
map={}
for i in p:
d[i]=d.get(i,0)+1
for i in d:
map[i]=0
i=0
ans=[]
for j in range(len(s)):
if s[j] in map:
map[s[j]]+=1
while(j-i+1==len(p)):
if map==d:
ans.append(i)
if s[i] in map:
map[s[i]]-=1
i+=1
return ans
Done :) using hashmap
🙌🙌🙌
thank you boos just watched your video and solved a simillar problem in leetcode .
hats off @codestorywithMIK
Bhaiya,what is the time complexity for the better solution,how is it better than the brute force
O(26N) ===>>O(N)
bhaiya leetcode ka bhi question link diya kariye
Sure. I always add Leetcode and gfg link as well in my current videos. Let me add for this too
what is the time complexity of sliding widow approach
The time complexity is linear O(n)
Inside the for loop we call allZero() which will take constant time as we only compare for 26 sized array for 26 alphabets which is constant.
So overall complexity remains O(n)
@@codestorywithMIK thanx ✌ .. New subscriber🙂..
Tysm ❤️
can anyone tell me why my code give TLE?? i am using a map
bool allZero(map mp, string &pat){
for(int i=0; i
Java code bhi de diya kijiye sir 🙏🙏🙏🙏
Sure Abhishek,
I will try to include java also
can someone give code in javascript
Wouldnt the time complexity of this also be O(m*n)?
I think 26*m(size of bigger string) ,26 because counter Length is constant
anyone plese can you give same code in JAVA 🙏🙏
JAVA code added in the github link in the description also
class Solution {
private boolean allZero(int[] count) {
for (int num : count) {
if (num != 0) {
return false;
}
}
return true;
}
int search(String pat, String txt) {
int k = pat.length();
int[] count = new int[26];
Arrays.fill(count, 0);
for (char ch : pat.toCharArray()) {
count[ch - 'a']++;
}
int i = 0, j = 0;
int n = txt.length();
int result = 0;
while (j < n) {
int idx = txt.charAt(j) - 'a';
count[idx]--;
if (j - i + 1 == k) {
if (allZero(count)) {
result++;
}
count[txt.charAt(i) - 'a']++;
i++;
}
j++;
}
return result;
}
}
can any please check my code it giving 0 as answer always
int search(string pat, string txt) {
int p[26]={0};int t[26]={0};
const char *s = pat.c_str(); int len=0;
while (*s)
{
p[*s-'a']++;
s++; len++;
}
int count=0; int flag=1;
const char *x = txt.c_str(); int lent=0; const char *first = txt.c_str();
while(*x)
{
t[*x-'a']++;
if(lent>=len) { flag=1;
for(int row=0;row