Interview Question: Longest Common Substring

Sdílet
Vložit
  • čas přidán 28. 08. 2024

Komentáře • 55

  • @sharjeeltahir5583
    @sharjeeltahir5583 Před 5 lety +17

    omg
    this video needs to be remade. The whole time I was confused with the i - length + 1, which is actually i - cache[i][j] + 1.

  • @gurmeetchawla8362
    @gurmeetchawla8362 Před 6 lety +30

    the way it is explained it very confusing.

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

      It's basically edit distance. Checkout out back to back swe's explanation of edit distance and then keep this in mind.

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

    At 13:30 totally agree it’s easier to do 2d array first then optimize to 1d if there’s enough time leftover

  • @actualprogramming
    @actualprogramming Před 5 lety +2

    Your channel is a gem, Thanks for all the stuff.

  • @SinCityGT3
    @SinCityGT3 Před 5 lety

    Good video! But I think it's better to not set the out variable as you go through the matrix. On two large strings, that could waste a lot of time allocating memory.

  • @carteryu967
    @carteryu967 Před 7 lety +7

    Think you got the i and j mixed up while testing?

    • @ByteByByte
      @ByteByByte  Před 7 lety

      That's totally possible, but it's fine as long as the code is consistent

  • @manaligupta5332
    @manaligupta5332 Před 3 lety

    a more optimized solution exists for this question with constant space complexity. The idea is to expand around the centers.

  • @anuragdani9739
    @anuragdani9739 Před 7 lety +1

    Hello Sam,
    Thanks for the video series. Just a small request- would it be possible to categorize or tag the problems in types such as DP, Strings, graphs, trees, etc. either on your website or on your CZcams. That will be really helpful.

    • @ByteByByte
      @ByteByByte  Před 7 lety +1

      You're welcome!
      And if you go on the website, they are tagged. See the links to different categories here: www.byte-by-byte.com/coding-interview-questions/

    • @anuragdani9739
      @anuragdani9739 Před 7 lety +1

      Thanks!

  • @randysalber4960
    @randysalber4960 Před 6 lety

    Kind of a nitpick here....
    I added a variable: int len = 0;
    Then assigned the value to the cache like so: cache[i][j] = len = cache[i-1][j-1] + 1;
    Here's the nitpick: When I save the current longest String I used: out = a.substring(i - (len-1), i+1);
    I think it makes more sense, and is less confusing than subtracting and then adding back. (i - cache[i][j] + 1)

    • @ByteByByte
      @ByteByByte  Před 6 lety +1

      Not personally a fan, but this just goes to show that everyone has their own coding style and one isn't necessarily better than the other :)

  • @arturolara1770
    @arturolara1770 Před 2 lety

    what if the strings are of different sizes, would this still work?

  • @CraftandDogs
    @CraftandDogs Před 5 lety

    If we have "ABCA" and "BCA" then you need to do if (cash[i][j] >= out.length()).

  • @rahulbhati3079
    @rahulbhati3079 Před 4 lety

    i like it very much thankyou keep helping us ...

  • @unoti
    @unoti Před 5 lety

    I get the general idea here, but haven't been able to make this code work. I think the disconnect has to do with the a.substring(i - ?? + 1, i + 1) bit you changed at the end, and I'm sure there's some combination of variables and operators that make it actually work, but haven't been able to work out what those might be yet. I tried the various things mentioned in the video, and various permutations thereof, but haven't figured it out yet. When debugging, it quickly becomes a sea of variables of ABA BABA ABAB A and 1's and 2's and 3's...

  • @jays8504
    @jays8504 Před 6 lety +1

    When you calculate a.substring(i - length + 1, i + 1), the length is the length of the out string variable, correct?

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

    24 minutes to provide the answer?

  • @falakk22
    @falakk22 Před 6 lety

    Thanks for great explanation!! Best one on DP prob ..

  • @hiteshdave1826
    @hiteshdave1826 Před 5 lety

    Understand properly and it’s great algorithm and vedio as well. I was thinking how it can be possible with singleArray(n+m) length ?

  • @RafGuitar
    @RafGuitar Před 4 lety

    What would be the complexity of the trie solution?

  • @andyyhope
    @andyyhope Před 7 lety

    Your solution produces a Time and Space complexity of 0(nm), is there any particular reason why you chose to create a NxM matrix? I think this can be done in O(1) space?

    • @andyyhope
      @andyyhope Před 7 lety

      Actually, nvm. I think to get to constant space, the time complexity would go up to O(nm^2)

    • @andyyhope
      @andyyhope Před 7 lety

      actually wait, i think i double wronged myself. I believe this -can- be done with O(nm) Time and O(1) Space

    • @ByteByByte
      @ByteByByte  Před 7 lety

      Yeah usually as you improve the time complexity, you make the space complexity worse and vice versa. The extreme example is that any problem can be solved in constant time if you save the output for every possible input

    • @ariabanazadeh1016
      @ariabanazadeh1016 Před 5 lety

      @@ByteByByte Youre Wrong For saving that output you most to compute the result Genius !

  • @praveenchouhan6388
    @praveenchouhan6388 Před 4 lety

    i think you missed the most important part - the time and space complexity analysis????

  • @24306529
    @24306529 Před 6 lety

    good video until you changed the substring function at the very end. little more explanation would have helped coz that is the most imp part and a bit confusing as well

  • @siddhartheswaramoorthy2672

    Great explanation, But I could not understand how you put i-cache[i][j]+1

    • @6b14yeungsinchun-8
      @6b14yeungsinchun-8 Před 4 lety +1

      cache[i][j] is the length of the common substring, i is the index of the last character in the substring. Therefore, (i - cache[i][j] + 1) is the index of the first character in the common substring, which is exactly what we need for a substring function.

  • @aditya234567
    @aditya234567 Před 7 lety

    Sorry just wondering if it would be a.substr or a.substring?

    • @ByteByByte
      @ByteByByte  Před 7 lety +2

      docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring(int,%20int)

    • @aditya234567
      @aditya234567 Před 7 lety

      Byte By Byte Oh I was doing it in c++. i could follow entire syntax was c++ knowledge . never looked into java. I've an interview this Monday so was particular about it so that I don't type it wrong. thank you very much for your response and video :)

    • @ByteByByte
      @ByteByByte  Před 7 lety +1

      Ah yeah. Its good how similar the languages are because these solutions can still be helpful, but I can see why it might get a little confusing

    • @aditya234567
      @aditya234567 Před 7 lety

      Byte By Byte yea even c++ has new to create something on heap although I suspect it should be int cache [ ][ ]= but not int [ ][ ] cache=. good to know the difference though :)

  • @miller-josh
    @miller-josh Před 6 lety

    What's the runtime and space complexity?

  • @abhijeetprakash854
    @abhijeetprakash854 Před 6 lety +1

    length variable??

  • @maltedloaf518
    @maltedloaf518 Před 4 lety

    Using a sliding window approach would offer the same O(a * b) complexity and is much simpler than DP? Here are two examples: imgur.com/a/FHncMsn imgur.com/a/mlRRMeF

  • @BlokeBritish
    @BlokeBritish Před 2 lety

    forget the interview and interviewer man.
    just focus on the concept

  • @ihopethiscommentisntabusiv4670

    Thanks for the video but please rehearse your talking script before recording. The explanation feels like completely disjointed, confusing and honestly full of mistakes.

  • @RajeshTycoon
    @RajeshTycoon Před 5 lety

    for input strings a = "stone", b = "longest" the output would come here as "st" of length 2 which would be wrong. The correct answer would be "one" of length 3. Hence, you should add else case - if a.charAt(i) != b.charAt(j) cache[i][j] = Math.max(cache[i-1][j], cache[i],[j-1])

    • @ThomasChau922
      @ThomasChau922 Před 5 lety +2

      This question is about longest common substring, not longest common sequence.