Is Subsequence | With Follow Up Solved | 2 Approaches | AMAZON | Leetcode - 392

Поділитися
Вставка
  • Опубліковано 6 січ 2025

КОМЕНТАРІ • 53

  • @09avishkargaikwad71
    @09avishkargaikwad71 Рік тому +13

    In my opinion this man going to change the entire community of Data Structure And Algorithm...
    Another next level video
    Looking for your contest solution video 😊😊

  • @FanIQQuiz
    @FanIQQuiz Рік тому +6

    "JAHANPANAH TUSSI GREAT HO, TOHFA KABOOL KARO" ❣❣❣❣

  • @UECAshutoshKumar
    @UECAshutoshKumar 10 місяців тому +2

    Thank you sir 😊

  • @codestorywithMIK
    @codestorywithMIK  Рік тому +7

    🎆Small Correction : Time Complexity of First Approach will be O(max(m, n)) because our for loop will not run more than max(m, n).
    Similar Problem (Follow Up) :
    Leetcode-792 - leetcode.com/problems/number-of-matching-subsequences/
    My Code for Leetcode-792 - github.com/MAZHARMIK/Interview_DS_Algo/blob/master/Arrays/Binary%20Search/Number%20of%20Matching%20Subsequences.cpp
    C++ & JAVA Code present in my Github link in the Description
    /*************************** JAVA ******************************/
    //Follow Up Approach
    //Approach-1 (Using Binary Search) -> This is an important concept for qns like Leetcode-792
    public class Solution {
    public boolean isSubsequence(String s, String t) {
    Map mp = new HashMap();
    for (int i = 0; i < t.length(); i++) {
    char ch = t.charAt(i);
    mp.computeIfAbsent(ch, k -> new ArrayList()).add(i);
    }
    int prev = -1;
    for (char ch : s.toCharArray()) {
    if (!mp.containsKey(ch))
    return false;
    List indices = mp.get(ch);
    int index = Collections.binarySearch(indices, prev + 1);
    if (index < 0)
    index = -index - 1;
    if (index == indices.size())
    return false;
    prev = indices.get(index);
    }
    return true;
    }
    }
    //Approach-2 (Simplest O(n) approach)
    public class Solution {
    public boolean isSubsequence(String s, String t) {
    int m = t.length();
    int n = s.length();
    int i = 0, j = 0;
    while (i < m && j < n) {
    if (t.charAt(i) == s.charAt(j)) {
    j++;
    }
    i++;
    }
    return j == n;
    }
    }

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

      Thanks a lot

    • @rahiljakir
      @rahiljakir Рік тому +2

      I think, first me time complexity O(t.length) hi hongi...kyu ki s ki length matter nhi karti loop t.length time hi run hoga. So it's O(n)

  • @Shubham-yc6nz
    @Shubham-yc6nz 21 день тому +1

    I was so confused...... after watching this video finally clarified, prev was actually storing index of 't' string
    Thanks

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

    I knew this guy will also cover followup Qn. This channel is no doubt the MOST UNDERRATED channel I have ever encountered.
    Thanks a lot for the detailed explanation

  • @rohitrajput2097
    @rohitrajput2097 7 місяців тому +2

    Never seen such a detailed explaination before.

  • @annuuuppp
    @annuuuppp Рік тому +2

    2nd Approach was awesome 🔥

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

    Mind blowing 🔥🔥🔥

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

    The best explanation 🙌🔥

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

    wow 🙌
    The follow up was just lit🔥🔥

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

    next level ❤

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

    Excellent explaination🙂🙂🙂

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

    This guy is a living legend

  • @mohdafzal4017
    @mohdafzal4017 8 місяців тому +1

    Your channel is gem

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

    Bhaiya Aapka padhaya hua bauth aache se samajh aa jata h , great ho aap

  • @AlishaKhan-ww3io
    @AlishaKhan-ww3io Рік тому +1

    Hats off 🔥

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

    class solution {
    Public boolean is Subsequence(string s,string t){
    int i=0,j=0;
    while (i

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

    approach 2 was amazing thanks for showing us that

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

    class Solution {
    public boolean isSubsequence(String s, String t) {
    if (s==null || t==null) return false;
    Map map = new HashMap(); //
    //process 1
    for(int i=0; i

  • @vinayjangra1401
    @vinayjangra1401 Місяць тому

    time complexity should be, O(t.length)

  • @Tesla_Arc
    @Tesla_Arc 6 місяців тому

    I have one doubt - how did you come up with this approach, what was your thought process ? could be please tell
    Great Explanation Bro

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

    THANK YOU BHAIYA FOR SUCH WONDERFUL EXPLAINATION: JUST ONE THING TO BE ADDED
    WHY THE MAP SOLUTION IS BETTER FOR LONGER RUN :-
    LET,
    t.length() = N (0

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

    Ak number.....

  • @samarohochattopadhyay7407
    @samarohochattopadhyay7407 Рік тому +3

    shouldn't the T.C for linear approach be O(Max(s,t)) ?

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

      Yes yes, correct. I will add the correction in the caption.
      Thanks a lot 😇🙏

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

      O(min(s,t))

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

    ❤❤❤❤❤

  • @saumilvachheta
    @saumilvachheta Рік тому +2

    Hi, can you make videos in Java? If not, can you suggest resources/UA-camChannels to learn DSA in Java?

    • @SplitSplit-te1qc
      @SplitSplit-te1qc Рік тому

      ua-cam.com/video/CVA85JuJEn0/v-deo.htmlsi=Drq_QwonMCf23LYD

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

      Hi there,
      I usually always add JAVA code exactly similar to my C++ version.
      You can find them in the same Github link in the description. I have updated.
      /************************* JAVA *******************************/
      //Follow Up Approach
      //Approach-1 (Using Binary Search) -> This is an important concept for qns like Leetcode-792
      public class Solution {
      public boolean isSubsequence(String s, String t) {
      Map mp = new HashMap();
      for (int i = 0; i < t.length(); i++) {
      char ch = t.charAt(i);
      mp.computeIfAbsent(ch, k -> new ArrayList()).add(i);
      }
      int prev = -1;
      for (char ch : s.toCharArray()) {
      if (!mp.containsKey(ch))
      return false;
      List indices = mp.get(ch);
      int index = Collections.binarySearch(indices, prev + 1);
      if (index < 0)
      index = -index - 1;
      if (index == indices.size())
      return false;
      prev = indices.get(index);
      }
      return true;
      }
      }
      //Approach-2 (Simplest O(n) approach)
      public class Solution {
      public boolean isSubsequence(String s, String t) {
      int m = t.length();
      int n = s.length();
      int i = 0, j = 0;
      while (i < m && j < n) {
      if (t.charAt(i) == s.charAt(j)) {
      j++;
      }
      i++;
      }
      return j == n;
      }
      }

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

      @@codestorywithMIK hi, thanks for your response. Actually I didn't open the GitHub link. My bad. Your videos are good, helps to understand logic. Thanks.

  • @maheshvarandani1308
    @maheshvarandani1308 Рік тому +2

    How long it will take to develop a intuition to approach the question like you. I am solving questions since a year still lacking intuition

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

      I also dream of becoming one day like this legend.
      With hard work and consistency we can definitely become better and better

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

    ❤❤

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

    bhaiya can u please make a video on prefix sum approach i am lacking a behind the intuition still trying

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

    i wrote follow up question in Bf approach why not give Tle please explain?
    😂❤

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

    Sir aap kya karte ho .?

  • @yadav.nitin03
    @yadav.nitin03 Рік тому +1

    Yo