Splitting a String Into Descending Consecutive Values - Leetcode 1849 - Python

Splitting a String Into Descending Consecutive Values - Leetcode 1849 - Python

NeetCode

3 года назад

16,036 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

frida
frida - 28.03.2023 05:21

slightly optimized code --> just did a bit more pruning

class Solution:
def splitString(self, s: str) -> bool:
def backtrack(low, lastNum):
for i in range(low, len(s)-1):
split = int(s[low: i+1]);
if (lastNum == '#' or lastNum-1 == split):
if backtrack(i+1, split):
return True;
if lastNum != '#' and int(s[low:]) == lastNum - 1:
return True;
return False;
return backtrack(0, '#');

Ответить
Abdel Megahed
Abdel Megahed - 03.02.2023 04:12

Woow. I strugled a lot with this problem. Super simple explanation, thank you!

Ответить
orel lavie
orel lavie - 11.01.2023 10:30

There is far more optimal solution (O(n^2) time and same space complexity (O(n) ). run a loop until half of the size of the string (min 2 numbers to be consecutive), and build all the string below it with a loop and num -1 each time. concatenate all of them to a one string and compare to the input. If equal you found, if not, -1 if you got into half of size

Ответить
Jason Swift
Jason Swift - 01.01.2023 19:40

The maximum value of s[:i + 1] exceeds the maximum value of int, so it is wrong to convert s[:i + 1] with int here.

Ответить
Jimmy Cheng
Jimmy Cheng - 18.05.2022 17:35

Why do we need two similar loops? the i and j functions? Also it seems one is iterations of initial value, is that correct? Why can't they be written together?

Ответить
Varun Mittal
Varun Mittal - 26.02.2022 18:33

This gave me 95% efficiency
---------------------
def splitString(self, s: str) -> bool:
n = len(s)
def dfs(i, prev):
if i == len(s):
return False

if prev and (prev - int(s[i:]) == 1):
return True

for k in range(i, n):
num = int(s[i:k+1])
if prev is not None and prev - num != 1:
continue
if dfs(k+1, num):
break
else:
return False
return True

return dfs(0, None)

Ответить
RJ
RJ - 30.01.2022 19:43

you deserve way more subscribers!!!

Ответить
Number Onep
Number Onep - 23.01.2022 04:54

Lovely content as usual! Btw, I used a default prev=None and base case check to avoid having to deal with the necessity to have at least one split, and looks like it works too :d
class Solution:
def splitString(self, s: str) -> bool:
def dfs(ind, prev=None):
if ind>=len(s) and prev is not None and len(prev)!=len(s):
return True
for i in range(ind, len(s)):
newPrev = s[ind:i+1]
isValid = prev is None or int(prev)==int(newPrev)+1
if isValid and dfs(i+1, newPrev):
return True
return False
return dfs(0)

Ответить
ken Tsang
ken Tsang - 18.12.2021 20:17

Thank you so much

Ответить
Jiawei Yue
Jiawei Yue - 15.06.2021 13:25

Hi NeetCode, I think your solution cant AC anymore because of this test case "050043".

Ответить
KMS
KMS - 10.06.2021 09:59

dude, please use cpp or java if possible

Ответить
Mandeep singh
Mandeep singh - 08.05.2021 15:10

Thank you for your Clear and to the point Explanation.

Ответить
Hong Xuan
Hong Xuan - 03.05.2021 11:18

How did you know this question is asked by Google? I checked the company tags under leetcode and it doesn't show any

Ответить
Algorithmo
Algorithmo - 02.05.2021 20:31

leetcode 1849
This question was identified as an interview question from here: @t​

Can you give me the direct github link?

Ответить
Algorithmo
Algorithmo - 02.05.2021 20:23

Hi Neetcode do you have backtracking playlist? I find your backtracking code super simple unlike other backtracking code which involves of popping the last element after returning the recursive call.

Ответить
entropy
entropy - 02.05.2021 20:07

I was able to solve this by myself. I am so hyped

Ответить
sunnilabeouf
sunnilabeouf - 02.05.2021 19:10

Can you do the "Ones and Zeroes" problem?

Ответить