Longest common substring | Dynamic programming

Sdílet
Vložit
  • čas přidán 28. 08. 2024
  • This video explains how to find the longest common substring as well as print the longest common substring. This is a very popular dynamic programming video which is frequently asked in programming interviews. The code for it is present below. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
    CODE LINK: drive.google.c...

Komentáře • 125

  • @gourabbanerjee4152
    @gourabbanerjee4152 Před 3 lety +30

    I believe I have gone through all the famous youtube channels explaining DSA. And I must say, the best explanation for each problem I find here. While I search for any problem, I have started looking whether TECH DOSE made any video on that or not, if yes, then I ignored all other channels blindly and I never disappointed. Thank you so much!!

    • @techdose4u
      @techdose4u  Před 3 lety +1

      Welcome :)

    • @amitis9632
      @amitis9632 Před 3 lety +1

      @@techdose4u I do the same, if I see a video from TECH DOSE I am done with my search.

    • @ravi7264
      @ravi7264 Před 2 lety +1

      Same with me.

    • @udayptp
      @udayptp Před 2 lety

      @@ravi7264 🔥

  • @lakhwindersingh-qu6wj
    @lakhwindersingh-qu6wj Před 4 lety +26

    thank you tech dose. Finally someone explained this throughly

  • @AtulKumarVermaOnline
    @AtulKumarVermaOnline Před 3 lety +8

    Most CZcamsrs just teach how it works and just go through the algorithm. Thank you for also teaching why it works and how to think about it.

  • @Bhu22bhu22
    @Bhu22bhu22 Před 3 lety +7

    Nothing can be better than this, you really wanted to make things clear, simply awesome man.
    Thank You and keep up the good work.

  • @jhilikkundu3158
    @jhilikkundu3158 Před 3 lety +6

    Most underrated channel

  • @mariocraft95
    @mariocraft95 Před rokem

    Was able to use this algorithm to finish a spell checker assignment. Could not have done it near as well without this video!

  • @atharvakulkarni2180
    @atharvakulkarni2180 Před rokem +3

    Honestly, for beginners bottom up DP is not at all intuitive and your majority audience is beginner to medium level guys, so please try to explain DP problems using Top Down approach as well. BTW Very nice explanation. Thanks!

  • @ashishm8850
    @ashishm8850 Před 3 lety +6

    Dude, you have my respect, and my thanks! 🙏

  • @EngWorld-nr2ww
    @EngWorld-nr2ww Před 2 lety

    Nice and precise explanation than other available on youtube

  • @damaroro
    @damaroro Před rokem

    YOUR VIDEO HELPS ME A LOT, THANKS SENSEI

  • @prasad.patil12
    @prasad.patil12 Před 4 lety +2

    Such a detailed and clear explanation 👍

  • @pavankumar.m1341
    @pavankumar.m1341 Před 2 lety

    Now i understood the logic of this dynamic programming

  • @shagunlamba6481
    @shagunlamba6481 Před 3 lety +3

    great explanation, recommending to everyone.

  • @atefnazi753
    @atefnazi753 Před 3 lety +1

    very well explained...want more DP solutions from you

  • @geniusboys8204
    @geniusboys8204 Před 4 lety +1

    great explanation ! Thank you finally I understood it.....

  • @SammyTvMan
    @SammyTvMan Před 2 lety +1

    Very well spoken, thank you

  • @PrateekJesingh
    @PrateekJesingh Před 3 lety

    Good explanation of the dynamic programming bit but what was lacking was the intuition behind how you filled the table, please include that as well. You wouldn't expect anyone to just memorise the solution

  • @shams_vai_360
    @shams_vai_360 Před 2 měsíci +1

    what is the difference between longest common subsequence and longest common substring?

  • @chiragkamat4887
    @chiragkamat4887 Před 4 lety +1

    Very nicely explained. Thank you :)

  • @where3639
    @where3639 Před 3 lety +1

    Excellent video keep up the good work

  • @prateeksrivastava9
    @prateeksrivastava9 Před 4 lety +5

    You made DP So Easy

  • @SZ-jw9my
    @SZ-jw9my Před 3 lety +1

    Excellent video, thank you!

  • @adityagoswami6881
    @adityagoswami6881 Před 4 lety +3

    Can you explain after getting the all diagonal number in dp table ,how to print the lcs strings

    • @vemulacharanrayadhanush3110
      @vemulacharanrayadhanush3110 Před 4 lety

      check this code
      int main() {
      int t;
      cin>>t;
      while(t--)
      {
      int n,m;
      cin>>n>>m;
      string s,s1;
      cin>>s>>s1;
      vector dp(n+1,vector(m+1,0));
      for(int i=1;i

  • @BharadwajGiridhar
    @BharadwajGiridhar Před 3 lety

    Nice channel. I love your content!

  • @soulehshaikh8799
    @soulehshaikh8799 Před 3 lety

    Easiest Explanation ever

  • @adityapandey9639
    @adityapandey9639 Před 3 lety +2

    What will be the recursive approach of this Q?? We can't return 0 if it's character is not matching!

    • @spartan5816
      @spartan5816 Před 3 lety

      Watch aditya verma YT channel. Thank me later

    • @adityapandey9639
      @adityapandey9639 Před 3 lety +1

      @@spartan5816 bhai I have already watched his video it was nice bt he used to spend more time in relating the Q rather than developing the concept.. Didn't worked well in my case 🙃

  • @sauravgsh16
    @sauravgsh16 Před 4 lety +2

    Great explanation. Can you also provide a space optimized solution for the same ?

    • @techdose4u
      @techdose4u  Před 4 lety

      Will try to....actually too many important requested videos are already pending. So, i want to focus on important topics for now 😅

    • @sauravgsh16
      @sauravgsh16 Před 4 lety

      @@techdose4u glad for the response .. will wait till you have time to upload one .. thanks ..

  • @Pooh__7__
    @Pooh__7__ Před 3 lety +1

    Such an amazing video

  • @yanamanagandlabhargavi4577

    Great explanation

  • @sreekanththota2200
    @sreekanththota2200 Před 4 lety +2

    sir,I like your Great explanation .....

  • @JoseFranc
    @JoseFranc Před 10 měsíci

    Thank you so much

  • @shubhamjaiswal7645
    @shubhamjaiswal7645 Před měsícem

    explanation was good ...but what about memoization and recursion .... rather it would give TLE i know but you should firstly tell that n......

  • @vallishreddy8263
    @vallishreddy8263 Před 3 lety +2

    good explanation

  • @ankitaverma2271
    @ankitaverma2271 Před 4 lety +3

    Can you please tell me about [i-1] =[j-1]
    Which index we are taking and how.?

    • @techdose4u
      @techdose4u  Před 4 lety +2

      Because dp table is indexed from 1 (as index 0 in table means empty string) but the string position is always index from 0 (as index 0 means first character).

    • @ankitaverma2271
      @ankitaverma2271 Před 4 lety +1

      @@techdose4u Thank youu.
      I got that.

    • @ankitaverma2271
      @ankitaverma2271 Před 4 lety +1

      As in the example you explained of there js no match in the characters then mark the dp row coloumn as zero.
      Shouldn't we mark the max of row and coloumn??

    • @techdose4u
      @techdose4u  Před 4 lety +2

      Max will be taken if we form subsequence where characters can be skipped but in this case you are matching substring, so whenever mismatch occurs, you cannot skip that character, you need to start from length 0 from the next character. This is the difference between subsequence and substring.

  • @ShaliniNegi24
    @ShaliniNegi24 Před 3 lety +1

    thanks for the videos

  • @andresnamm982
    @andresnamm982 Před 3 lety +1

    You are awesome!

  • @impatientgaming9868
    @impatientgaming9868 Před 8 měsíci

    good one

  • @bharathirv8479
    @bharathirv8479 Před 3 lety

    Hi, The code returns less than one value from the actual length of substring, so I return the value +1 (return lcs+1) now it returns the correct length. are my findings correct?

  • @parnitasharma8898
    @parnitasharma8898 Před 3 lety +1

    Great job

  • @supernova7870
    @supernova7870 Před 4 lety +1

    Sir, do you have code for 0(N^3) approach , i solved it using 3 while loops , but the 3rd loop just execute for few statements , so i think its complexity is 0(N^2) .

    • @MrThepratik
      @MrThepratik Před 3 lety

      that would be average case time complexity

  • @praveenchouhan6388
    @praveenchouhan6388 Před 4 lety +1

    awesome explaination!!!!!!!!!!!!!! your way just super cool, thanks a lot!!!

  • @shaziasamreen8584
    @shaziasamreen8584 Před 4 lety

    Sir can't we use the string Matching method.Brute force approach is n*m and there are String Matching algorithms that reduce the complexity can't we use them with some manipulations and find the longest common substring

  • @osmantahir8659
    @osmantahir8659 Před 3 lety

    How to find a smallest number in the longest subsequence? in a dense subsequence. Dense subsequence means that the i+1 element should be less than or equal to 2 with the ith element in the subsequence

  • @sakthim7160
    @sakthim7160 Před 4 lety +2

    Why you are keeping all the elements as zero In the first row and column of a matrix? If any one of string doesn't contain any character, then we can simply say the result by using a single if conditions! And here 6x6 matrix is enough to perform what you have explained but why you are taking 7x7 matrix? Your space complexity is O(n1+1 * n2+1) complexity why? If I am using n1xn2 matrix that is 6x6 I have faced diagonal problem in matrix but I can solve it by putting a extra one condition! I want a explanation from your side kindly waiting for your reply! I think checking for the previous result that is previous diagonal only you are using the extra space

    • @techdose4u
      @techdose4u  Před 4 lety

      If condition is costlier. The if condition will be compared for each of the 36 cells. If you just fill it with 0 then time complexity is heavily improved. Think about 100 by 100 matrix. You will be comparing if for all 10000 cells while you can simply put 199 zeroes when you already know they will be zero. 2nd reason is to make the formula work seamlessly and reduce the code complexity. So you gain both in terms of TIME and CODE complexity. I have explained why it will be 0 by the way :)

    • @sakthim7160
      @sakthim7160 Před 4 lety +1

      @@techdose4u thanks for your explanation😍you are almost responding to all of my comments. Thank you

    • @techdose4u
      @techdose4u  Před 4 lety

      Welcome :)

  • @RahulKumar-wf9xx
    @RahulKumar-wf9xx Před 3 lety

    What is the time complexity for the recursive Approach? is it O(2^(m+n)) ??

  • @ibrahimshaikh3642
    @ibrahimshaikh3642 Před 4 lety

    Very good. Which whiteboard software u use

  • @suryakiran2970
    @suryakiran2970 Před rokem +1

    super

  • @AyushSingh-oq2fm
    @AyushSingh-oq2fm Před 2 lety +1

    does it have any recursive approach with memorisation??

    • @techdose4u
      @techdose4u  Před 2 lety +1

      Yes it does

    • @AyushSingh-oq2fm
      @AyushSingh-oq2fm Před 2 lety

      Please provide it's link I am struggling to come up with it's recursive logic

  • @bismeetsingh352
    @bismeetsingh352 Před 3 lety

    @9:46 , why does the position even matter? Eg. s0 = "acdghr" s1 = "cdghra", cdghr are at different positions but that is a valid answer.

  • @tejasghone5118
    @tejasghone5118 Před 4 lety +1

    Great Explaination !! Can you do the unique path question from leetcode? qs-62

    • @techdose4u
      @techdose4u  Před 4 lety

      Sure....I think I have already done. Don't remeber 😅 If not then I will do it in future.

    • @tejasghone5118
      @tejasghone5118 Před 4 lety

      @@techdose4u no u haven't done that coz from last month whenever i get stuck on some qs I search for your videos first😂

    • @sayantaniguha8519
      @sayantaniguha8519 Před 3 lety

      Link?

  • @sakthim7160
    @sakthim7160 Před 4 lety +1

    One character can't be string know? Then how you can say a single character as subject?Or did we can say a single character is a substring?

    • @techdose4u
      @techdose4u  Před 4 lety +1

      A single character is also a string actually. It's a string of length 1. Dont confuse with char and string datatypes. Think in general sense. A string can have no characters too (empty string).

    • @sakthim7160
      @sakthim7160 Před 4 lety +1

      @@techdose4u thank you

  • @salmanfaries3064
    @salmanfaries3064 Před 3 lety +1

    Would u have longest decreasing substring

    • @techdose4u
      @techdose4u  Před 3 lety

      It's the same. Do LIS from the end.

  • @sayantaniguha8519
    @sayantaniguha8519 Před 3 lety

    Recursive solution?
    I don't care about TLE but need it for clear explanation

  • @thatindianboy2817
    @thatindianboy2817 Před rokem

    Any DFS Java solution?

  • @chessmaster856
    @chessmaster856 Před 2 lety

    Start with 0 n-1, then 1 n-1, then 0 n-2 etc. Stop at the first palindrome. Everything else will be smaller so no need to check. No dp required

  • @sais7065
    @sais7065 Před rokem +1

  • @575saisri4
    @575saisri4 Před 4 lety +1

    sir can u provide solutions of hackwithinfy round 1 2020 questions also plzz?

    • @techdose4u
      @techdose4u  Před 4 lety

      I will try this weekend. Are the questions and editorials for the same are available?

    • @saisrisai9649
      @saisrisai9649 Před 4 lety +1

      Yes sir.

    • @techdose4u
      @techdose4u  Před 4 lety

      Okay...then I will try to find some time on weekend. But I can't guarantee.

  • @coderpriyabrat3585
    @coderpriyabrat3585 Před 3 lety +1

    💕💕💕💕💕💕💕💕💕💕

  • @madhurjyadas1471
    @madhurjyadas1471 Před 4 lety +2

    You stretch the video so long with some random unnecessary explanations.

    • @techdose4u
      @techdose4u  Před 4 lety +8

      For some it may be unnecessary and for some it may be necessary. I explained assuming a beginner is watching. So people who already knows stuffs will feel its too long 😅 That's only my personal opinion though.

    • @sreekanththota2200
      @sreekanththota2200 Před 4 lety +3

      @@techdose4u
      thats true bro....am a beginner

    • @ruthwang9177
      @ruthwang9177 Před 3 lety +1

      @@techdose4u I find your explanation very helpful!

    • @techdose4u
      @techdose4u  Před 3 lety

      Thanks

  • @petarulev6977
    @petarulev6977 Před rokem +1

    very bad explanation

  • @huangCAnerd
    @huangCAnerd Před rokem

    Absolute insanity that this solution TLEs on CodeSignal.