Restore IP Addresses - Leetcode 93 - Python

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 พ.ย. 2024

ความคิดเห็น • 49

  • @worldwide6626
    @worldwide6626 3 ปีที่แล้ว +46

    I love how you did a problem related to IP address from facebook after facebook outage

  • @Iamnoone56
    @Iamnoone56 3 ปีที่แล้ว +11

    you can add this
    if len(s)12:
    return [ ]
    as constraints are
    0

  • @arina1193
    @arina1193 10 หลายเดือนก่อน +2

    technically you can leave just range(i, len(s)) because if it's more than i+3, if condition would cut it as invalid anyway

  • @khalmasonart
    @khalmasonart ปีที่แล้ว

    Actually because you are doing string slicing at each step. The time complexity is 3 ^ 4 * N where N is the length of string

  • @sujithameriga7348
    @sujithameriga7348 หลายเดือนก่อน

    Please discuss the Time complexity for backtracking solutions.

  • @aditii5150
    @aditii5150 19 วันที่ผ่านมา

    This is so difficult, I got this question in my Oracle interview today

  • @akhma102
    @akhma102 22 วันที่ผ่านมา

    Thank you, Neet!

  • @shuhaoliang144
    @shuhaoliang144 2 ปีที่แล้ว +1

    thank you very much. one more problem that I fully understand now after watching your explanation:)

  • @michaell8237
    @michaell8237 3 ปีที่แล้ว +3

    Thank you Neetcode! Your explanation is really clear and making sense!

  • @superfakerbros
    @superfakerbros 2 ปีที่แล้ว +3

    I’m sure there’s some way I’m doing it wrong but, even when I try to recreate your solution perfectly, it for some reason doesn’t seem to work. I’ve gone through it several times and, at this point, I’m not sure what to do

    • @adityadn95
      @adityadn95 2 หลายเดือนก่อน

      Check if you are incrementing dots while making the recursive call to backtrack

  • @jasonthai9647
    @jasonthai9647 2 ปีที่แล้ว +3

    How do you decide when to build a decision tree with 2 choices vs. a decision tree with more than 2 choices? I ended up going the 2 choices route (put a dot at a position vs not putting a dot at a position which is a technique you have used in other backtracking problems) which has a time complexity of O(2^n)? That 2 choice solution runs slower than your O(3^n / 3^4) solution. I drew out both the decision trees for input "101023" and the O(2^n) solution has 41 total recursive calls while your O(3^n / 3^4) solution only has 30 total recursive calls. I thought O(2^n) was always faster than O(3^n / 3^4) but that doesn't seem to be the case. What am I not understanding here?
    Thank you so much for your videos btw.

    • @abheykalia3409
      @abheykalia3409 2 ปีที่แล้ว

      this happened with me when i was solving Maximum length of concatenated string with all unique characters. I thought of a 2^n solution whilst the video used a better soon. However, when I solved this question I started with 2^n and reached towards 3^4. Basically, you should know the difference as to how both the decision trees are made and then you can easily visualise as to which algo to use

    • @ananyaarya2465
      @ananyaarya2465 11 หลายเดือนก่อน

      He made it to google, and then left.

  • @LakshyGupta
    @LakshyGupta 2 ปีที่แล้ว +4

    Java Code:
    :: LC All Passed ::
    class Solution {
    public List restoreIpAddresses(String s) {
    ArrayList ans = new ArrayList();
    if(s.length() > 12) return ans;
    helper(s,0,0,"",ans);
    return ans;
    }
    private void helper(String s, int i, int dots, String res, ArrayList ans){
    if(dots == 4 && i == s.length()){ // Base
    ans.add(res.substring(0,res.length()-1));
    return;
    }
    if(dots > 4){
    return;
    }

    for(int j = i; j < Math.min(i+3,s.length());j++){
    int currnum = Integer.parseInt(s.substring(i,j+1));
    if(currnum

  • @theunknown2090
    @theunknown2090 3 ปีที่แล้ว +3

    Bit confused where is back tracking happening here ?

  • @shantipriya370
    @shantipriya370 หลายเดือนก่อน

    best explanation..

  • @QinBinHua
    @QinBinHua 3 ปีที่แล้ว

    thx a lot, learned a lot from your channel, keep it up, bro.

  • @陳柏仰-j5n
    @陳柏仰-j5n 3 ปีที่แล้ว +1

    good as always

  • @ygwg6145
    @ygwg6145 ปีที่แล้ว

    Another way: O(n^4) by iterating the possible locations of the 3 dots.

  • @edwardteach2
    @edwardteach2 2 ปีที่แล้ว +2

    U an IP God

  • @Mcfly868
    @Mcfly868 2 ปีที่แล้ว

    Isnt it 3^3? you are placing 3 dots, and each dot position can have 3 choices

  • @maaph1
    @maaph1 11 หลายเดือนก่อน +3

    wow, i have no idea how i would come up with this solution in a real interview. 😔

  • @rishabhroy5259
    @rishabhroy5259 8 หลายเดือนก่อน

    you could have added the iterative solution too

  • @starstarhaha
    @starstarhaha 7 หลายเดือนก่อน

    thanks. condition dots>4 is not needed

  • @odelyaholiday9519
    @odelyaholiday9519 2 ปีที่แล้ว +2

    at 2:50 you probably mean 3 dots

    • @huseyinbarin1653
      @huseyinbarin1653 2 ปีที่แล้ว

      I think there should be 3 dots as well

    • @oogieboogie7028
      @oogieboogie7028 ปีที่แล้ว

      The code is terminating the 4th dot which is always at the end

  • @forceboxed
    @forceboxed 2 ปีที่แล้ว

    Inner loop should be replaced by if conditions

  • @nuamaaniqbal6373
    @nuamaaniqbal6373 2 ปีที่แล้ว

    thanks man!

  • @tonyz2203
    @tonyz2203 2 ปีที่แล้ว +5

    on line 21, why do we check i == j?

    • @tonyz2203
      @tonyz2203 2 ปีที่แล้ว +8

      this makes more sense to me :
      on line 21 :
      len(s[i:j+1]) == 1 or s[i]!="0"

    • @ayush03d
      @ayush03d 2 ปีที่แล้ว

      I think it's because you can have a single 0 but not any more digit after it. i == j ensures single zero.

    • @ayush03d
      @ayush03d 2 ปีที่แล้ว

      It's similar to saying:
      if (j != i && s.charAt(i) == '0') return;
      if we are not at the first index of our current substring, check if the first index is zero.

  • @skyjoe3655
    @skyjoe3655 4 หลายเดือนก่อน

    This is tricky to implement correctly in the real interview

  • @nandinisraghavan8788
    @nandinisraghavan8788 3 ปีที่แล้ว

    Can u do diagonal traversal of tree in java ?

  • @MuskanMall-y6d
    @MuskanMall-y6d หลายเดือนก่อน

    what does it mean == j mean? I am not sure i understand

  • @bentato
    @bentato 3 ปีที่แล้ว

    thanks!

  • @tigerbear3038
    @tigerbear3038 2 ปีที่แล้ว

    Can you explain how you got max height = 5?

    • @vuluongtrieu2609
      @vuluongtrieu2609 2 ปีที่แล้ว

      I think because of the call stack only 4 level deep. The dots > 4 return, prevent the call go deeper even when we not reach the end of the string.

  • @berylbose8635
    @berylbose8635 2 ปีที่แล้ว

    Anybody has java code for this??? I am getting some error, when I write code like his. Must be some java error.

    • @KuchhBhiLab
      @KuchhBhiLab ปีที่แล้ว

      class Solution {
      public List restoreIpAddresses(String s) {
      ArrayList ans = new ArrayList();
      if(s.length() > 12) return ans;
      helper(s,0,0,"",ans);
      return ans;
      }
      private void helper(String s, int i, int dots, String res, ArrayList ans){
      if(dots == 4 && i == s.length()){ // Base
      ans.add(res.substring(0,res.length()-1));
      return;
      }
      if(dots > 4){
      return;
      }

      for(int j = i; j < Math.min(i+3,s.length());j++){
      int currnum = Integer.parseInt(s.substring(i,j+1));
      if(currnum

  • @hts2370
    @hts2370 2 ปีที่แล้ว +3

    so hard

  • @anman1575
    @anman1575 10 หลายเดือนก่อน

    your code is incorrect it should be

  • @hanif2285
    @hanif2285 8 วันที่ผ่านมา

    No sir, you are very very far from being dumb.

  • @jatingoswami4839
    @jatingoswami4839 ปีที่แล้ว

    bro why are you always saying 4 dots when it is crystal clear that we need 3 dots to bifurcate string to 4 part

    • @scykl3
      @scykl3 ปีที่แล้ว +2

      did you even watch the video