Restore IP Addresses - Leetcode 93 - Python

preview_player
Показать описание


0:00 - Read the problem
4:30 - Drawing Explanation
10:25 - Coding Explanation

leetcode 93

#coding #interview #python
Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.
Рекомендации по теме
Комментарии
Автор

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

worldwide
Автор

you can add this
if len(s)<4 or len(s)>12:
return [ ]

as constraints are
0<=s.length<=20

Iamnoone
Автор

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

arina
Автор

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

Anthony-mpmf
Автор

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

khalmasonart
Автор

The integer validation checking condition should be int(s[i:j+1]) <=255

girishmakarandthatte
Автор

Bit confused where is back tracking happening here ?

theunknown
Автор

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

shuhaoliang
Автор

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

michaell
Автор

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

superfakerbros
Автор

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. <3 Hope to see you at Google one day :)

jasonthai
Автор

Java Code:
:: LC All Passed ::

class Solution {
public List<String> restoreIpAddresses(String s) {
ArrayList<String> 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<String> 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 <= 255 && (i == j || s.charAt(i) != '0')){
helper(s, j+1, dots+1, res + s.substring(i, j+1) + ".", ans);
}
}
}
}

LakshyGupta
Автор

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

ygwg
Автор

This is tricky to implement correctly in the real interview

skyjoe
Автор

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

QinBinHua
Автор

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

Mcfly
Автор

you could have added the iterative solution too

rishabhroy
Автор

thanks. condition dots>4 is not needed

starstarhaha
Автор

Inner loop should be replaced by if conditions

forceboxed
Автор

Can u do diagonal traversal of tree in java ?

nandinisraghavan