Restore IP Addresses - Leetcode 93 - Python

  Рет қаралды 45,574

NeetCode

NeetCode

Күн бұрын

Пікірлер: 51
@worldwide6626
@worldwide6626 3 жыл бұрын
I love how you did a problem related to IP address from facebook after facebook outage
@Iamnoone56
@Iamnoone56 3 жыл бұрын
you can add this if len(s)12: return [ ] as constraints are 0
@arina1193
@arina1193 11 ай бұрын
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
@aditii5150
@aditii5150 Ай бұрын
This is so difficult, I got this question in my Oracle interview today
@LakshyGupta
@LakshyGupta 2 жыл бұрын
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
@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
@michaell8237
@michaell8237 3 жыл бұрын
Thank you Neetcode! Your explanation is really clear and making sense!
@superfakerbros
@superfakerbros 2 жыл бұрын
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 3 ай бұрын
Check if you are incrementing dots while making the recursive call to backtrack
@shuhaoliang144
@shuhaoliang144 2 жыл бұрын
thank you very much. one more problem that I fully understand now after watching your explanation:)
@theunknown2090
@theunknown2090 3 жыл бұрын
Bit confused where is back tracking happening here ?
@sujithameriga7348
@sujithameriga7348 2 ай бұрын
Please discuss the Time complexity for backtracking solutions.
@akhma102
@akhma102 Ай бұрын
Thank you, Neet!
@maaph1
@maaph1 Жыл бұрын
wow, i have no idea how i would come up with this solution in a real interview. 😔
@ygwg6145
@ygwg6145 Жыл бұрын
Another way: O(n^4) by iterating the possible locations of the 3 dots.
@shantipriya370
@shantipriya370 2 ай бұрын
best explanation..
@jasonthai9647
@jasonthai9647 2 жыл бұрын
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 Жыл бұрын
He made it to google, and then left.
@edwardteach2
@edwardteach2 2 жыл бұрын
U an IP God
@starstarhaha
@starstarhaha 7 ай бұрын
thanks. condition dots>4 is not needed
@Mcfly868
@Mcfly868 2 жыл бұрын
Isnt it 3^3? you are placing 3 dots, and each dot position can have 3 choices
@odelyaholiday9519
@odelyaholiday9519 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
@QinBinHua
@QinBinHua 3 жыл бұрын
thx a lot, learned a lot from your channel, keep it up, bro.
@forceboxed
@forceboxed 2 жыл бұрын
Inner loop should be replaced by if conditions
@skyjoe3655
@skyjoe3655 4 ай бұрын
This is tricky to implement correctly in the real interview
@陳柏仰-j5n
@陳柏仰-j5n 3 жыл бұрын
good as always
@rishabhroy5259
@rishabhroy5259 8 ай бұрын
you could have added the iterative solution too
@nandinisraghavan8788
@nandinisraghavan8788 3 жыл бұрын
Can u do diagonal traversal of tree in java ?
@tonyz2203
@tonyz2203 2 жыл бұрын
on line 21, why do we check i == j?
@tonyz2203
@tonyz2203 2 жыл бұрын
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.
@MuskanMall-y6d
@MuskanMall-y6d Ай бұрын
what does it mean == j mean? I am not sure i understand
@nuamaaniqbal6373
@nuamaaniqbal6373 2 жыл бұрын
thanks man!
@bentato
@bentato 3 жыл бұрын
thanks!
@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
@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.
@hts2370
@hts2370 2 жыл бұрын
so hard
@anman1575
@anman1575 11 ай бұрын
your code is incorrect it should be
@VikasShivashankara
@VikasShivashankara 2 ай бұрын
I agree on this
@TheMadisonBluesBand
@TheMadisonBluesBand 6 күн бұрын
He fixes it in the end
@hanif2285
@hanif2285 Ай бұрын
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 Жыл бұрын
did you even watch the video
@TheMadisonBluesBand
@TheMadisonBluesBand 6 күн бұрын
Are you daft
Stone Game - Leetcode 877 - Python
22:00
NeetCode
Рет қаралды 31 М.
Snakes and Ladders - Leetcode 909 - Python
21:22
NeetCode
Рет қаралды 54 М.
Players push long pins through a cardboard box attempting to pop the balloon!
00:31
ТЮРЕМЩИК В БОКСЕ! #shorts
00:58
HARD_MMA
Рет қаралды 2,7 МЛН
Как Я Брата ОБМАНУЛ (смешное видео, прикол, юмор, поржать)
00:59
Long Nails 💅🏻 #shorts
00:50
Mr DegrEE
Рет қаралды 16 МЛН
Premature Optimization
12:39
CodeAesthetic
Рет қаралды 835 М.
Validate IP Address | Regex | Leetcode #468
26:32
Techdose
Рет қаралды 39 М.
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 303 М.
Edit Distance - Dynamic Programming - Leetcode 72 - Python
21:00
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 150 М.
Being Competent With Coding Is More Fun
11:13
TheVimeagen
Рет қаралды 118 М.
Mastering Dynamic Programming - How to solve any interview problem (Part 1)
19:41
Microservices are Technical Debt
31:59
NeetCodeIO
Рет қаралды 649 М.
Dynamic Programming isn't too hard. You just don't know what it is.
22:31
DecodingIntuition
Рет қаралды 199 М.
Players push long pins through a cardboard box attempting to pop the balloon!
00:31