description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
if len(s) == 1:
return 1
def splitter(str):
for i in range(1, len(str)):
start = str[0:i]
end = str[i:]
yield start, end
for split in splitter(end):
result = [start]
result.extend(split)
yield result
maxnum = 0
for split in splitter(s):
if len(set(split)) == len(split):
maxnum = max(len(split), maxnum)
if maxnum == 0:
return self.maxUniqueSplit(s[: len(s) - 1])
else:
return maxnum | CLASS_DEF FUNC_DEF VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR VAR EXPR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR EXPR FUNC_CALL VAR VAR EXPR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
max_len = [1]
def helper(i, s, vis):
if i == len(s) and i != s:
max_len[0] = max(max_len[0], len(vis))
return
j = i
while j < len(s):
if s[i : j + 1] not in vis and s[i : j + 1] != s:
vis[s[i : j + 1]] = 1
helper(j + 1, s, vis)
del vis[s[i : j + 1]]
j += 1
helper(0, s, {})
return max_len[0] | CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR DICT RETURN VAR NUMBER VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
options = []
options = self.recursiveSplit(s)
bestcount = 0
for option in options:
count = 0
encountered = {}
valid = True
for substr in option:
if substr not in encountered:
encountered[substr] = 1
count += 1
else:
valid = False
break
if valid and count > bestcount:
bestcount = count
return bestcount
def recursiveSplit(self, s: str) -> str:
if len(s) == 1:
return [[s]]
elif len(s) == 0:
return [[]]
options = []
for i in range(0, len(s)):
recursiveOptions = self.recursiveSplit(s[:i])
for option in recursiveOptions:
newoption = option.copy()
newoption.append(s[i:])
options.append(newoption)
return options | CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF VAR IF FUNC_CALL VAR VAR NUMBER RETURN LIST LIST VAR IF FUNC_CALL VAR VAR NUMBER RETURN LIST LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str, mem=None, seen=None) -> int:
if mem is None:
mem = {}
if seen is None:
seen = set()
res = 0
for i in range(0, len(s)):
if s[0 : i + 1] in seen:
continue
seen.add(s[0 : i + 1])
res = max(res, 1 + self.maxUniqueSplit(s[i + 1 :], mem, seen))
seen.remove(s[0 : i + 1])
mem[s] = res
return res | CLASS_DEF FUNC_DEF VAR NONE NONE IF VAR NONE ASSIGN VAR DICT IF VAR NONE ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
seen = set()
def helper(start):
nonlocal seen, s
length = len(s)
if start == length:
return 0
max_split, cur_split = 0, 0
for split in range(start + 1, length):
left, right = s[start:split], s[split:length]
if left in seen or right in seen or left == right:
continue
seen.add(left)
cur_split = 1 + helper(split)
seen.remove(left)
max_split = max(max_split, cur_split)
return max_split
return 1 + helper(0) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR RETURN BIN_OP NUMBER FUNC_CALL VAR NUMBER VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
def helper(start):
if len(s[start:]) == 0:
return 0
res = -math.inf
for i in range(start + 1, len(s) + 1):
if s[start:i] not in seen:
seen.add(s[start:i])
followup = helper(i)
res = max(res, 1 + followup)
seen.remove(s[start:i])
return res
seen = set()
return helper(0) | CLASS_DEF FUNC_DEF VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR NUMBER VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
n = len(s)
mx = 1
m = n - 1
total = 1 << m
state = 0
for state in range(total):
seen = set()
lo = 0
for i in range(m + 1):
if state & 1 << i:
token = s[lo : i + 1]
if token in seen:
break
else:
seen.add(token)
lo = i + 1
else:
token = s[lo:n]
if token not in seen:
seen.add(token)
mx = max(len(seen), mx)
return mx | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
book = set()
def maxUniqueSplit(self, s):
result = 0
for i in range(1, len(s) + 1):
curr = s[:i]
if curr not in self.book:
self.book.add(curr)
result = max(result, 1 + self.maxUniqueSplit(s[i:]))
self.book.remove(curr)
return result | CLASS_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
prev = [[s[0]]]
for elm in s[1:]:
current = []
for sub in prev:
copy = list(sub)
copy[-1] = copy[-1] + elm
current.append(copy)
copy = list(sub)
copy.append(elm)
current.append(copy)
prev = current
return max(len(set(combo)) for combo in prev) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST LIST VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
return self.dp(s, 0, 0, [])
def dp(self, s, start, pos, ans):
if pos == len(s):
return len(ans)
a = 0
b = self.dp(s, start, pos + 1, list(ans))
if s[start : pos + 1] not in ans:
ans.append(s[start : pos + 1])
a = self.dp(s, pos + 1, pos + 1, list(ans))
return a if a > b else b | CLASS_DEF FUNC_DEF VAR RETURN FUNC_CALL VAR VAR NUMBER NUMBER LIST VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR RETURN VAR VAR VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
n = len(s)
@lru_cache(None)
def dp(i, cur):
nonlocal ans
if len(cur) + n - i < ans:
return
if i >= n:
ans = max(ans, len(cur))
l = n - i
for k in range(1, l + 1):
cand = s[i : i + k]
if cand not in cur:
dp(i + k, cur + (cand,))
ans, cur = 0, ()
dp(0, cur)
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR RETURN IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR NONE ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
d = set()
res = 1
n = len(s)
def solve(p, ans):
nonlocal res
if p == n:
res = max(res, ans)
for i in range(p, n):
ss = "".join(s[p : i + 1])
if ss in d:
continue
d.add(ss)
solve(i + 1, ans + 1)
d.remove(ss)
solve(0, 0)
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
best = -1
ans = []
for i in range(2 ** (len(s) - 1)):
x = bin(i)
x = x[2:]
x = (len(s) - len(x) - 1) * "0" + x
tmp = ""
a = []
c = 0
for char in s:
tmp += char
if c < len(x):
if x[c] == "1":
a.append(tmp)
tmp = ""
c += 1
a.append(tmp)
if len(a) == len(set(a)):
if len(a) > best:
best = len(a)
ans = a
return len(ans) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER STRING VAR ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
def split(s, b):
res = []
x, y = s[0], b[0]
for i in range(1, len(s)):
if b[i] == y:
x += s[i]
else:
res.append(x)
x, y = s[i], b[i]
res.append(x)
return res
count = 0
for i in range(2 ** (len(s) - 1)):
b = bin(i)[2:]
bb = "0" * (len(s) - len(b)) + b
res = split(s, bb)
if len(res) == len(set(res)):
count = max(count, len(res))
return count | CLASS_DEF FUNC_DEF VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
self.res = 1
def backtrack(curr, idx):
if len(curr) != len(set(curr)):
return
elif idx == len(s):
self.res = max(self.res, len(curr))
return
for i in range(idx + 1, len(s) + 1):
backtrack(curr + [s[idx:i]], i)
backtrack([], 0)
return self.res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST NUMBER RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def dfs(self, s, req_l, segs, cur, hs):
if cur >= len(s):
return segs == req_l
for pos in range(cur, len(s)):
ts = s[cur : pos + 1]
if ts in hs:
continue
hs.add(ts)
if self.dfs(s, req_l, segs + 1, pos + 1, hs):
return True
hs.remove(ts)
return False
def maxUniqueSplit(self, s: str) -> int:
for i in range(len(s), 0, -1):
if self.dfs(s, i, 0, 0, set()):
return i
return 1 | CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER FUNC_DEF VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR RETURN VAR RETURN NUMBER VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
def builder(s, seen):
if not s:
return 0
res = 0
for i, c in enumerate(s):
candidate = s[: i + 1]
if candidate not in seen:
seen.add(candidate)
res = max(res, 1 + builder(s[i + 1 :], seen))
seen.remove(candidate)
return res
return builder(s, set()) | CLASS_DEF FUNC_DEF VAR FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
max_ = 0
def maxUniqueSplit(self, s: str) -> int:
b = len(s) - 1
ar = []
self.h(s, b, ar)
return self.max_
def h(self, s, b, ar):
ar2 = ar.copy()
if b < 0:
self.max_ = max(self.max_, len(ar2))
return 0
t = s[b:]
self.h(s, b - 1, ar2)
s = s[:b]
if t not in ar2:
ar2.append(t)
self.h(s, b - 1, ar2) | CLASS_DEF ASSIGN VAR NUMBER FUNC_DEF VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
def maxU(s, soFar=set()):
if len(s) == 1:
if s in soFar:
return 0
maxSplit = len(soFar) + 1
for partition in range(1, len(s)):
a = s[:partition]
b = s[partition:]
if a not in soFar:
maxSplit = max(maxSplit, maxU(b, soFar | {a}))
return maxSplit
return maxU(s) | CLASS_DEF FUNC_DEF VAR FUNC_DEF FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
if s == None or len(s) == 0:
return 0
self.res = 0
has = set()
def dfs(cur, lasti, ct):
if cur > len(s):
self.res = max(self.res, ct)
return
for cur in range(cur, len(s) + 1):
if s[lasti:cur] not in has:
has.add(s[lasti:cur])
ct += 1
dfs(cur + 1, cur, ct)
ct -= 1
has.remove(s[lasti:cur])
else:
while cur <= len(s) and s[lasti:cur] in has:
cur += 1
dfs(cur, lasti, ct)
dfs(1, 0, 0)
return self.res | CLASS_DEF FUNC_DEF VAR IF VAR NONE FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
def allsplit(s):
if len(s) == 1:
return [[s]]
res = [[s]]
for i in range(1, len(s)):
res += [([s[:i]] + x) for x in allsplit(s[i:])]
return res
ans = 0
for sp in allsplit(s):
if len(sp) == len(set(sp)):
ans = max(ans, len(sp))
return ans | CLASS_DEF FUNC_DEF VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN LIST LIST VAR ASSIGN VAR LIST LIST VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP LIST VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
result = []
def dfs(path, word):
if not word:
result.append(path)
return
for i in range(1, len(word) + 1):
prefix, suffix = word[:i], word[i:]
if prefix not in path:
dfs(path | {prefix}, suffix)
dfs(set(), s)
l = 1
for item in result:
l = max(len(item), l)
return l | CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
def check(st, cm):
mx = 0 if st in cm else 1
for i in range(1, len(st)):
a = st[:i]
b = st[i:]
if a not in cm:
ncm = set(cm)
ncm.add(a)
c = check(b, ncm) + 1
if c > mx:
mx = c
return mx
return check(s, set()) | CLASS_DEF FUNC_DEF VAR FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
n = len(s)
self.res = 0
def helper(curIdx, curW, curSets):
if curIdx == n:
self.res = max(self.res, len(curSets))
return
helper(curIdx + 1, curW + s[curIdx], curSets)
if curW + s[curIdx] not in curSets:
helper(curIdx + 1, "", curSets | {curW + s[curIdx]})
helper(0, "", set())
return self.res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER STRING FUNC_CALL VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | def get_sequences(symbols, length):
if length == 0:
return [[]]
sequences = []
tail_sequences = get_sequences(symbols, length - 1)
for tail_sequence in tail_sequences:
for symbol in symbols:
sequences.append(list(tail_sequence) + [symbol])
return sequences
def is_unique(sequence, s):
if s == "":
return 0
words = set()
letters = [s[0]]
for i in range(len(sequence)):
if sequence[i] == 1:
word = "".join(letters)
if word in words:
return False
words.add(word)
letters = [s[i + 1]]
else:
letters.append(s[i + 1])
word = "".join(letters)
if word in words:
return False
return True
class Solution:
def maxUniqueSplit(self, s: str) -> int:
sequences = get_sequences([0, 1], len(s) - 1)
max_split = 1
for sequence in sequences:
if is_unique(sequence, s):
max_split = max(max_split, sum(sequence) + 1)
return max_split | FUNC_DEF IF VAR NUMBER RETURN LIST LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR LIST VAR RETURN VAR FUNC_DEF IF VAR STRING RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR IF VAR VAR RETURN NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR LIST NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
N = len(s)
res = 0
for mask in itertools.product("10", repeat=N - 1):
mask = list(mask) + ["1"]
substr = set()
begin = 0
i = 0
valid = True
while True:
try:
idx = mask.index("1", begin)
begin = idx + 1
if s[i : idx + 1] in substr:
valid = False
break
substr.add(s[i : idx + 1])
i = idx + 1
except ValueError:
break
if valid:
res = max(res, mask.count("1"))
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR LIST STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
self.ans = 1
def rec(split, s):
if not s:
se = set(split)
if len(se) == len(split):
self.ans = max(self.ans, len(split))
return
for i in range(len(s)):
piece = s[0 : i + 1]
spl = split[:]
spl.append(piece)
rec(spl, s[i + 1 :])
rec([], s)
return self.ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
best_ans = 1
cur_ans = set()
def find_result(i):
nonlocal best_ans
if i >= len(s):
return len(cur_ans)
elif len(s) - i + len(cur_ans) < best_ans:
return best_ans
for j in range(i + 1, len(s) + 1):
substr = s[i:j]
if substr not in cur_ans:
cur_ans.add(substr)
best_ans = max(best_ans, find_result(j))
cur_ans.remove(substr)
return max(best_ans, len(cur_ans))
return find_result(0) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR IF BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR NUMBER VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
if not s:
return 0
cache = [None] * len(s)
setSet = self.dp(s, 0, cache)
return len(max(setSet, key=lambda x: len(x))) if setSet else 0
def dp(self, inputStr, i, cache):
if i == len(inputStr):
return [set()]
if cache[i] is not None:
return cache[i]
end = len(inputStr) + 1
res = set()
for j in range(i + 1, end):
tempSol = self.dp(inputStr, j, cache)
for substrArr in tempSol:
if inputStr[i:j] in substrArr:
continue
combined = set(substrArr)
combined.add(inputStr[i:j])
res.add(frozenset(combined))
cache[i] = frozenset(res)
return cache[i] | CLASS_DEF FUNC_DEF VAR IF VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NONE FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR RETURN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN LIST FUNC_CALL VAR IF VAR VAR NONE RETURN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
ans = 1
def getans(self, s, start, n, temp):
self.ans = max(self.ans, len(temp))
for x in range(start + 1, n + 1):
boss = set(list(temp))
boss.add(s[start:x])
self.getans(s, x, n, boss)
def maxUniqueSplit(self, s: str) -> int:
temp = set()
self.getans(s, 0, len(s), temp)
return self.ans | CLASS_DEF ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
self.seen = set()
self.result = 0
def backtrack(s, start):
if start == len(s):
return True
for end in range(start + 1, len(s) + 1):
if s[start:end] not in self.seen:
self.seen.add(s[start:end])
if backtrack(s, end):
self.result = max(self.result, len(self.seen))
self.seen.remove(s[start:end])
return False
backtrack(s, 0)
return self.result | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
def search(s):
ans = [[s]]
for i in range(1, len(s)):
ans += [([s[:i]] + comb) for comb in search(s[i:])]
return ans
a = search(s)
r = 1
for comb in a:
if len(set(comb)) == len(comb):
r = max(r, len(comb))
return r | CLASS_DEF FUNC_DEF VAR FUNC_DEF ASSIGN VAR LIST LIST VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP LIST VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
if not s:
return 0
return self.dfs(s, set())
def dfs(self, s: str, present: set) -> int:
maximum = 0
for i in range(1, len(s) + 1):
substring = s[:i]
if substring not in present:
sub_try = self.dfs(s[i:], {*present, substring})
maximum = max(maximum, 1 + sub_try)
return maximum | CLASS_DEF FUNC_DEF VAR IF VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | def aux(s, lst, i):
if i == len(s):
return 0
mx = 0
for j in range(i, len(s)):
if s[i : j + 1] not in lst:
tmp = 1 + aux(s, lst + [s[i : j + 1]], j + 1)
if tmp > mx:
mx = tmp
return mx
class Solution:
def maxUniqueSplit(self, s: str) -> int:
lst = []
return aux(s, lst, 0) | FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR LIST VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST RETURN FUNC_CALL VAR VAR VAR NUMBER VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
n = len(s)
ans = 0
for pat in range(1 << n - 1):
start = 0
end = 1
split = []
for b in bin(pat)[2:].zfill(n - 1):
if b == "1":
split.append(s[start:end])
start = end
end += 1
else:
end += 1
split.append(s[start:end])
if len(set(split)) == len(split):
ans = max(ans, len(split))
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
for answer in range(len(s), 0, -1):
num_splits = answer - 1
for split_points in combinations(range(1, len(s)), num_splits):
current = 0
words = set()
for split_point in split_points:
words.add(s[current:split_point])
current = split_point
words.add(s[current:])
if len(words) == answer:
return answer | CLASS_DEF FUNC_DEF VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
def dfs(index, visited):
nonlocal ans
if s[index:n] and s[index:n] not in visited:
ans = max(ans, len(visited) + 1)
for i in range(index, n):
if (
s[index:i]
and s[index:i] not in visited
and len(visited) + 1 + n - i > ans
):
visited.add(s[index:i])
dfs(i, visited)
visited.remove(s[index:i])
n, ans = len(s), 0
dfs(0, set())
return ans | CLASS_DEF FUNC_DEF VAR FUNC_DEF IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
n = len(s)
def back(i, curr, addhere):
if i == n:
return len(addhere) - 1
if curr + s[i] in addhere:
return back(i + 1, curr + s[i], addhere.copy())
else:
return max(
back(i + 1, "", addhere.union({curr + s[i]})),
back(i + 1, curr + s[i], addhere.copy()),
)
b = set()
b.add("")
return back(0, "", b) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER STRING FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING RETURN FUNC_CALL VAR NUMBER STRING VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
le = len(s) - 1
max = 1
for ma in range(pow(2, le) - 1, 0, -1):
bi = format(ma, "0" + str(le) + "b") + "1"
prev = 0
lis = []
valid = True
for bit in range(len(bi)):
if bi[bit] == "1":
part = s[prev : bit + 1]
if part in lis:
valid = False
break
lis.append(part)
prev = bit + 1
if valid and len(lis) > max:
max = len(lis)
return max | CLASS_DEF FUNC_DEF VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP STRING FUNC_CALL VAR VAR STRING STRING ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
N = len(s)
possibilities = []
for k in range(1, N):
comb = itertools.combinations(list(range(1, N)), k)
possibilities += [
(
[s[0 : x[0]]]
+ [s[x[i] : x[i + 1]] for i in range(len(x) - 1)]
+ [s[x[-1] :]]
)
for x in comb
]
ansr = 1
for k in possibilities:
if len(k) == len(set(k)):
ansr = max(ansr, len(k))
return ansr | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP BIN_OP LIST VAR NUMBER VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER LIST VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
def split(i):
if i >= len(s):
return len([v for v in t.values() if v])
best = 0
for j in range(i, len(s)):
ss = s[i : j + 1]
t[ss] += 1
best = max(best, split(j + 1))
t[ss] -= 1
return best
t = collections.defaultdict(int)
return split(0) | CLASS_DEF FUNC_DEF VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR NUMBER VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
@lru_cache(None)
def helper(s):
if not s:
return [[]]
splits = []
for i in range(1, len(s) + 1):
word = s[:i]
next_splits = helper(s[i:])
for split in next_splits:
if word not in split:
splits.append(split + [word])
return splits
ret = helper(s)
max_len = max([len(x) for x in ret])
return max_len | CLASS_DEF FUNC_DEF VAR FUNC_DEF IF VAR RETURN LIST LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR RETURN VAR FUNC_CALL VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
N, res = len(s), 0
for i in range(2 ** (N - 1)):
start, seen = 0, set()
for j in range(N):
if i + (1 << N - 1) & 1 << j:
seen.add(s[start : j + 1])
start = j + 1
res = max(res, len(seen))
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
self.ans = 0
n = len(s)
def backtrack(start, have):
if start >= n:
self.ans = max(self.ans, len(have))
return
for i in range(start + 1, n + 1):
tmp = s[start:i]
if tmp in have:
continue
else:
have.add(tmp)
backtrack(i, have)
have.remove(tmp)
backtrack(0, set())
return self.ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
n = len(s)
currli = []
visiting = set()
self.maxval = 1
def dfs(st, s):
if len(s) <= 0:
self.maxval = max(self.maxval, len(currli))
return True
for i in range(1, len(s) + 1):
if s[0:i] not in visiting:
visiting.add(s[0:i])
currli.append(s[0:i])
dfs(i, s[i:])
currli.pop()
visiting.remove(s[0:i])
return False
for i in range(1, n + 1):
visiting.add(s[0:i])
currli.append(s[0:i])
dfs(i, s[i:])
currli.pop()
visiting.remove(s[0:i])
return self.maxval | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
have = set()
self.cur = []
def dfs(i):
if i == len(s):
return 0 if "".join(self.cur) not in have else -float("inf")
self.cur.append(s[i])
ret = dfs(i + 1)
st = "".join(self.cur)
if st not in have:
tmp, self.cur = self.cur, []
have.add(st)
ret = max(dfs(i + 1) + 1, ret)
have.remove(st)
self.cur = tmp
self.cur.pop()
return ret
return dfs(0) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL STRING VAR VAR NUMBER FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR IF VAR VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
def all_partitions(string):
for cutpoints in range(1 << len(string) - 1):
result = []
lastcut = 0
for i in range(len(string) - 1):
if 1 << i & cutpoints != 0:
result.append(string[lastcut : i + 1])
lastcut = i + 1
result.append(string[lastcut:])
yield result
ps = all_partitions(s)
ret = 0
for p in ps:
l = list(dict.fromkeys(p))
if len(p) == len(l):
ret = max(ret, len(p))
return ret | CLASS_DEF FUNC_DEF VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
def numway(s):
nw = 0
for i in range(1, len(s) + 1):
if s[:i] not in nq:
nq.add(s[:i])
nw = max(nw, 1 + numway(s[i:]))
nq.remove(s[:i])
return nw
nq = set()
return numway(s) | CLASS_DEF FUNC_DEF VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
n = len(s)
res = 1
if n == 1:
return res
for x in range(2 ** (n - 1)):
m = 1 << n - 2
p0 = 0
p1 = 0
st = set()
done = False
while m:
if x & m:
sr = s[p0 : p1 + 1]
if sr in st:
done = True
break
st.add(sr)
p0 = p1 + 1
p1 = p1 + 1
else:
p1 += 1
m >>= 1
if not done:
sr = s[p0 : p1 + 1]
if sr not in st:
res = max(res, len(st) + 1)
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
def backtrack(seen, sidx):
if sidx == len(s):
self.res = max(self.res, len(seen))
return
if len(seen) + len(s) - sidx <= self.res:
return
for eidx in range(sidx, len(s)):
if s[sidx : eidx + 1] not in seen:
backtrack(seen | {s[sidx : eidx + 1]}, eidx + 1)
self.res = 1
backtrack(set(), 0)
return self.res | CLASS_DEF FUNC_DEF VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN IF BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def func(self, s, idx, words):
max_result = 0
for i in range(idx + 1, len(s)):
max_result = max(max_result, self.func(s, i, words | {s[idx:i]}))
max_result = max(max_result, len(words | {s[idx:]}))
return max_result
def maxUniqueSplit(self, s: str) -> int:
return self.func(s, 0, set()) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF VAR RETURN FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
result = 1
for l in itertools.product([True, False], repeat=len(s)):
if l[-1] == False:
continue
cache = list()
now = ""
for c, end in zip(s, l):
now += c
if end:
cache.append(now)
now = ""
if len(cache) == len(set(cache)):
result = max(result, len(cache))
return result | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR LIST NUMBER NUMBER FUNC_CALL VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
n = len(s)
def dp(cur, ss):
if cur == n - 1:
return len(set(ss.split()))
cur += 1
return max(dp(cur, ss + s[cur]), dp(cur, ss + " " + s[cur]))
return dp(0, s[0]) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR STRING VAR VAR RETURN FUNC_CALL VAR NUMBER VAR NUMBER VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
val = 0
if len(s) == 0:
return val
def helper(s, sett=()):
return max(
(
1 + helper(s[i:], {candidate, *sett})
for i in range(1, len(s) + 1)
if (candidate := s[:i]) not in sett
),
default=0,
)
val = helper(s)
return val | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
n = len(s)
if n == 1:
return 1
res = 1
seen = set()
for m in range(1, 1 << n - 1):
if bin(m).count("1") < res:
continue
valid = True
p = 0
for i in range(1, n + 1):
if valid:
if m & 1 << i - 1 or i == n:
if s[p:i] in seen:
valid = False
seen.add(s[p:i])
p = i
if valid:
res = max(res, len(seen))
seen = set()
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER IF FUNC_CALL FUNC_CALL VAR VAR STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR IF BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
res = 0
for count in range(len(s)):
for partitions in combinations(range(1, len(s)), count):
partitions = [0] + list(partitions) + [len(s)]
parts = [s[a:b] for a, b in zip(partitions, partitions[1:])]
if len(parts) == len(set(parts)):
res = max(res, len(parts))
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR VAR LIST FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
def dfs(i, vis):
nonlocal ans
if len(vis) + n - i < ans:
return
if i >= n:
ans = max(ans, len(vis))
l = n - i
for k in range(1, l + 1):
x = s[i : i + k]
if x not in vis:
vis.add(x)
dfs(i + k, vis)
vis.discard(x)
ans, n, vis = 0, len(s), set()
dfs(0, vis)
return ans | CLASS_DEF FUNC_DEF VAR FUNC_DEF IF BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR RETURN IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def f(self, s):
xss = []
if len(s) == 0:
temp = []
temp.append("")
xss.append(temp)
return xss
for i in range(1, len(s) + 1):
xs = self.f(s[i:])
for x in xs:
if s[:i] not in x:
x.append(s[:i])
xss.append(x)
return xss
def maxUniqueSplit(self, s: str) -> int:
x = max([len(y) for y in self.f(s)])
return x - 1 | CLASS_DEF FUNC_DEF ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR NUMBER VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
def helper(curr_set, s):
if len(s) == 0:
return len(s)
max_split = 0
for i in range(len(s)):
if s[: i + 1] not in curr_set:
curr_set.add(s[: i + 1])
max_split = max(max_split, 1 + helper(curr_set, s[i + 1 :]))
curr_set.remove(s[: i + 1])
return max_split
return helper(set(), s) | CLASS_DEF FUNC_DEF VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
self.mx = 0
self.lim = len(s)
def r(i, l, c):
if i == self.lim - 1:
if s[i] not in l:
if c + 1 > self.mx:
self.mx = c + 1
elif c > self.mx:
self.mx = c
else:
for k in range(i + 1, self.lim + 1):
if k == self.lim:
if s[i:k] not in l:
if c + 1 > self.mx:
self.mx = c + 1
elif s[i:k] not in l:
r(k, l | {s[i:k]}, c + 1)
r(0, set(), 0)
return self.mx | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER IF VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
self.s = s
self.cache = {}
count = self.maxUniqueSplitHelper(0, set())
return count
def maxUniqueSplitHelper(self, pos: int, substrings: set) -> int:
hashed = frozenset(substrings)
if (pos, hashed) in self.cache:
return self.cache[pos, hashed]
if pos == len(self.s):
return len(substrings)
count = 0
for i in range(pos, len(self.s)):
substring = self.s[pos : i + 1]
if substring in substrings:
continue
substrings.add(substring)
count = max(count, self.maxUniqueSplitHelper(i + 1, substrings))
substrings.remove(substring)
self.cache[pos, hashed] = count
return count | CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR RETURN VAR VAR FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | def partition(s):
def backtrack(index, path):
if index == len(s):
res.append(list(path))
else:
path.append(s[index])
backtrack(index + 1, path)
path.pop()
if path:
path[-1] = path[-1] + s[index]
backtrack(index + 1, path)
res = []
backtrack(0, [])
return res
class Solution:
def maxUniqueSplit(self, s: str) -> int:
ans = 0
lst = partition(s)
for prt in lst:
if len(set(prt)) == len(prt):
ans = max(ans, len(prt))
return ans | FUNC_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR IF VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER LIST RETURN VAR CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
self.maxsplits = 0
def DFS(i, substring_set):
if i == len(s):
self.maxsplits = max(len(substring_set), self.maxsplits)
return
for j in range(i + 1, len(s) + 1):
DFS(j, substring_set | {s[i:j]})
DFS(0, set())
return self.maxsplits | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
def helper(s, strings=set()):
leng_s = len(s)
leng_set = len(strings)
if leng_s == 1 and s in strings:
return 0
else:
ans = leng_set + 1
for i in range(1, leng_s):
substr = s[:i]
if substr not in strings:
ans = max(ans, helper(s[i:], strings | {substr}))
return ans
return helper(s) | CLASS_DEF FUNC_DEF VAR FUNC_DEF FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
ans = 0
queue = [(set([s[:i]]), s[i:]) for i in range(1, len(s) + 1)]
while queue:
curSet, curS = queue.pop(0)
if curS == "":
ans = max(ans, len(curSet))
for i in range(1, len(curS) + 1):
if curS[:i] in curSet:
continue
else:
tmpSet = curSet.copy()
tmpSet.add(curS[:i])
queue.append((tmpSet, curS[i:]))
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
spl = list(s)
n = len(spl)
c = collections.Counter(spl)
if all(v == 1 for v in c.values()):
return n
mx = 2 ** (n - 1) - 2
ans = 1
for cmb in range(mx, 0, -1):
bs = list(bin(cmb)[2:].zfill(n - 1))
if bs.count("1") + 1 < ans:
continue
spl = []
sub = s[0]
for i, b in enumerate(bs):
if b == "0":
sub += s[i + 1]
else:
spl.append(sub)
sub = s[i + 1]
spl.append(sub)
c = collections.Counter(spl)
if all(v == 1 for v in c.values()):
if len(spl) > ans:
ans = len(spl)
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP FUNC_CALL VAR STRING NUMBER VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | def count1(x):
xx = x
ans = 0
while x != 0:
x &= x - 1
ans += 1
return ans
def ok(mask, s):
strs = []
prev = 0
for i in range(len(s) - 1):
if 1 << i & mask:
strs.append(s[prev : i + 1])
prev = i + 1
strs.append(s[prev:])
return len(set(strs)) == len(strs)
class Solution:
def maxUniqueSplit(self, s: str) -> int:
n = len(s)
ans = 0
for i in range(1, 1 << n - 1):
if ok(i, s):
ans = max(count1(i), ans)
return ans + 1 | FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR NUMBER VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
n = len(s) - 1
ans = 1
for i in range(2**n + 1):
prev = 0
ok = True
visited = set()
for j in range(n):
if i & 1 << j:
tmp = s[prev : j + 1]
prev = j + 1
if tmp in visited:
ok = False
break
visited.add(tmp)
if not ok:
break
if not ok:
continue
tmp = s[prev:]
if tmp not in visited:
ans = max(ans, len(visited) + 1)
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR IF VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
return get_max(s, set())
def get_max(s, set):
maxi = 0
for i in range(1, len(s) + 1):
if s[:i] not in set:
set.add(s[:i])
maxi = max(maxi, 1 + get_max(s[i:], set))
set.remove(s[:i])
return maxi | CLASS_DEF FUNC_DEF VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
def helper(s, start, seen):
ans = 0
for end in range(start + 1, len(s) + 1):
added = s[start:end]
print(added)
if added not in seen:
seen.add(added)
ans = max(ans, 1 + helper(s, end, seen))
seen.remove(added)
return ans
seen = set()
return helper(s, 0, seen) | CLASS_DEF FUNC_DEF VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR NUMBER VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
def backtrack(m, s):
if len(s) == 0:
return 0
ans = 0
for i in range(1, len(s) + 1):
candidate = s[:i]
if candidate not in m:
m.add(candidate)
ans = max(ans, 1 + backtrack(m, s[i:]))
m.remove(candidate)
return ans
m = set()
return backtrack(m, s) | CLASS_DEF FUNC_DEF VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | import itertools
class Solution:
def maxUniqueSplit(self, s: str) -> int:
maxres = 1
for sep in itertools.product([0, 1], repeat=len(s) - 1):
sett = set()
cur = s[0]
for i, (se, c) in enumerate(zip(sep, s[1:]), start=1):
if se:
if cur in sett:
break
sett.add(cur)
cur = c
else:
cur += c
else:
if cur in sett:
continue
sett.add(cur)
maxres = max(maxres, len(sett))
return maxres | IMPORT CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR LIST NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | from itertools import combinations
class Solution:
def maxUniqueSplit(self, s: str) -> int:
for num_splits in reversed(range(1, len(s))):
num_strings = num_splits + 1
for split_indices in combinations(range(1, len(s)), num_splits):
indices = [0] + list(split_indices) + [len(s)]
split_strs = set()
for start, end in zip(indices[:-1], indices[1:]):
split_strs.add(s[start:end])
if len(split_strs) == num_strings:
return num_strings
return 1 | CLASS_DEF FUNC_DEF VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR VAR LIST FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN VAR RETURN NUMBER VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
def helper(s1_set, s2):
N = len(s2)
if N == 1:
if s2 in s1_set:
return 0
return len(s1_set) + 1
res = 0 if s2 in s1_set else len(s1_set) + 1
for i in range(1, N):
if s2[:i] in s1_set:
continue
s1_set.add(s2[:i])
res = max(res, helper(s1_set, s2[i:]))
s1_set.discard(s2[:i])
return res
res = helper(set(), s)
return res | CLASS_DEF FUNC_DEF VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
def split(s):
if not s:
return [set()]
n = len(s)
w = set()
w.add(tuple([s]))
ret = [set([s])]
if n == 1:
return ret
if n == 2:
if s[0] != s[1]:
ret += [set([*s])]
return ret
for i in range(1, n):
ls = split(s[0:i])
rs = split(s[i:n])
for l in ls:
for r in rs:
if len(l.intersection(r)) == 0:
e = tuple(sorted(list(l | r)))
if e not in w:
w.add(e)
ret += [l | r]
return ret
if not s:
return 0
n = len(s)
if n < 10:
ws = split(s)
return max(map(len, ws))
else:
ans = 0
ls = split(s[:7])
rs = split(s[7:])
ws = []
w = set()
for l in ls:
for r in rs:
if len(l.intersection(r)) == 0:
e = tuple(sorted(list(l | r)))
if e not in w:
w.add(e)
ws += [l | r]
ans = max(ans, max(map(len, ws)))
ls = split(s[:6])
rs = split(s[6:])
ws = []
w = set()
for l in ls:
for r in rs:
if len(l.intersection(r)) == 0:
e = tuple(sorted(list(l | r)))
if e not in w:
w.add(e)
ws += [l | r]
ans = max(ans, max(map(len, ws)))
return ans | CLASS_DEF FUNC_DEF VAR FUNC_DEF IF VAR RETURN LIST FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR LIST VAR ASSIGN VAR LIST FUNC_CALL VAR LIST VAR IF VAR NUMBER RETURN VAR IF VAR NUMBER IF VAR NUMBER VAR NUMBER VAR LIST FUNC_CALL VAR LIST VAR RETURN VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR FOR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR LIST BIN_OP VAR VAR RETURN VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FOR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR LIST BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FOR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR LIST BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, string: str) -> int:
def split(s, splits):
maxs = 1 if s not in splits else 0
for i in range(1, len(s)):
if s[:i] in splits:
continue
rst = 1 + split(s[i:], splits + [s[:i]])
maxs = max(rst, maxs)
return maxs
return split(string, []) | CLASS_DEF FUNC_DEF VAR FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR LIST VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
n = len(s)
ans = -1
for bit in range(1 << n - 1):
sep = []
for i in range(n - 1):
if bit >> i & 1:
sep.append(i + 1)
sep.append(n)
ww = set()
l = r = 0
ng = False
for r in sep:
w = s[l:r]
if w in ww:
ng = True
break
ww.add(w)
l = r
if ng:
continue
ans = max(ans, len(ww))
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
if len(s) == 1:
return 1
if len(s) == 2:
if s[0] == s[1]:
return 1
return 2
ans = 0
for i in range(2 ** (len(s) - 1)):
ss = format(i, "0" + str(len(s) - 1) + "b") + "1"
st = set()
count = 0
prev = 0
for j in range(len(ss)):
lth = len(st)
if ss[j] == "1":
st.add(s[prev : j + 1])
if len(st) == lth:
break
prev = j + 1
count += 1
ans = max(ans, count)
return ans | CLASS_DEF FUNC_DEF VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP STRING FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
def dfs(i):
nonlocal used, ret
if i == n:
ret = max(ret, len(used))
return
for j in range(i + 1, n + 1):
w = s[i:j]
if w in used:
continue
used.add(w)
dfs(j)
used.discard(w)
n = len(s)
used = set()
ret = 0
dfs(0)
return ret | CLASS_DEF FUNC_DEF VAR FUNC_DEF IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def __init__(self):
self.ans = 0
self.set = set()
def maxUniqueSplit(self, s: str) -> int:
self.solve(s, 0)
return self.ans
def solve(self, s: str, pos: int) -> None:
if pos > len(s):
return
self.ans = max(self.ans, len(self.set))
if self.ans - len(self.set) >= len(s) - pos:
return
for i in range(pos, len(s)):
v = s[pos : i + 1]
if v in self.set:
continue
self.set.add(v)
self.solve(s, i + 1)
self.set.remove(v) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_DEF VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR FUNC_DEF VAR VAR IF VAR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NONE |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
i = 0
self.res = 0
def sub(s, x):
if len(s) == 0:
if sorted(x) == sorted(list(set(x))):
self.res = max(self.res, len(set(x)))
return
for i in range(1, len(s) + 1):
sub(s[i:], x + [s[:i]])
res = sub(s, [])
return self.res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR LIST RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
n = len(s) - 1
ans = 1
for i in range(1 << n):
subs = set()
start = 0
flag = False
for j in range(n):
if 1 << j & i:
tmp = s[start : j + 1]
if tmp in subs:
flag = True
break
subs.add(tmp)
start = j + 1
if flag:
continue
tmp = s[start:]
if tmp in subs:
continue
ans = max(ans, len(subs) + 1)
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
lookup = set()
return self.traverse(s, 0, lookup)
def traverse(self, s, index, lookup):
if index > len(s):
return 0
array = []
for idx in range(index, len(s) + 1):
if len(s[index:idx]) > 0 and s[index:idx] not in lookup:
lookup.add(s[index:idx])
array.append(1 + self.traverse(s, idx, lookup))
lookup.remove(s[index:idx])
return max(array) if array else 0 | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR VAR NUMBER |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
def dfs(s, n):
if n < 0:
return [[s]]
else:
res = []
for i in range(1, len(s) - n):
for group in dfs(s[i:], n - 1):
res.append([s[:i]] + group)
return res
res = 1
for i in range(len(s)):
for group in dfs(s, i):
res = max(res, len(set(group)))
return res | CLASS_DEF FUNC_DEF VAR FUNC_DEF IF VAR NUMBER RETURN LIST LIST VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1.
Example 1:
Input:
{xs,ys} = {0,0}
{xd,yd} = {1,7}
matrix = 1 1 1 1 1 1 1 1 1 1
1 1 0 1 1 0 1 1 0 1
1 1 1 1 1 1 1 1 1 1
Output: 24
Explanation:
Example 2:
Input:
{xs,ys} = {0,3}
{xd,yd} = {2,2}
matrix = 1 0 0 1 0
0 0 0 1 0
0 1 1 0 0
Output: -1
Explanation:
We can see that it is impossible to
reach the cell (2,2) from (0,3).
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path.
Expected Time Complexity: O(2^(N*M))
Expected Auxiliary Space: O(N*M)
Constraints:
1 <= N,M <= 10 | class Solution:
def longestPath(
self, mat, n: int, m: int, xs: int, ys: int, xd: int, yd: int
) -> int:
if mat[xs][ys] == 0 or mat[xd][yd] == 0:
return -1
def dfs(r, c, pathlen, res):
if r == xd and c == yd:
res[0] = max(res[0], pathlen)
return
for x, y in directions:
row = r + x
col = c + y
if (
-1 < row < rows
and -1 < col < cols
and (r, c) not in visited
and mat[r][c] == 1
):
visited.add((r, c))
dfs(row, col, pathlen + 1, res)
visited.remove((r, c))
directions = [[1, 0], [-1, 0], [0, 1], [0, -1]]
rows = n
cols = m
res = [-1]
pathlen = 0
visited = set()
dfs(xs, ys, pathlen, res)
return res[0] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR RETURN FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR NUMBER VAR |
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1.
Example 1:
Input:
{xs,ys} = {0,0}
{xd,yd} = {1,7}
matrix = 1 1 1 1 1 1 1 1 1 1
1 1 0 1 1 0 1 1 0 1
1 1 1 1 1 1 1 1 1 1
Output: 24
Explanation:
Example 2:
Input:
{xs,ys} = {0,3}
{xd,yd} = {2,2}
matrix = 1 0 0 1 0
0 0 0 1 0
0 1 1 0 0
Output: -1
Explanation:
We can see that it is impossible to
reach the cell (2,2) from (0,3).
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path.
Expected Time Complexity: O(2^(N*M))
Expected Auxiliary Space: O(N*M)
Constraints:
1 <= N,M <= 10 | class Solution:
def longestPath(
self, matrix, n: int, m: int, xs: int, ys: int, xd: int, yd: int
) -> int:
currlen = 0
def safe(matrix, xs, ys):
if (
xs < 0
or ys < 0
or xs >= len(matrix)
or ys >= len(matrix[0])
or matrix[xs][ys] != 1
):
return False
else:
return True
res = 0
def solve(matrix, xs, ys, xd, yd, currlen, length):
currlen = max(currlen, length)
nonlocal res
if xs == xd and ys == yd:
res = max(res, currlen)
return
if safe(matrix, xs + 1, ys):
matrix[xs][ys] = 2
solve(matrix, xs + 1, ys, xd, yd, currlen + 1, length)
matrix[xs][ys] = 1
if safe(matrix, xs - 1, ys):
matrix[xs][ys] = 2
solve(matrix, xs - 1, ys, xd, yd, currlen + 1, length)
matrix[xs][ys] = 1
if safe(matrix, xs, ys + 1):
matrix[xs][ys] = 2
solve(matrix, xs, ys + 1, xd, yd, currlen + 1, length)
matrix[xs][ys] = 1
if safe(matrix, xs, ys - 1):
matrix[xs][ys] = 2
solve(matrix, xs, ys - 1, xd, yd, currlen + 1, length)
matrix[xs][ys] = 1
if matrix[xs][ys] == 0 or matrix[xd][yd] == 0:
return -1
solve(matrix, xs, ys, xd, yd, currlen, 0)
return res | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR |
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1.
Example 1:
Input:
{xs,ys} = {0,0}
{xd,yd} = {1,7}
matrix = 1 1 1 1 1 1 1 1 1 1
1 1 0 1 1 0 1 1 0 1
1 1 1 1 1 1 1 1 1 1
Output: 24
Explanation:
Example 2:
Input:
{xs,ys} = {0,3}
{xd,yd} = {2,2}
matrix = 1 0 0 1 0
0 0 0 1 0
0 1 1 0 0
Output: -1
Explanation:
We can see that it is impossible to
reach the cell (2,2) from (0,3).
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path.
Expected Time Complexity: O(2^(N*M))
Expected Auxiliary Space: O(N*M)
Constraints:
1 <= N,M <= 10 | class Solution:
def longestPath(
self, mat, n: int, m: int, xs: int, ys: int, xd: int, yd: int
) -> int:
if mat[xs][ys] == 0 or mat[xd][yd] == 0:
return -1
if xs == xd and ys == yd:
return 0
vis = [[(0) for i in range(m)] for j in range(n)]
def dfs(grid, sr, sc, dr, dc, vis, n, m, ans, cnt):
if sr == dr and sc == dc:
ans[0] = max(ans[0], cnt)
return
if (
sr < 0
or sc < 0
or sr >= n
or sc >= m
or vis[sr][sc] == 1
or grid[sr][sc] == 0
):
return
vis[sr][sc] = 1
dfs(grid, sr + 1, sc, dr, dc, vis, n, m, ans, cnt + 1)
dfs(grid, sr - 1, sc, dr, dc, vis, n, m, ans, cnt + 1)
dfs(grid, sr, sc + 1, dr, dc, vis, n, m, ans, cnt + 1)
dfs(grid, sr, sc - 1, dr, dc, vis, n, m, ans, cnt + 1)
vis[sr][sc] = 0
ans = [-1]
cnt = 0
dfs(mat, xs, ys, xd, yd, vis, n, m, ans, cnt)
return ans[0] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR RETURN IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR NUMBER VAR |
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1.
Example 1:
Input:
{xs,ys} = {0,0}
{xd,yd} = {1,7}
matrix = 1 1 1 1 1 1 1 1 1 1
1 1 0 1 1 0 1 1 0 1
1 1 1 1 1 1 1 1 1 1
Output: 24
Explanation:
Example 2:
Input:
{xs,ys} = {0,3}
{xd,yd} = {2,2}
matrix = 1 0 0 1 0
0 0 0 1 0
0 1 1 0 0
Output: -1
Explanation:
We can see that it is impossible to
reach the cell (2,2) from (0,3).
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path.
Expected Time Complexity: O(2^(N*M))
Expected Auxiliary Space: O(N*M)
Constraints:
1 <= N,M <= 10 | class Solution:
def longestPath(
self, mat, n: int, m: int, xs: int, ys: int, xd: int, yd: int
) -> int:
def solve(mat, n, m, xs, ys, xd, yd, cnt):
if xs == xd and yd == ys:
ans[0] = max(cnt, ans[0])
return
dir = [[0, 1], [0, -1], [1, 0], [-1, 0]]
for i, j in dir:
dx = xs + i
dy = ys + j
if (
dx >= 0
and dy >= 0
and dx <= n - 1
and dy <= m - 1
and mat[dx][dy] == 1
):
mat[dx][dy] = 0
solve(mat, n, m, dx, dy, xd, yd, cnt + 1)
mat[dx][dy] = 1
if mat[xs][ys] == 0 or mat[xd][yd] == 0:
return -1
ans = [-1]
mat[xs][ys] = 0
solve(mat, n, m, xs, ys, xd, yd, 0)
return ans[0] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR FUNC_DEF IF VAR VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER RETURN ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR NUMBER VAR |
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1.
Example 1:
Input:
{xs,ys} = {0,0}
{xd,yd} = {1,7}
matrix = 1 1 1 1 1 1 1 1 1 1
1 1 0 1 1 0 1 1 0 1
1 1 1 1 1 1 1 1 1 1
Output: 24
Explanation:
Example 2:
Input:
{xs,ys} = {0,3}
{xd,yd} = {2,2}
matrix = 1 0 0 1 0
0 0 0 1 0
0 1 1 0 0
Output: -1
Explanation:
We can see that it is impossible to
reach the cell (2,2) from (0,3).
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path.
Expected Time Complexity: O(2^(N*M))
Expected Auxiliary Space: O(N*M)
Constraints:
1 <= N,M <= 10 | class Solution:
def longestPath(self, M, R, C, sc, sr, dc, dr):
if M[sc][sr] == 0:
return -1
ans = dfs(sc, sr, R, C, M, set(), dr, dc)
return -1 if ans == 0 else ans
def dfs(r, c, R, C, M, v, dc, dr):
if (r, c) == (dr, dc):
return len(v)
v.add((r, c))
l = 0
for nr, nc in ((r + 1, c), (r - 1, c), (r, c + 1), (r, c - 1)):
if 0 <= nr < R and 0 <= nc < C and (nr, nc) not in v and M[nr][nc] == 1:
l = max(l, dfs(nr, nc, R, C, M, v, dc, dr))
v.remove((r, c))
return l | CLASS_DEF FUNC_DEF IF VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER NUMBER VAR FUNC_DEF IF VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1.
Example 1:
Input:
{xs,ys} = {0,0}
{xd,yd} = {1,7}
matrix = 1 1 1 1 1 1 1 1 1 1
1 1 0 1 1 0 1 1 0 1
1 1 1 1 1 1 1 1 1 1
Output: 24
Explanation:
Example 2:
Input:
{xs,ys} = {0,3}
{xd,yd} = {2,2}
matrix = 1 0 0 1 0
0 0 0 1 0
0 1 1 0 0
Output: -1
Explanation:
We can see that it is impossible to
reach the cell (2,2) from (0,3).
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path.
Expected Time Complexity: O(2^(N*M))
Expected Auxiliary Space: O(N*M)
Constraints:
1 <= N,M <= 10 | class Solution:
def longestPath(self, mat, n, m, xs, ys, xd, yd) -> int:
res = [0]
vis = [[(0) for i in range(m)] for i in range(n)]
if mat[xs][ys] == 0 or mat[xd][yd] == 0:
return -1
def recsol(row, col, temp):
if row == xd and col == yd:
res[0] = max(res[0], temp)
return
if (
row < 0
or col < 0
or row >= n
or col >= m
or vis[row][col] == 1
or mat[row][col] == 0
):
return
vis[row][col] = 1
recsol(row + 1, col, temp + 1)
recsol(row, col + 1, temp + 1)
recsol(row - 1, col, temp + 1)
recsol(row, col - 1, temp + 1)
vis[row][col] = 0
return
recsol(xs, ys, 0)
return res[0] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR RETURN IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR NUMBER VAR |
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1.
Example 1:
Input:
{xs,ys} = {0,0}
{xd,yd} = {1,7}
matrix = 1 1 1 1 1 1 1 1 1 1
1 1 0 1 1 0 1 1 0 1
1 1 1 1 1 1 1 1 1 1
Output: 24
Explanation:
Example 2:
Input:
{xs,ys} = {0,3}
{xd,yd} = {2,2}
matrix = 1 0 0 1 0
0 0 0 1 0
0 1 1 0 0
Output: -1
Explanation:
We can see that it is impossible to
reach the cell (2,2) from (0,3).
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path.
Expected Time Complexity: O(2^(N*M))
Expected Auxiliary Space: O(N*M)
Constraints:
1 <= N,M <= 10 | class Solution:
def longestPath(self, mat, n: int, m: int, xs: int, ys: int, xd: int, yd: int):
visited = [[(0) for i in range(m)] for j in range(n)]
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
def dfs(x, y):
if x == xd and y == yd:
return 0
mn = -float("inf")
visited[x][y] = 1
for i, j in directions:
nx = x + i
ny = y + j
if (
nx >= n
or nx < 0
or ny >= m
or ny < 0
or visited[nx][ny]
or mat[nx][ny] == 0
):
continue
mn = max(mn, 1 + dfs(nx, ny))
visited[x][y] = 0
return mn
if mat[xs][ys] == 0:
return -1
val = dfs(xs, ys)
if val == -float("inf"):
return -1
return val | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR IF VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR |
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1.
Example 1:
Input:
{xs,ys} = {0,0}
{xd,yd} = {1,7}
matrix = 1 1 1 1 1 1 1 1 1 1
1 1 0 1 1 0 1 1 0 1
1 1 1 1 1 1 1 1 1 1
Output: 24
Explanation:
Example 2:
Input:
{xs,ys} = {0,3}
{xd,yd} = {2,2}
matrix = 1 0 0 1 0
0 0 0 1 0
0 1 1 0 0
Output: -1
Explanation:
We can see that it is impossible to
reach the cell (2,2) from (0,3).
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path.
Expected Time Complexity: O(2^(N*M))
Expected Auxiliary Space: O(N*M)
Constraints:
1 <= N,M <= 10 | class Solution:
def longestPath(self, l, n, m, xs, ys, xd, yd):
if l[xs][ys] == 0 or l[xd][yd] == 0:
return -1
self.ans = -1
def ss(i, j, t):
if i == xd and j == yd:
self.ans = max(self.ans, t)
return
if i < 0 or j < 0 or i >= n or j >= m or l[i][j] == 0:
return
l[i][j] = 0
ss(i + 1, j, t + 1)
ss(i - 1, j, t + 1)
ss(i, j + 1, t + 1)
ss(i, j - 1, t + 1)
l[i][j] = 1
ss(xs, ys, 0)
return self.ans | CLASS_DEF FUNC_DEF IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR |
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1.
Example 1:
Input:
{xs,ys} = {0,0}
{xd,yd} = {1,7}
matrix = 1 1 1 1 1 1 1 1 1 1
1 1 0 1 1 0 1 1 0 1
1 1 1 1 1 1 1 1 1 1
Output: 24
Explanation:
Example 2:
Input:
{xs,ys} = {0,3}
{xd,yd} = {2,2}
matrix = 1 0 0 1 0
0 0 0 1 0
0 1 1 0 0
Output: -1
Explanation:
We can see that it is impossible to
reach the cell (2,2) from (0,3).
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path.
Expected Time Complexity: O(2^(N*M))
Expected Auxiliary Space: O(N*M)
Constraints:
1 <= N,M <= 10 | class Solution:
def longestPath(self, mat, n: int, m: int, xs: int, ys: int, xd: int, yd: int):
visited = [[(0) for i in range(m)] for j in range(n)]
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
def dfs(x, y):
if x == xd and y == yd:
return 0
mn = -float("inf")
visited[x][y] = 1
for i, j in directions:
nx = x + i
ny = y + j
if (
nx >= n
or nx < 0
or ny >= m
or ny < 0
or visited[nx][ny]
or mat[nx][ny] == 0
):
continue
mn = max(mn, 1 + dfs(nx, ny))
visited[x][y] = 0
return mn
if mat[xs][ys] == 0:
return -1
val = dfs(xs, ys)
if val == -float("inf"):
return -1
return val
class IntArray:
def __init__(self) -> None:
pass
def Input(self, n):
arr = [int(i) for i in input().strip().split()]
return arr
def Print(self, arr):
for i in arr:
print(i, end=" ")
print()
class IntMatrix:
def __init__(self) -> None:
pass
def Input(self, n, m):
matrix = []
for _ in range(n):
matrix.append([int(i) for i in input().strip().split()])
return matrix
def Print(self, arr):
for i in arr:
for j in i:
print(j, end=" ")
print()
if __name__ == "__main__":
t = int(input())
for _ in range(t):
a = IntArray().Input(2)
b = IntArray().Input(4)
mat = IntMatrix().Input(a[0], a[0])
obj = Solution()
res = obj.longestPath(mat, a[0], a[1], b[0], b[1], b[2], b[3])
print(res) | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR IF VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR CLASS_DEF FUNC_DEF NONE FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR RETURN VAR FUNC_DEF FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR CLASS_DEF FUNC_DEF NONE FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR RETURN VAR FUNC_DEF FOR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1.
Example 1:
Input:
{xs,ys} = {0,0}
{xd,yd} = {1,7}
matrix = 1 1 1 1 1 1 1 1 1 1
1 1 0 1 1 0 1 1 0 1
1 1 1 1 1 1 1 1 1 1
Output: 24
Explanation:
Example 2:
Input:
{xs,ys} = {0,3}
{xd,yd} = {2,2}
matrix = 1 0 0 1 0
0 0 0 1 0
0 1 1 0 0
Output: -1
Explanation:
We can see that it is impossible to
reach the cell (2,2) from (0,3).
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path.
Expected Time Complexity: O(2^(N*M))
Expected Auxiliary Space: O(N*M)
Constraints:
1 <= N,M <= 10 | class Solution:
def rec(self, mat, i, j, n, m, temp, ans, vis, xd, yd):
if i < 0 or i >= n or j < 0 or j >= m or vis[i][j] == 1 or mat[i][j] == 0:
return
vis[i][j] = 1
if i == xd and j == yd:
ans[0] = max(ans[0], temp)
paths = [[0, 0, 1, -1], [1, -1, 0, 0]]
for k in range(4):
self.rec(
mat, paths[0][k] + i, paths[1][k] + j, n, m, temp + 1, ans, vis, xd, yd
)
vis[i][j] = 0
def longestPath(self, mat, n, m, xs, ys, xd, yd):
ans = [-1]
vis = [([0] * m) for i in range(n)]
self.rec(mat, xs, ys, n, m, 0, ans, vis, xd, yd)
return ans[0] | CLASS_DEF FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR RETURN VAR NUMBER |
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1.
Example 1:
Input:
{xs,ys} = {0,0}
{xd,yd} = {1,7}
matrix = 1 1 1 1 1 1 1 1 1 1
1 1 0 1 1 0 1 1 0 1
1 1 1 1 1 1 1 1 1 1
Output: 24
Explanation:
Example 2:
Input:
{xs,ys} = {0,3}
{xd,yd} = {2,2}
matrix = 1 0 0 1 0
0 0 0 1 0
0 1 1 0 0
Output: -1
Explanation:
We can see that it is impossible to
reach the cell (2,2) from (0,3).
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path.
Expected Time Complexity: O(2^(N*M))
Expected Auxiliary Space: O(N*M)
Constraints:
1 <= N,M <= 10 | class Solution:
def solve(self, mat, i, j, m, n, dp, c, ans):
if (
i < 0
or j < 0
or i >= len(mat)
or j >= len(mat[0])
or mat[i][j] == 0
or dp[i][j] == False
):
return -100000000
if i == n and j == m:
return 0
dp[i][j] = False
ans = 1 + max(
self.solve(mat, i + 1, j, m, n, dp, c + 1, ans),
self.solve(mat, i, j + 1, m, n, dp, c + 1, ans),
self.solve(mat, i - 1, j, m, n, dp, c + 1, ans),
self.solve(mat, i, j - 1, m, n, dp, c + 1, ans),
)
dp[i][j] = True
return ans
def longestPath(self, mat, n, m, xs, ys, xd, yd):
if mat[xs][ys] == 0 or mat[xd][yd] == 0:
return -1
dp = [[(True) for i in range(m)] for j in range(n)]
ans = self.solve(mat, xs, ys, yd, xd, dp, 0, 0)
if ans < 0:
return -1
return ans | CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER NUMBER IF VAR NUMBER RETURN NUMBER RETURN VAR |
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1.
Example 1:
Input:
{xs,ys} = {0,0}
{xd,yd} = {1,7}
matrix = 1 1 1 1 1 1 1 1 1 1
1 1 0 1 1 0 1 1 0 1
1 1 1 1 1 1 1 1 1 1
Output: 24
Explanation:
Example 2:
Input:
{xs,ys} = {0,3}
{xd,yd} = {2,2}
matrix = 1 0 0 1 0
0 0 0 1 0
0 1 1 0 0
Output: -1
Explanation:
We can see that it is impossible to
reach the cell (2,2) from (0,3).
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path.
Expected Time Complexity: O(2^(N*M))
Expected Auxiliary Space: O(N*M)
Constraints:
1 <= N,M <= 10 | class Solution:
def longestPath(
self, mat, n: int, m: int, xs: int, ys: int, xd: int, yd: int
) -> int:
if mat[xs][ys] == 0 or mat[xd][yd] == 0:
return -1
def sol(mat, i, j, xd, yd, length):
if i == xd and j == yd:
if length > self.ans:
self.ans = length
return
dirs = [[0, 1], [0, -1], [1, 0], [-1, 0]]
for idx in range(len(dirs)):
newx, newy = i + dirs[idx][0], j + dirs[idx][1]
if (
0 <= newx < len(mat)
and 0 <= newy < len(mat[0])
and mat[newx][newy] == 1
):
mat[i][j] = 2
sol(mat, newx, newy, xd, yd, length + 1)
mat[i][j] = 1
return
self.ans = -1
sol(mat, xs, ys, xd, yd, 0)
return self.ans | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER IF NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR |
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1.
Example 1:
Input:
{xs,ys} = {0,0}
{xd,yd} = {1,7}
matrix = 1 1 1 1 1 1 1 1 1 1
1 1 0 1 1 0 1 1 0 1
1 1 1 1 1 1 1 1 1 1
Output: 24
Explanation:
Example 2:
Input:
{xs,ys} = {0,3}
{xd,yd} = {2,2}
matrix = 1 0 0 1 0
0 0 0 1 0
0 1 1 0 0
Output: -1
Explanation:
We can see that it is impossible to
reach the cell (2,2) from (0,3).
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path.
Expected Time Complexity: O(2^(N*M))
Expected Auxiliary Space: O(N*M)
Constraints:
1 <= N,M <= 10 | class Solution:
def isBound(self, x, y):
if 0 <= x < self.N and 0 <= y < self.M:
return True
return False
def dfs(self, x, y, dist):
if x == self.targetX and y == self.targetY:
self.maxLength = max(self.maxLength, dist)
return
self.mat[x][y] = 0
for delX, delY in zip(self.dx, self.dy):
newX = x + delX
newY = y + delY
if not self.isBound(newX, newY):
continue
if self.mat[newX][newY] == 0:
continue
self.dfs(newX, newY, dist + 1)
self.mat[x][y] = 1
def longestPath(self, mat, n, m, xs, ys, xd, yd):
if mat[xs][ys] == 0 or mat[xd][yd] == 0:
return -1
self.dx = [1, -1, 0, 0]
self.dy = [0, 0, 1, -1]
self.N, self.M = n, m
self.maxLength = -1
self.mat = mat
self.targetX, self.targetY = xd, yd
self.dfs(xs, ys, 0)
return self.maxLength | CLASS_DEF FUNC_DEF IF NUMBER VAR VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN ASSIGN VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_DEF IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR |
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1.
Example 1:
Input:
{xs,ys} = {0,0}
{xd,yd} = {1,7}
matrix = 1 1 1 1 1 1 1 1 1 1
1 1 0 1 1 0 1 1 0 1
1 1 1 1 1 1 1 1 1 1
Output: 24
Explanation:
Example 2:
Input:
{xs,ys} = {0,3}
{xd,yd} = {2,2}
matrix = 1 0 0 1 0
0 0 0 1 0
0 1 1 0 0
Output: -1
Explanation:
We can see that it is impossible to
reach the cell (2,2) from (0,3).
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path.
Expected Time Complexity: O(2^(N*M))
Expected Auxiliary Space: O(N*M)
Constraints:
1 <= N,M <= 10 | class Solution:
def longestPath(
self, mat, n: int, m: int, xs: int, ys: int, xd: int, yd: int
) -> int:
row = len(mat)
col = len(mat[0])
ans = []
visited = [([False] * col) for i in range(row)]
def solve(i, j, c, visited):
if (
i < 0
or j < 0
or j >= col
or i >= row
or mat[i][j] == 0
or visited[i][j] == True
):
return
if i == xd and j == yd:
ans.append(c)
return
visited[i][j] = True
solve(i + 1, j, c + 1, visited)
solve(i - 1, j, c + 1, visited)
solve(i, j - 1, c + 1, visited)
solve(i, j + 1, c + 1, visited)
visited[i][j] = False
solve(xs, ys, 0, visited)
if len(ans) == 0:
return -1
return max(ans) | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR |
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1.
Example 1:
Input:
{xs,ys} = {0,0}
{xd,yd} = {1,7}
matrix = 1 1 1 1 1 1 1 1 1 1
1 1 0 1 1 0 1 1 0 1
1 1 1 1 1 1 1 1 1 1
Output: 24
Explanation:
Example 2:
Input:
{xs,ys} = {0,3}
{xd,yd} = {2,2}
matrix = 1 0 0 1 0
0 0 0 1 0
0 1 1 0 0
Output: -1
Explanation:
We can see that it is impossible to
reach the cell (2,2) from (0,3).
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path.
Expected Time Complexity: O(2^(N*M))
Expected Auxiliary Space: O(N*M)
Constraints:
1 <= N,M <= 10 | class Solution:
def check(self, i, j, n, m, mat):
if i < n and 0 <= i and j < m and j >= 0 and mat[i][j] == 1:
return True
return False
def find(self, i, j, x, y, n, m, mat, ans, visited, cnt):
if i == x and j == y:
ans[0] = max(ans[0], cnt)
return
if self.check(i, j, n, m, mat) == False or visited[i][j] == 1:
return
visited[i][j] = 1
if self.check(i + 1, j, n, m, mat):
self.find(i + 1, j, x, y, n, m, mat, ans, visited, cnt + 1)
if self.check(i - 1, j, n, m, mat):
self.find(i - 1, j, x, y, n, m, mat, ans, visited, cnt + 1)
if self.check(i, j + 1, n, m, mat):
self.find(i, j + 1, x, y, n, m, mat, ans, visited, cnt + 1)
if self.check(i, j - 1, n, m, mat):
self.find(i, j - 1, x, y, n, m, mat, ans, visited, cnt + 1)
visited[i][j] = 0
def longestPath(self, mat, n, m, xs, ys, xd, yd):
ans = [-1]
visited = [[(0) for i in range(100)] for j in range(100)]
self.find(xs, ys, xd, yd, n, m, mat, ans, visited, 0)
return ans[0] | CLASS_DEF FUNC_DEF IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR RETURN IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR NUMBER |
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1.
Example 1:
Input:
{xs,ys} = {0,0}
{xd,yd} = {1,7}
matrix = 1 1 1 1 1 1 1 1 1 1
1 1 0 1 1 0 1 1 0 1
1 1 1 1 1 1 1 1 1 1
Output: 24
Explanation:
Example 2:
Input:
{xs,ys} = {0,3}
{xd,yd} = {2,2}
matrix = 1 0 0 1 0
0 0 0 1 0
0 1 1 0 0
Output: -1
Explanation:
We can see that it is impossible to
reach the cell (2,2) from (0,3).
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path.
Expected Time Complexity: O(2^(N*M))
Expected Auxiliary Space: O(N*M)
Constraints:
1 <= N,M <= 10 | class Solution:
def longestPath(self, arr, n, m, xs, ys, xd, yd) -> int:
if arr[xd][yd] == 0:
return -1
def helper(i, j):
if i == xd and j == yd:
return 0
if i < 0 or i >= n or j < 0 or j >= m or arr[i][j] == 0:
return -100000
arr[i][j] = 0
val = 1 + max(
helper(i + 1, j), helper(i, j + 1), helper(i - 1, j), helper(i, j - 1)
)
arr[i][j] = 1
return val
res = helper(xs, ys)
if res < 0:
return -1
return res
class IntArray:
def __init__(self) -> None:
pass
def Input(self, n):
arr = [int(i) for i in input().strip().split()]
return arr
def Print(self, arr):
for i in arr:
print(i, end=" ")
print()
class IntMatrix:
def __init__(self) -> None:
pass
def Input(self, n, m):
matrix = []
for _ in range(n):
matrix.append([int(i) for i in input().strip().split()])
return matrix
def Print(self, arr):
for i in arr:
for j in i:
print(j, end=" ")
print()
if __name__ == "__main__":
t = int(input())
for _ in range(t):
a = IntArray().Input(2)
b = IntArray().Input(4)
mat = IntMatrix().Input(a[0], a[0])
obj = Solution()
res = obj.longestPath(mat, a[0], a[1], b[0], b[1], b[2], b[3])
print(res) | CLASS_DEF FUNC_DEF IF VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN VAR VAR CLASS_DEF FUNC_DEF NONE FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR RETURN VAR FUNC_DEF FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR CLASS_DEF FUNC_DEF NONE FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR RETURN VAR FUNC_DEF FOR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.