31 Sequence Pattern Matching

Поділитися
Вставка
  • Опубліковано 5 жов 2024
  • Given two strings s and t, return true if s is a subsequence of t, or false otherwise.
    A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., "ace" is a subsequence of "abcde" while "aec" is not).
    Example 1:
    Input: s = "abc", t = "ahbgdc"
    Output: true
    Example 2:
    Input: s = "axc", t = "ahbgdc"
    Output: false
    Problem Link: leetcode.com/p...
    Playlist Link: • Dynamic Programming | ... .
    ------------------------------------------------------------------------------------------
    Here are some of the gears that I use almost everyday:
    🖊️ : My Pen (Used in videos too): amzn.to/38fKSM1
    👨🏻‍💻 : My Apple Macbook pro: amzn.to/3w8iZh6
    💻 : My gaming laptop: amzn.to/3yjcn23
    📱 : My Ipad: amzn.to/39yEMGS
    ✏️ : My Apple Pencil: amzn.to/3kMnKYf
    🎧 : My Headphones: amzn.to/3kMOzM7
    💺 : My Chair: amzn.to/385weqR
    🛋 : My Table: amzn.to/3kMohtd
    ⏰ : My Clock: amzn.to/3slFUV3
    🙋🏻‍♀️ : My girlfriend: amzn.to/3M6zLDK ¯\_(ツ)_/¯
    PS: While having good gears help you perform efficiently, don’t get under the impression that they will make you successful without any hard work.

КОМЕНТАРІ • 205

  • @amitkumarsingh5414
    @amitkumarsingh5414 4 роки тому +217

    I don't know why I fully watched this videos, I already think the approach within a minute, this is just because you broo.. Thanks soo much, from the bottom of my heart

    • @TheAdityaVerma
      @TheAdityaVerma  4 роки тому +51

      Thanks brother, Do share to make this channel grow ! thought the credit is only yours ! You have a sharp mind !!

    • @neghatnazir1668
      @neghatnazir1668 4 роки тому +16

      if lcs(a,b)==min(a.length(),b.length());
      return true;
      else
      return false;

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

      same bro, its cuz he taught before videos well

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

      same

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

      @@neghatnazir1668 , if we had to find a in b and a length is greater than b then also return false , there may be a chance you getting b substring in a.

  • @trendingindia972
    @trendingindia972 2 роки тому +41

    This guy will be remembered as a guy who made DP interesting and easy

  • @mayurbhor2231
    @mayurbhor2231 3 роки тому +151

    Just a linear traversal in enough I guess to solve it. Maintain two pointers at each string , move one if you find the character in another. Not worth DP , but always good to know multiple ways of solving !

    • @iitibhupendrasingh6887
      @iitibhupendrasingh6887 3 роки тому +10

      yes true
      ``` class Solution {
      public:
      bool isSubsequence(string s, string t) {
      int n=s.length();
      int m=t.length();
      int i=n-1;int j=m-1;
      while(i>=0 and j>=0){
      if(s[i]==t[j]){
      i--;j--;
      }else{
      j--;
      }
      }
      if(i==-1){
      return true;
      }
      return false;
      }
      };```

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

      True

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

      bool solveRecursive(string &S,string &L,int n,int m){
      if(n > m)
      return false;
      if(n==0)
      return true;
      if(S[n-1] == L[m-1])
      return solveRecursive(S,L,n-1,m-1);
      else
      return solveRecursive(S,L,n,m-1);
      }
      bool isSubsequence(string s, string t) {
      return solveRecursive(s,t,s.length(),t.length());
      }

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

      boolean isSubSequence(String s1, String s2){
      int i=0,j=0;
      while(i

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

      bool isSubSequence(string A, string B)
      {
      int j=0;
      for(int i=0;i

  • @sanketoreken2980
    @sanketoreken2980 3 роки тому +16

    2 pointer approach:
    if we want to match string s with string t:
    int pntr = 0;
    for (int i=0; i

  • @0anant0
    @0anant0 4 роки тому +45

    30 of 50 (60%) done! Others have mentioned a 2 ptr approach, but this LCS approach is also a diff way of thinking.

  • @shivanisheth186
    @shivanisheth186 2 роки тому +9

    after learning from ur videos i am now getting approaches within few minutes thank you for teaching dp in such great manner.

  • @jagrit07
    @jagrit07 4 роки тому +11

    Just by understanding the problem statement, LCS approach came to my mind. All thanks to your previous videos.

    • @TheAdityaVerma
      @TheAdityaVerma  4 роки тому +6

      Glad to hear that, please subscribe and share to help this channel grow !!

  • @priyakoshta7129
    @priyakoshta7129 3 роки тому +13

    You have built the concepts really nicely man. After following your all previous dp videos now I dont even wait for you to explain the solution, I just read the question take a pause and write the solution myself. But I still watch your video till the end so as to support you :)

  • @sanyamsinghal7992
    @sanyamsinghal7992 4 роки тому +63

    for this question, we can take two pointer i and j, i for larger string and j for smaller string
    run a loop for bigger string:
    if ith char of bigger == jth char of smaller THEN j++
    now check if j==size then it is present
    This can be done in O(N+M) time complexity instead O(N*M) by LCS

    • @TheAdityaVerma
      @TheAdityaVerma  4 роки тому +45

      Yeah but thats not intuitive brother, I can give you one more optimized solution but that would be just you mugging it up, try to come up with the solutions !!

    • @sanyamsinghal7992
      @sanyamsinghal7992 4 роки тому +5

      @@TheAdityaVerma you are right that may not be intitutive, I was just doing a question where time constraints were O(N) that's when I came up with this. Btw if there is another more optimized approach then please share

    • @najimali32
      @najimali32 4 роки тому +18

      @@TheAdityaVerma How can that's not the intuitive first thing that came in my mind is two pointers approached.
      Your method is good but you can also share another method that is more optimized.

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

      please share code

    • @hackingguy
      @hackingguy 4 роки тому +13

      @@TheAdityaVerma Two Pointer Approach Came By Intution, Never Mugged Up!

  • @SurajKumar-bw9oi
    @SurajKumar-bw9oi 4 роки тому +71

    Leetcode (EASY):
    LCS -> O(N*M)
    2 Pointers -> O(N+M)

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

      please share question link

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

      ya we can do same question with one for loop.(THINK LITTLE BIT !).

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

      @@kohinoorkhan8529 yup...just traverse the bigger string and check for smaller string ...

    • @sayantaniguha8519
      @sayantaniguha8519 3 роки тому +8

      bool isSubSequence(string A, string B)
      {
      int j = 0, i = 0;
      while ( i < B.size() && j < A.size() )
      {
      if (A[j] == B[i])
      j++;
      i++;
      }
      return ( j == A.size() );
      }
      Iye hi hai 2-pointers wala solution?

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

      @@apoorvkrishan1447 Thank you.

  • @aakashyadav6228
    @aakashyadav6228 3 роки тому +9

    Salute to people who cracked placements before feb 6, 2020 !

  • @rasenshuriken6333
    @rasenshuriken6333 3 роки тому +17

    on leetcode this problem is called "is Subsequence"

  • @mnsayeed999
    @mnsayeed999 4 роки тому +148

    Wildcard pattern matching is a more suitable candidate for DP than this one, since this can easily by solved using 2 pointer approach.

    • @abhinavtyagi1804
      @abhinavtyagi1804 3 роки тому +17

      with two pointers this will be O(n) optimized, not worth LCS approach

    • @abhineetsarkar3456
      @abhineetsarkar3456 3 роки тому +6

      Before watching this playlist, I used to skip the DP problems. Thanks to @Aditya Verma, seeing this problem I give it a try and boom, solved in just 2 attempts. Thank You @Aditya Verma from the bottom to my heart. 😊❤

    • @sayanghosh2883
      @sayanghosh2883 3 роки тому +16

      even two pointer is not required just a linear scan is enough

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

      Exactly! A linear scan would do it 🧐

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

      @@sayanghosh2883 how can u elaborate

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

    I did not watch the full video because I was able to think of the logic and it is because of your sir. Thanks for this wonderful playlist.

  • @ngtv5608
    @ngtv5608 3 роки тому +8

    Need a playlist on GRAPH !!

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

    I have solved lot of DP question. because of you only bro ..thanks a lot for superb videos.....

  • @abhishekbabbar9234
    @abhishekbabbar9234 4 роки тому +23

    Bro, can u tell us more questions on sequence pattern matching?

    • @vinaybansal2430
      @vinaybansal2430 3 роки тому +8

      Find number of times a string occurs as a subsequence in given string

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

    big thanks to make are life easy and developing our capacity to relate questions and think answers or solutions in a minute.

  • @prashant.s.397
    @prashant.s.397 8 місяців тому

    30 out of 50 done ,will finish in 2 days.this is literally better than web series

  • @girishkulkarni1702
    @girishkulkarni1702 4 роки тому +11

    I love your videos bro I wish we were taught algorithms in a similar way!!. I shared your videos with many friends they all call you GOD. Please try to make videos on other topics also thank you for this.

    • @TheAdityaVerma
      @TheAdityaVerma  4 роки тому +5

      Thanks for sharing!! Sure, will be uploading more videos soon, kinda stuck now due to lockdown as everybody else. 😕

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

    wow I'm also able to think about the approach before watching the video Thanks Aditya Sir

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

    Thanks for amazing explanation.
    Also it can be done in O(N) time and O(1) space:
    bool isSubSequence(string A, string B)
    {
    // code here
    int n = A.length();
    int m = B.length();
    int i=0,j=0;
    while(i

  • @shivammishra-sj8jo
    @shivammishra-sj8jo 4 роки тому +1

    Bro your teaching methodology is awesome and very useful to correlate with other subproblems

  • @VishalKumar-xr4nm
    @VishalKumar-xr4nm 2 роки тому

    DP is good to understand concept of subsequence.
    Following is optimal way to do the same
    Time complexity: O( min(s.length() ,t.length() ) )
    Space complexity: O( 1 )
    class Solution {
    public boolean isSubsequence(String s, String t) {
    int m = s.length(), n = t.length();

    if(m>n) return false;

    int j = 0;
    for(int i=0; j

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

      Time complexity will be O (max (|a|, |b|)).

  • @shubhamsharma-ec3re
    @shubhamsharma-ec3re 3 роки тому +2

    One more way for this :
    a=input()
    b=input()
    f=0
    for i in a:
    if i in b:
    ind=b.index(i)
    b=b[ind:]
    f+=1
    print(f==len(a))

  • @arifwaqas698
    @arifwaqas698 3 роки тому +4

    I think this can be done n O(n) complexity using a stack,
    reverse the given string and push it into the stack,
    Loop through the second string and keep popping if same elements are found.
    In the end, if (my_stack.empty()) return true; else return false.
    Pls correct me if I am wrong but this would also give O(N)

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

      U r right

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

      @@codekro6060 how will you push the string into the stack, index by index? Just by traversing it or is there a better way in JAVA or C++ to do it?

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

      @@kxnyshk u can create a stack of type string

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

      no need of using extra memory we can do it in O(1) extra space
      bool isSubSequence(string A, string B)
      {
      int j=0;
      for(int i=0; i

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

    Your video is really very nice. Just a suggestion! It would be better,if you can also explain the time complexity of your solutions.

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

    I have done this by your previous video LRS. First combine 2 string then find longest repeating sequence if it matches with original then true

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

    Try leetcode 10 and 44 . Must try dp questions on pattern matching.

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

    Excellent videos.Thanks for sharing!!! Really excited to see your subscribers are growing.

  • @deepak-ly3ob
    @deepak-ly3ob 9 місяців тому

    Wonderful lectures.

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

    Thank you very much. You are a genius.

  • @charan775
    @charan775 4 роки тому +23

    bro where are questions on LIS & DP on grid?

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

      isko LIS nahi aata.

    • @nikhilraj5115
      @nikhilraj5115 4 роки тому +20

      @@sumantopal558 isko jitna aata hai, utna toh seekh le

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

      @@nikhilraj5115 hum toh college dropout hai... Dukaan pe baithke agle 80 saal nikalne waale hai

    • @usurper1091
      @usurper1091 4 роки тому +7

      @@sumantopal558 ungratefulness ki bhi seema hoti hai

    • @VishalSharma-hq5ti
      @VishalSharma-hq5ti 4 роки тому +4

      @@sumantopal558 toh dukaan par baith na chupchap!

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

    This question is not wildcard pattern matching, right?

  • @raj-nq8ke
    @raj-nq8ke 3 роки тому

    LEETCODE Question Name : Is Susequence (EASY)

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

    we can solve it directly also by dp,we will check two conditions
    if a[i-1]==b[j-1] then return true&&dp[i-1][j-1]
    otherwise return false||dp[i][j-1]
    like this we can simply return dp[a.size()][b.size()] at the end

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

    Why not using two pointers one on string a another on string b. In that case, time complexity: O(m+n) space complexity: O(1)

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

    @aditya_verma plzz..upload wildcard matching problem

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

    First of all thank you for putting your efforts in creating content 🙂
    Why can't we do it like this in O(n) time?
    boolean isSubsequence(String src, String s) {
    int j=0;
    for(int i=0;i

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

    bool isSubsequence(string s, string t) {
    int k=0;int j=0;
    for(int i=0; i

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

    we can here just use stack to store string a and iterate from backwards of string b and then check

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

    Thanks for this tutorial

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

    please cover LIS also

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

    I think we can use Two pointer approach, It would be better.

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

    This type of same question is asked in Google Kickstart round a 2022 ...😁

  • @code7434
    @code7434 4 роки тому +3

    Bhai wildcard pattern matching daal do

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

    I have one approach , we can have length of string a in i and length of string b in j.
    We run a loop till any one of i and j gets 0.
    now we will check what is 0 now, if i is 0, we return YES else we return NO
    i will become 0 only when string b has every character of string a (sequentially), otherwise j will become 0 first.
    see below :
    while(i && j)
    {
    if(a[i-1] == b[j-1]) {
    i--;
    j--;}
    else { j--;}
    }
    if(i==0) return "YES";
    else return "NO";

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

    well you can also solve this using 2 pointer approach with o(n) tc

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

    that length sirf kaam kar jaega concept was great....i was using printLCS .... nice concept of LCS Range E {0 , min(a,b)} 🥰🫶🥰

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

    plz also share pratice questions on relevant topic.

  • @yash_renaissance_athlete
    @yash_renaissance_athlete 4 роки тому +6

    Love your videos bro. Lovely explanations. But I got another idea.
    This can be done through using stack or queue in O(N) time and O(M) space.
    This is how I did it with stack
    def seq_pat_match(a, b):
    stack_a = []
    ## populating the stack the stack in such a way that the first character of a is on top
    reversed_a = a[::-1]
    for char in reversed_a:
    stack_a.append(char)
    ##### stack_a would look like this for a="axy"
    # a

    • @mental-block
      @mental-block 4 роки тому

      As other's suggested this is DP series. But yeah If you want to use other method then I don't think you don't even need any auxiliary space. See this solution -
      public boolean isSubsequence(String s, String t) {
      if(s == null || t == null || s.length() > t.length() )
      return false;
      if(s.length() == 0 && t.length() == 0)
      return true;
      int i =0;
      for(int j=0; i

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

    more optimised is a two pointer solution O(N)

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

    Agar ye poocha ho ki count the no. Of subsequences of one string s that equals another string t ,then what to do

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

    The question can be solved with the help of queue. Put String 'a' in Queue, q1; String 'b' in Queue, q2. Now, traverse q1 and q2 from starting and check for the front element of q1 in q2. If it matches, then pop the front element of q1 and q2. If it doesn't , then pop the first element of q2 and carry on.
    After traversing the loop completely, if q1 is still not empty then a is not a subsequence of b.

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

      So, by now we got three approach for the problem: Queues, two pointers, dp.

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

    Sir, can't we check character by character and move in "a" only when matched else move in "b" and if matched then move in both? until one ends

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

      Yes we can do that but that is the naive approach and most probably using that would give you a time limit error(TLE).

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

      @@panavkumar9009 lol how come it will be efficient just a single while loop is needed

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

    We can solve it using stacks with O(n) time

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

    😅 😅 Is this wrong?
    def check(str1,str2):
    curr = 0
    for i in range(len(str1)):
    if str1[i] == str2[curr]:
    curr +=1
    return curr >= len(str2)
    str1 = "ADXCPY"
    str2 = "AXY"
    print(check(str1,str2))

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

    Sir ,you doing great work sir thanks from the core of my heart ❤️❣️ sir
    And plz upload the complete playlist of dp sir 🙏🙏

  • @PiyushCodes-ph3hl
    @PiyushCodes-ph3hl 8 місяців тому

    Bhaiya please DP series complete kardo

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

    Lcs is overkill for this. Just do linear search or use hashmap and solve in o(n).

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

    bool isSubsequence(string s, string t) {
    int i=0;
    for(int j=0;j

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

    Could have been solved with two pointer approach too.

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

    can't we do it with two pointer approach too ?

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

    Can this code be further optimized for some kind of inputs ?
    for e.g. *S1.length > S2.length* then surely *S1* can't be LCS of *S2*
    if ( S1.length > S2.length )
    return false
    if( S1.length==LCS(S1,S2) )
    return true
    return false

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

    Result = t[n][m]
    If(result== min(n,m){
    Return true;
    }
    Else{
    Return false;
    }

  • @MALIWAL1000
    @MALIWAL1000 3 роки тому +22

    *# Recursive Approach*
    The idea is simple, we traverse both strings from one side to other side (say from rightmost character to leftmost). If we find a matching character, we move ahead in both strings. Otherwise we move ahead only in str2.
    Code:
    class Solution {
    public:
    bool isSubSeq(string str1, string str2, int m, int n)
    {
    // Base Cases
    if (m == 0) return true;
    if (n == 0) return false;
    // If last characters of two strings are matching
    if (str1[m-1] == str2[n-1])
    return isSubSeq(str1, str2, m-1, n-1);
    // If last characters are not matching
    return isSubSeq(str1, str2, m, n-1);
    }
    bool isSubsequence(string s, string t) {
    int m = s.size();
    int n = t.size();
    return isSubSeq(s,t,m,n);
    }
    };
    // Approach Using TWO POINTER
    class Solution {
    public:
    bool isSubsequence(string s, string t) {
    int m = s.size();
    int n = t.size();
    int i = 0, j = 0;
    while(i < m && j < n) {
    if(s[i] == t[j]) {
    i++;
    }
    j++;
    }
    return i == m ? 1 : 0;
    }
    };
    // USING DYNAMIC PROGRAMMING
    // if LCS of string A nd B is equal to Size of String A then its True else false;
    class Solution {
    public:
    int helper(string x, string y) {
    int m = x.size();
    int n = y.size();
    int dp[m+1][n+1];
    for(int i = 0; i

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

    can anyone paste the link where i can get this practice problem of Sequence Pattern Matching?

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

    Can't we dont it using 2 pointer technique

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

    Hi Aditya,
    This question can be solved as :
    boolean isSubSequence(String A, String B) {
    {
    int i = 0, j = 0;
    while (i < A.length() && j < B.length()) {
    if (A.charAt(i) == B.charAt(j)) {
    i++;
    j++;
    } else
    j++;
    }
    if (i == A.length())
    return true;
    return false;
    }
    }

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

    Plz make video on greedy algo also

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

    This particular problem can be solved without dp :

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

    Does anyone know how to solve count palindromic subsequences?

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

    Someone please give the link of the question here

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

    public boolean isSubsequence(String s, String t) {
    if(s.length()==0)
    return true;
    int i=0;
    int j=0;
    while(i

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

    Sir please upload question related to sequence pattern maching.....

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

      Will upload more video in the month of may !! Till then keep on sharing and help this channel grow + that keeps me motivated to make more videos !! ✌️❤️

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

      @@TheAdityaVerma these two lines are copy pasted. i have seen this in 15-16 comments😂
      *Till then keep on sharing and help this channel grow + that keeps me motivated to make more videos !! ✌️❤️*

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

    how to find time complexity of these algorithm someone plz help

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

    one approach can be
    if(i==0&& j==0)return true;
    else if(i==0 ||j==0) return false;
    if(s.charAt(i-1)==s.charAt(j-1))return fn(i-1,j-1);
    else return fn(i,j-1);
    time O(N)
    space O(N) (recursion stack

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

    we can solve this with 2 pointer approach

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

    17 SEP 2022

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

    #Looping through string
    int i=s.length()-1, j=t.length()-1;
    while(i>=0 && j>=0){
    if(s[i]==t[j]) {i--; j--;}
    else j--;
    }

    return i

  • @RishuKumar-mn2jo
    @RishuKumar-mn2jo 3 роки тому

    Bro can u please upload the recursive video approach?
    I am not able to form the choice diagram.

    • @YashSharma-dz7uu
      @YashSharma-dz7uu 2 роки тому

      #include
      #include
      using namespace std;
      bool ans(string s1, string s2, int n, int m)
      {
      if (n == 0)
      {
      return true;
      }
      if (m == 0)
      {
      return false;
      }
      if (s1[n - 1] == s2[m - 1])
      {
      return true && ans(s1, s2, n - 1, m - 1);
      }
      return ans(s1, s2, n, m - 1);
      }
      int main()
      {
      string s1, s2;
      cin >> s1;
      cin >> s2;
      int l1 = s1.size();
      int l2 = s2.size();
      if (ans(s1, s2, l1, l2))
      {
      cout

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

    Bro need series on recursion and backtracking

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

    c++ easy solution below--
    int i=0;
    for(int j=0;j

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

    nice

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

    @Aditya we will return true even if len(LCS) > len(str1)?? or only if they are equal. I am a little confused with question

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

      LCS str1 se badha nhi ho sakta
      Maximum voh equal ho sakta

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

      ​@@gurnoorsingh2954 LCS wala code send kardo bhai , mera segmentation fault dikha rha

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

    #include
    using namespace std;
    int main()
    {
    string a="axyy";
    string b="adxcpy";
    int n=a.size();
    int m=b.size();
    int j=0;
    for(int i=0;i

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

    *Without using DP*
    bool is_sub_sequence(string &s1, string &s2, int m, int n) {
    int i = m-1;
    int j = n-1;
    while(j >= 0) {
    if(s1[i] == s2[j])
    i--;
    j--;
    }
    return i == -1 ? 1 : 0;
    }
    TC: O(N)
    SC: O(1)
    *Using DP* :
    int isSequence(string &s1, string &s2, int m, int n) {
    vector< vector > dp(m + 1, vector(n+1, 0));
    for (int i = 1; i < m+1; i++)
    {
    for (int j = 1; j < n+1; j++)
    {
    if(s1[i-1] == s2[j-1])
    dp[i][j] = 1 + dp[i-1][j-1];
    else
    dp[i][j] = max( dp[i-1][j], dp[i][j-1] );
    }
    }
    return dp[m][n];
    }
    string s1 = "AXY";
    string s2 = "YADXCPY";
    cout

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

    Z = gee nhii hota yha , America nhi h ye 💁

    • @TheAdityaVerma
      @TheAdityaVerma  4 роки тому +5

      tu !! 😂😂 Aap pdhai pe dhyan de Sir !! doubt ho to pooch lena Whatsapp pe ✌️

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

    Ye question kaha par hai

  • @ravilamkoti5081
    @ravilamkoti5081 4 роки тому +3

    two pointer approach yaha jayda acha hota

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

      ha but isme DP padh rhe hai hum that's why he is using this approach.

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

    Simple as fuck. @adityaverma Bhaiya time complexity jyada aa rhi thi dp ke approach se
    boolean isSubSequence(String A, String B){
    int n = A.length() , m = B.length() ;
    int i = 0 , j = 0 ;
    while(i < n && j < m){
    if(A.charAt(i) == B.charAt(j)){
    i++ ;
    j++ ;
    }else{
    j++ ;
    }
    }
    if( i == n){
    return true ;
    }
    return false ;
    }
    }

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

    bool isSubsequence(string s, string t)
    {
    int m = s.length(); int n = t.length();
    if(m==0) return true;
    bool flag = false;
    int i=0; int j=0;
    while(i

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

    please make a playlist on backtracking and recursion

  • @priyankasetiya1358
    @priyankasetiya1358 3 місяці тому

    class Solution {
    private int lcs(String s,String t){
    int m=s.length();
    int n=t.length();
    int[][] dp=new int[m+1][n+1];
    for(int i=1;i

  • @YashSharma-dz7uu
    @YashSharma-dz7uu 2 роки тому

    Recursive Approach :
    #include
    #include
    using namespace std;
    bool ans(string s1, string s2, int n, int m)
    {
    if (n == 0)
    {
    return true;
    }
    if (m == 0)
    {
    return false;
    }
    if (s1[n - 1] == s2[m - 1])
    {
    return true && ans(s1, s2, n - 1, m - 1);
    }
    return ans(s1, s2, n, m - 1);
    }
    int main()
    {
    string s1, s2;
    cin >> s1;
    cin >> s2;
    int l1 = s1.size();
    int l2 = s2.size();
    if (ans(s1, s2, l1, l2))
    {
    cout

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

    class Solution:
    def isSubsequence(self, s: str, t: str) -> bool:
    lcs=self.longestCommonSubsequence(s,t)
    if len(s)==lcs:
    return True
    return False

    def longestCommonSubsequence(self, text1, text2):
    m = len(text1)
    n = len(text2)
    t = [[0]*(n+1) for i in range(m+1)]
    for i in range(1, m + 1):
    for j in range(1, n + 1):
    if text1[i-1] == text2[j-1]:
    t[i][j] = t[i-1][j-1] + 1
    else:
    t[i][j] = max(t[i][j - 1], t[i - 1][j])
    return t[m][n]

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

    Leetcode Problem :
    leetcode.com/problems/is-subsequence/

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

    i can do this without dp :lol

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

    What in the f

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

    Can anybody please provide the solution of this question using LCS , It shows segmentation fault in gfg
    Question-->
    practice.geeksforgeeks.org/problems/check-for-subsequence/0

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

      I tried it using dp, but it gave tle. So used two pointer approach which takes O(N) time and O(1) space.
      bool isSubSequence(string A, string B)
      {
      // code here
      int i = 0, j = 0, count = 0;
      while(j

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

      @@atharvacodes3705 yes I also did using two pointer but using LCS it gives segmentation fault.
      My two-pointer approach:
      bool isSubSequence(string A, string B)
      {
      int m= A.length();
      int n= B.length();
      int j=0;
      for(int i=0; i

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

      @@tekbssync5727 then there must be a small mistake in your dp solution