Count Substrings with K Distinct Characters | Hashmap Interview Questions

Поділитися
Вставка
  • Опубліковано 19 жов 2024

КОМЕНТАРІ • 78

  • @tushard3736
    @tushard3736 2 роки тому +37

    This question was asked for me in amazon SDE1 On-campus online assessment 2022

  • @aasifali9139
    @aasifali9139 Рік тому +5

    thanks sir. The way you did the dry run made the concepts clear for this ques

  • @kumarpriyansh4238
    @kumarpriyansh4238 3 роки тому +28

    Anyone facing difficulty in understanding...
    Exactly K distinct = Atmost K distinct - Atmost K-1 distinct
    and you can find Atmost K distinct in 0(n) .....;)

  • @stith_pragya
    @stith_pragya 7 місяців тому +1

    Thank You So Much for this wonderful video................🙏🙏🙏🙏🙏🙏

  • @ManishKumar-zk5ky
    @ManishKumar-zk5ky Рік тому +1

    very nice and easy explanation sir
    Thanks a lot for such nice content
    please continue on youtube sir🙏

  • @rishabhkumar2062
    @rishabhkumar2062 8 місяців тому

    i tried this for 2 days continuously without any help , still was able to reach only n square ,,This method takes O(n) ,Thank you!! but i believe it can be done using a single hashmap and within 30-40 lines of code ,using atmost k substring logic

  • @aman6561
    @aman6561 4 роки тому +2

    Sir abhi count of at most ki video dekhi hai
    to simple bhi to kr skte hai isko count at most k minus count of at most (k-1) krke?

  • @LegitGamer2345
    @LegitGamer2345 3 роки тому +5

    a tough one!!
    as always , awesome explanation :)

    • @LegitGamer2345
      @LegitGamer2345 3 роки тому

      this can be easily done by doing count of atmost k - count of atmost k-1
      but for k = 1 answer will be same as count of atmost k

    • @raviashwin1157
      @raviashwin1157 3 роки тому +1

      @mayank sharma class Solution
      {
      public:
      long long int solve(string s,int k){
      int n=s.size();
      int i=0; //starting point of the window
      int j=0; //ending point of the window
      long long int ans=0;
      int dist_cnt=0;
      int hash_count[26]={};
      while(jk){
      hash_count[s[i]-97]--;
      if(hash_count[s[i]-97]==0){
      dist_cnt--;
      }
      i++;
      }
      ans+=j-i+1;
      j++;
      }
      return ans;
      }
      long long int substrCount (string s, int k)
      {
      //code here.
      long long int ans=solve(s,k)-solve(s,k-1);
      return ans;
      }
      };

  • @ravitiwari2160
    @ravitiwari2160 Рік тому

    Sir u r really great.. god bless u sir

  • @mickyman753
    @mickyman753 3 роки тому +3

    you can use this loop too for finding answer for k=1
    public static int solutionForOne(String str)
    {
    int ans=1;
    int count=1;
    for(int i=1;i

  • @adilkhatri7475
    @adilkhatri7475 2 роки тому +3

    ye vala reupload hona cheye sir iske intuition thik se samje nhi😞

  • @harshtekriwal131
    @harshtekriwal131 4 роки тому +4

    Sir isme count of atmost K - count of atmost k-1 nikal ke kia hai. wo zyada easy hai

    • @Pepcoding
      @Pepcoding  4 роки тому +1

      bilkul sahi. dhyaan se dekhie, ghoom firr ke ye wahi kar rha hai

  • @Yash34567
    @Yash34567 Рік тому

    easiest c++ soln using hash map
    int hash[26];
    int count,index,ans=0;

    for(int i=0;i

  • @raghavgarg8024
    @raghavgarg8024 3 роки тому +2

    sir intermediate wala module kab tak complete upload karoge

    • @Pepcoding
      @Pepcoding  3 роки тому +5

      Hanji beta we are very near now. Feeling very strong about 10th Jan. Finances ki tension thi, aadhi si solve ho gai, baaki ke workflows ban gae. I will get back to what i enjoy - making videos

    • @raghavgarg8024
      @raghavgarg8024 3 роки тому +1

      @@Pepcoding ok sir thank you for replying

  • @harshalgarg1676
    @harshalgarg1676 7 місяців тому

    nicely explained

  • @Prashantkumar-pn6qq
    @Prashantkumar-pn6qq 3 роки тому +1

    Sir isko longesr substring with k distinct me jab bhi ans update kar rhe h waha counter update karke nhi kar sakte?

    • @Pepcoding
      @Pepcoding  3 роки тому

      Beta, I regret to inform you that, I won't be able to answer/solve the personal doubts of each and every student over here. For clearing your doubts, you can join our community on telegram - t.me/pepcoding.

    • @virenderkumar9480
      @virenderkumar9480 3 роки тому

      bhai agar tab count ko increment karege toh choti substring jo characters ko release karte hue ayyi h ve reh jaegi
      aabcbcdbca ismai karke dekh (ab) reh jaega or kuch or bhi

    • @loserfruit9663
      @loserfruit9663 3 роки тому +1

      @@virenderkumar9480 thanks buddy,it helped a lot

    • @indranilchakraborty5949
      @indranilchakraborty5949 3 роки тому

      thanks... you save my efforts!!

  • @prashantbhalla5278
    @prashantbhalla5278 Рік тому +1

    can someone explain why we are using two hashmap cant we solve it using one hashmap like the earlier one

    • @shantanu2612
      @shantanu2612 8 місяців тому

      Its because the count of charachter itsel will be the number of substrings formed for the k=1 case but for the other cases we have to find the difference to calculate number of substrings formed for that particular instance.

  • @prakashsingh1107
    @prakashsingh1107 Рік тому

    Wow sir amazing 😍😍

  • @LegitGamer2345
    @LegitGamer2345 3 роки тому +2

    this can be easily done by doing count of atmost k - count of atmost k-1
    but for k = 1 answer will be same as count of atmost k

    • @PriyanshuKumar-wu4wk
      @PriyanshuKumar-wu4wk 3 роки тому +1

      can you explain the intuition behind this?

    • @pravinmudaliyar3421
      @pravinmudaliyar3421 2 роки тому

      @@PriyanshuKumar-wu4wk Check Leetcode 992 discussion section's most voted answer

  • @TheIndianChroniclee
    @TheIndianChroniclee 3 роки тому +1

    Excellent Explaination Sir ...

    • @Pepcoding
      @Pepcoding  3 роки тому

      Thankyou beta!
      I am glad you liked it. I hope that you are watching till the end and trying to understand what, how, and especially why of the problem.
      If you like our efforts, will you like to write a few words about us here (www.quora.com/What-are-the-good-websites-to-learn-data-structures-and-algorithms )

  • @Tarun_Kumar_bhu
    @Tarun_Kumar_bhu 3 роки тому

    Nicely explained

  • @krishnendudutta1465
    @krishnendudutta1465 2 роки тому +4

    same code is giving TLE in nados

    • @rajarajanr
      @rajarajanr Рік тому

      Use arrays instead of hashmap, arrays could reduce the time taken.

    • @ashwinnema06
      @ashwinnema06 Рік тому

      Use C++or java as language

    • @KuldeepSingh-yt4xg
      @KuldeepSingh-yt4xg Рік тому

      @@ashwinnema06 gives output limit exceeded

  • @shivangagrawal2923
    @shivangagrawal2923 3 роки тому

    master piece

  • @trevorPhillips284
    @trevorPhillips284 2 роки тому +1

    Approach samajh gya implement karne me time lag gya par 1 ghante me khudse hogya

    • @Pepcoding
      @Pepcoding  2 роки тому +1

      Good.
      For better experience, visit nados.io, where you will get well curated content and career opportunities

  • @shreyashachoudhary480
    @shreyashachoudhary480 2 роки тому

    Amazing!

    • @Pepcoding
      @Pepcoding  2 роки тому

      Glad you think so, for better experience and well curated content sign up on nados.io and start learning.

  • @aman6561
    @aman6561 4 роки тому

    sir ye video site pr nhi hai

  • @rahulranjan7567
    @rahulranjan7567 3 роки тому +2

    Sir 2 Sliding window ka intuition aayega kaise?

  • @amanagarwal9700
    @amanagarwal9700 3 роки тому

    sir why we are running loop till str.length-1;

    • @amanagarwal9700
      @amanagarwal9700 3 роки тому

      @Shubham Kumar Thanks for the clarification

    • @rounaq_khandelwal
      @rounaq_khandelwal Рік тому

      bro pta chlaa? str.length()-1 tak loop run kyo ho rha h?

    • @divyanshuagarwal1912
      @divyanshuagarwal1912 Рік тому +1

      @@rounaq_khandelwal bhai apn increment starting mai krta hai isliya woh str.length()-1 krta hai warna idx out of bound hozaya ga

    • @rounaq_khandelwal
      @rounaq_khandelwal Рік тому +1

      @@divyanshuagarwal1912 thanks bud!

    • @divyanshuagarwal1912
      @divyanshuagarwal1912 Рік тому

      @@rounaq_khandelwal which year bro

  • @enigma2886
    @enigma2886 2 роки тому +2

    Aaj amazon ne pucha , meri fath gai , Kash pahle dekha hota.... :(

    • @Pepcoding
      @Pepcoding  2 роки тому +1

      Bhai, nados.pepcoding.com pe ye sara content free of charge hai. Web dev hai aur core bhi

    • @enigma2886
      @enigma2886 2 роки тому

      @@Pepcoding i know bhaiya, mane to course bhi le rakaha ha pep pe, bhot questions kiye the, almost 500, bas yahi nahi kiya tha :(

    • @araragikoyomi7186
      @araragikoyomi7186 2 роки тому

      interviewer ne hint nahi diya kuch? mujhse bhi nahi ho raha tha par gfg pe at most k characters ka hint dekh kar khud se kar liya maine to interview me hints nahi dete kya aisa?

  • @aakash1763
    @aakash1763 3 роки тому +2

    Kaafi mushkil tha question yaar

  • @ronaktyagi2691
    @ronaktyagi2691 3 роки тому +2

    Can anyone please tell me this code's Time Complexity?

    • @mickyman753
      @mickyman753 3 роки тому +2

      O(2n)==O(n) ,we are adding n times to hashmap and we are removing n times ,so doing 2n operations (or 4n for 2 hashmaps ,still O(n))

  • @ashwinnema06
    @ashwinnema06 Рік тому +1

    If I get a job above 15 LPA by studying your course I promise you that I will pay your course fees through donating money on youtube

    • @rahulbhagat4023
      @rahulbhagat4023 Рік тому

      Mili ?

    • @ashwinnema06
      @ashwinnema06 Рік тому

      @@rahulbhagat4023 abhi tak toh nhi abhi apply nhi kar sakta mera March tak ka contract he

    • @rahulbhagat4023
      @rahulbhagat4023 Рік тому

      So it means u r already placed somewhere

    • @ashwinnema06
      @ashwinnema06 Рік тому

      @@rahulbhagat4023 Yes I just want a huge hike

  • @thecoderstv22
    @thecoderstv22 25 днів тому

    confusing......

  • @divyanshbarar3840
    @divyanshbarar3840 2 роки тому +2

    :(