inputs
stringlengths 50
14k
| targets
stringlengths 4
655k
|
|---|---|
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)
maxi=0
def sub(ind,ar):
if(ind==n):
if(len(ar)==len(set(ar))):
return len(ar)
return maxi
a=ar+[s[ind]]
b=ar[:]
b[-1]+=s[ind]
return max(sub(ind+1,a),sub(ind+1,b))
return sub(1,[s[0]])
|
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 = 0
partition = []
def backtrack(start_i):
if start_i == len(s):
self.res = max(self.res, len(set(partition)))
else:
for j in range(start_i + 1, len(s) + 1):
partition.append(s[start_i:j])
backtrack(j)
partition.pop()
backtrack(0)
return self.res
|
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, i, visited):
if i == len(s):
yield len(visited)
return
for j in range(i + 1, len(s) + 1):
if s[i:j] not in visited:
visited.add(s[i:j])
yield from self._dfs(s, j, visited)
visited.remove(s[i:j])
def maxUniqueSplit(self, s: str) -> int:
if not s:
return 0
return max(self._dfs(s, 0, set()))
|
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, path):
res = 0
if not s: return len(path)
for i in range(1,len(s)+1):
res = max(res, dfs(s[i:],path|{s[:i]}))
return res
return dfs(s,set())
|
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 = 1
def dfs(pos, path):
nonlocal res
if pos == len(s):
res = max(res, len(set(path)))
for i in range(pos + 1, len(s) + 1):
path.append(s[pos: i])
dfs(i, path)
path.pop()
dfs(0, [])
return res
|
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
self.answer = 0
substrings = []
def backtrack(start_i):
if start_i == len(s):
self.answer = max(self.answer, len(set(substrings)))
return
for j in range(start_i+1, len(s)+1):
substrings.append(s[start_i:j])
backtrack(j)
substrings.pop()
backtrack(0)
return self.answer
|
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.bt(s, set(), 0, 0)
def bt(self, s, d, l, maxl):
if l >= len(s):
maxl = max(len(d), maxl)
return maxl
else:
for r in range(l+1,len(s)+1):
if s[l:r] not in d: # before put the current stage into your dictionary, make sure it meet the condition.
d.add(s[l:r]) # then, you have two choices: put
maxl=self.bt(s, d, r, maxl)
d.remove(s[l:r]) # the other choice is not put. (add, then remove)
return maxl
|
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 = 1
def dfs(pos, path):
nonlocal res
if pos == len(s):
#print(path)
res = max(res, len(set(path)))
for i in range(pos + 1, len(s) + 1):
path.append(s[pos: i])
dfs(i, path)
path.pop()
dfs(0, [])
return res
|
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 solve(S, subs):
if not S:
return len(subs)
ans = 0
for i in range(len(S)):
sub = S[:i+1]
ans = max(ans, solve(S[i+1:], subs | {sub}))
return ans
return solve(s, set())
|
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
|
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.best = 1
self.solve(0, set(), 0)
return self.best
def solve(self, index, seen, count):
s = self._s
if index >= len(s):
self.best = max(self.best, count)
for end in range(index+1, len(s)+1):
if s[index:end] not in seen:
seen.add(s[index:end])
self.solve(end, seen, count+1)
seen.remove(s[index:end])
|
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
|
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):
# print(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])
|
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.result = 0
def maxUniqueSplit(self, s: str) -> int:
if not s:
return 0
def backtrack(start, cur):
if start == len(s):
self.result = max(self.result, len(set(cur)))
return
for i in range(start, len(s)):
cur += [s[start:i+1]]
backtrack(i + 1, cur)
cur.pop()
backtrack(0, [])
return self.result
|
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 max_unique_substrings(self, s, seen=()):
maximum = 0
for i in range(1, len(s) + 1):
candidate = s[:i]
if candidate not in seen:
maximum = max(maximum, 1 + self.max_unique_substrings(s[i:], {candidate, *seen}))
return maximum
def maxUniqueSplit(self, s: str) -> int:
return self.max_unique_substrings(s)
|
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 = 1
def dfs(pos, path):
nonlocal res
if pos == len(s):
res = max(res, len(set(path)))
for i in range(pos + 1, len(s) + 1):
path.append(s[pos: i])
dfs(i, path)
path.pop()
dfs(0, [])
return res
|
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
def dfs(cur_pos):
nonlocal res
if cur_pos == len(s):
res = max(res, len(seen))
for next_pos in range(cur_pos, len(s)):
if s[cur_pos:next_pos + 1] not in seen:
seen.add(s[cur_pos:next_pos + 1])
dfs(next_pos + 1)
seen.remove(s[cur_pos:next_pos + 1])
seen = set()
dfs(0)
return res
|
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(i,s,seen):
if i > len(s)-1:
return len(seen)
max_len = 1
for k in range(i,len(s)):
temp = set(seen)
temp.add(s[i:k+1])
max_len = max(max_len, helper(k+1, s,temp))
return max_len
return helper(0,s,set([]))
|
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
self. res = 0
stack = []
def backtrack(ind):
if ind == len(s):
self.res = max(self.res, len(set(stack)))
for i in range(ind+1, len(s)+1):
stack.append(s[ind:i])
backtrack(i)
stack.pop()
backtrack(0)
return self.res
|
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):
#print(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
|
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
from math import ceil
class Solution:
def maxUniqueSplit(self, s: str) -> int:
l, r = 1, len(s)
def check(n):
if n == 1: return True
pos = [i + 1 for i in range(len(s) - 1)]
for dividers in combinations(pos, n - 1):
i = 0
words = set()
flag = 0
for p in dividers:
if s[i: p] in words:
flag = 1
break
words.add(s[i: p])
i = p
if not flag and s[i: len(s)] not in words:
return True
return False
while l < r:
mid = l + (r - l) // 2 + 1
if check(mid):
l = mid
else:
r = mid - 1
return r
|
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)
|
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
ltrs = set(list(s))
counts = {}
for ltr in ltrs:
counts[ltr] = s.count(ltr)
unique_count = 0
for ltr, count in list(counts.items()):
if count == 1:
index = s.index(ltr)
if index == 0 or index == len(s)-1:
s = s.replace(ltr,'')
unique_count = unique_count + 1
else:
prev = s[index-1]
next = s[index+1]
if counts[prev] == 1 and counts[next] == 1:
s = s.replace(ltr, '')
unique_count = unique_count + 1
if len(s) == 0:
return unique_count
else:
return unique_count + self.find_max(len(s)-1, s)
def find_max(self, i, s):
from sortedcontainers import SortedSet
unique, unique_count = False, -1
words = s.split()
if len(words) == len(set(words)):
unique_count = len(words)
if i == 0:
return unique_count
a = self.find_max(i-1, s[:i] + ' ' + s[i:])
b = self.find_max(i-1, s)
return max(a,b,unique_count)
|
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 dfs(s, seen=seen):
if not s:
return 0
res = 0
for i in range(1, len(s)+1):
if s[:i] in seen:
continue
seen.add(s[:i])
res = max(res, 1 + dfs(s[i:]))
seen.remove(s[:i])
return res
return dfs(s)
|
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
|
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]
|
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)
#print(cache[i])
return cache[i]
|
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, seen):
return max((1 + helper(s[i:], {candidate, *seen}) for i in range(1, len(s) + 1) if (candidate := s[:i]) not in seen), default=0)
rt = helper(s, set())
return rt
|
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
|
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:
#if cmb<2**10:
#print(cmb,bs)
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)
#print(n,cmb,ans,spl)
return ans
|
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
|
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
# if i >= n:
# # currli.append(s)
# if s not in visiting:
# return True
# else:
# return False
for i in range(1, len(s) + 1):
# print(s[0:i], s)
if s[0:i] not in visiting:
visiting.add(s[0:i])
currli.append(s[0:i])
dfs(i, s[i:])
# if dfs(i, s[i:]):
# return True
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])
# print(s[0:i], s[i:])
dfs(i, s[i:])
# if dfs(i, s[i:]):
# break
currli.pop()
visiting.remove(s[0:i])
# return len(currli)
return self.maxval
|
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))
# print(lst)
return ans
|
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, seen= ()) -> int:
return max((1 + self.maxUniqueSplit(s[i:], {candidate, *seen}) for i in range(1, len(s) + 1) if (candidate := s[:i]) not in seen), default=0)
|
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
def dfs(before_res, cur_s):
nonlocal res
if len(cur_s) == 0:
res = max(res, len(before_res))
for i in range(len(cur_s)):
if cur_s[:i + 1] not in before_res:
before_res.add(cur_s[:i + 1])
dfs(before_res, cur_s[i + 1:])
before_res.remove(cur_s[:i + 1])
dfs(set(), s)
return res
|
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
else:
if 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
|
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 copy
class Solution:
def maxUniqueSplit(self, s: str) -> int:
n = len(s)
def dfs(temp, s, res):
if len(s) == 0:
if len(''.join(list(set(temp)))) == n and len(temp) > len(res):
res = copy.deepcopy(temp)
return res
for i in range(1, len(s) + 1):
temp.append(s[:i])
# print(temp)
res = dfs(temp, s[i:], res)
temp.pop()
return res
return len(dfs([], s, []))
|
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:
# my solution after contest ends ... 68 ms ... 80 % ... 13.6 MB ... 100 %
# time: O()
# space: O()
def backtrack(seen, sidx):
if sidx == len(s):
self.res = max(self.res, len(seen))
return
if len(seen) + len(s) - sidx <= self.res: # 截断条件提速 600 ms ===> 68 ms
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
|
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, seen):
# return max((1 + helper(s[i:], {candidate, *seen}) for i in range(1, len(s) + 1) if (candidate := s[:i]) not in seen), default=0)
# rt = helper(s, set())
# return rt
class Solution:
def maxUniqueSplit(self, s: str) -> int:
self.res = 0
def dfs(s, path):
if not s: self.res = max(self.res, len(path))
for i in range(1,len(s)+1):
dfs(s[i:],path|{s[:i]})
dfs(s,set())
return self.res
|
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 max_uni_seq(s, vis=()):
return max((1 + max_uni_seq(s[i:], {sol, *vis}) for i in range(1, len(s) + 1) if (sol := s[:i]) not in vis), default=0)
return max_uni_seq(s)
|
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 functools
class Solution:
def maxUniqueSplit(self, s: str) -> int:
@functools.lru_cache(None)
def dp(i):
res=[{s[:i+1]}]
for j in range(0,i):
for sets in dp(j):
if s[j+1:i+1] not in sets:
res.append(sets|{s[j+1:i+1]})
return res
return max([len(set_) for set_ in dp(len(s)-1)])
|
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 max_unique(s, visited=()):
return max((1 + max_unique(s[i:], {candidate, *visited}) for i in range(1, len(s) + 1) if (candidate := s[:i]) not in visited), default=0)
return max_unique(s)
|
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]
def split(curr, ind):
if ind >= len(s):
return len(curr)
for i in range(ind + 1, len(s) + 1):
tmp = curr.copy()
tmp.add(s[ind:i])
s1 = split(tmp, i)
ans[0] = max(ans[0], s1)
return len(curr)
split(set(), 0)
return ans[0]
|
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 max_unique_substrings(s, seen=()):
return max((1 + max_unique_substrings(s[i:], {candidate, *seen}) for i in range(1, len(s) + 1) if (candidate := s[:i]) not in seen), default=0)
return max_unique_substrings(s)
|
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:
from functools import lru_cache
L = len(s)
@lru_cache(None)
def DP(idx, seen):
if idx == L:
return len(seen)
res = 0
for i in range(idx+1, L+1):
temp = s[idx:i]
if temp in seen:
continue
res = max(res, DP(i, seen|{temp}))
return res
return DP(0, frozenset())
|
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 max_unique_substrings(s, seen=()):
return max((1 + max_unique_substrings(s[i:], {candidate, *seen}) for i in range(1, len(s) + 1) if (candidate := s[:i]) not in seen), default=0)
class Solution:
def maxUniqueSplit(self, s: str) -> int:
return max_unique_substrings(s,());
|
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 solve(last, idx, cur):
if idx == len(s):
if len(cur) == len(set(cur)):
self.ans = max(self.ans, len(cur))
return
cur.append(s[last:idx+1])
solve(idx+1, idx+1, cur)
cur.pop()
solve(last, idx+1, cur)
self.ans = 0
solve(0, 0, [])
return self.ans
|
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 = 1
def dfs(pos, path):
nonlocal res
if pos == len(s):
res = max(res, len(set(path)))
for i in range(pos + 1, len(s) + 1):
path.append(s[pos:i])
dfs(i, path)
path.pop()
dfs(0, [])
return res
|
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
dfs = [[set(),s]]
while dfs:
curSet, curS = dfs.pop()
res = max(res, len(curSet))
if len(curSet)+len(curS) <= res or len(curS) == 0:
continue
for i in range(len(curS)):
if curS[:i+1] in curSet: continue
curSet.add(curS[:i+1])
dfs.append([curSet.copy(), curS[i+1:]])
curSet.remove(curS[:i+1])
return res
|
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
|
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 divide(s):
for i in range(1, len(s)):
low = s[0: i]
high = s[i: ]
yield (low, high)
for div in divide(high):
ret = [low]
ret.extend(div)
yield ret
combinations = list(divide(s))
combinations.append(s)
# print(combinations)
ret = 1
for comb in combinations:
if len(comb) == len(set(comb)):
# print(comb, set(comb))
ret = max(ret, len(comb))
return ret
|
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:
visited = set()
def dfs(start):
nonlocal visited
if start == len(s):
return 0
res = 0
for i in range(start,len(s)):
if s[start:i+1] not in visited:
visited.add(s[start:i+1])
res = max(res, 1+ dfs(i+1))
visited.remove(s[start:i+1])
else:
res = max(res, dfs(i+1))
return res
return dfs(0)
|
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 = 0
def dfs(s, path):
if not s: self.res = max(self.res, len(path))
for i in range(1,len(s)+1):
dfs(s[i:],path|{s[:i]})
dfs(s,set())
return self.res
|
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:
combin_dict = {s[0]:[set(s[0])]}
for index, char in enumerate(s[1:]):
new_dict = {}
for last_str, sets in combin_dict.items():
for combin_set in sets:
if char not in new_dict:
new_dict[char] = []
single_new_set = combin_set | set(char)
if len(''.join(combin_set)) == index+1:
new_dict[char].append(single_new_set)
new_last = last_str+char
if len(''.join(combin_set)) == index+1:
new_set = combin_set.difference(set([last_str]))
else:
new_set = combin_set
new_set.add(new_last)
if new_last not in new_dict:
new_dict[new_last] = []
new_dict[new_last].append(new_set)
combin_dict = new_dict
res = 0
for key, value in combin_dict.items():
for value_set in value:
if len(''.join(value_set)) == len(s):
res = max(res, len(value_set))
return res
|
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 f(s, seen):
if not s: return 0
res = 0
for i in range(1,len(s)+1):
chunk = s[:i]
if chunk not in seen:
res = max(res, f(s[i:], seen | {chunk}) + 1)
return res
return f(s, set())
|
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
|
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
dfs = [[set(),s]]
while dfs:
curEl = dfs.pop()
res = max(res, len(curEl[0]))
if len(curEl[0])+len(curEl[1]) <= res or len(curEl[1]) == 0:
continue
for i in range(len(curEl[1])):
if curEl[1][:i+1] in curEl[0]: continue
curEl[0].add(curEl[1][:i+1])
dfs.append([curEl[0].copy(), curEl[1][i+1:]])
curEl[0].remove(curEl[1][:i+1])
return res
|
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 helper(self, s,occur):
if len(s) == 0:
return 0
curMax = -1
for i in range(1,len(s)+1):
if s[:i] not in occur:
occur.append(s[:i])
if (s[i:],tuple(occur)) in self.memo:
res = self.memo[(s[i:],tuple(occur))]
else:
res = self.helper(s[i:], occur)
self.memo[(s[i:],tuple(occur))] = res
if res >= 0:
curMax = max(curMax, 1+res)
occur.pop()
return curMax
def maxUniqueSplit(self, s: str) -> int:
self.memo = {}
return self.helper(s,[])
|
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)
|
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
|
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:
positions = []
for i in range(1,len(s)):
positions.append(i)
if(len(s) < 3):
if(s[0] != s[1]):
return 2
maximally_split = 1
for i in range(1,len(s)):
separate_positions = list(itertools.combinations(positions,i))
for separate_position in separate_positions:
curr_position = 0
curr_idx = 0
substrings = []
while(curr_idx < len(separate_position)):
substrings.append(s[curr_position:separate_position[curr_idx]])
curr_position = separate_position[curr_idx]
curr_idx += 1
substrings.append(s[curr_position:])
length_of_unique_list = len(set(substrings))
if(length_of_unique_list == len(substrings)):
if(length_of_unique_list > maximally_split):
maximally_split = length_of_unique_list
if(len(set(list(s))) > maximally_split):
maximally_split = len(set(list(s)))
return maximally_split
|
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 max_unique_substrings(s, seen=()):
return max((1 + max_unique_substrings(s[i:], {candidate, *seen}) for i in range(1, len(s) + 1) if (candidate := s[:i]) not in seen), default=0)
class Solution:
def maxUniqueSplit(self, s: str) -> int:
return max_unique_substrings(s)
|
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 solve(start, visited, memo):
state = (start, tuple(visited))
if start >= len(s):
return 0
if state in memo:
return memo[state]
result = 0
for idx in range(start, len(s)):
cut_str = s[start:idx + 1]
pos = get_position(cut_str)
if visited[pos]:
continue
visited[pos] = True
result = max(result, 1 + solve(idx + 1, visited, memo))
visited[pos] = False
memo[state] = result
return result
def get_position(string):
return self.sub_str_pos[string]
self.sub_str_pos = {}
for i in range(len(s)):
for j in range(i, len(s)):
sub_str = s[i:j + 1]
if sub_str not in self.sub_str_pos:
self.sub_str_pos[sub_str] = len(self.sub_str_pos)
return solve(0, [False for _ in range(len(self.sub_str_pos))], {})
|
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
|
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
dfs = [[set(),s]]
while dfs:
curEl = dfs.pop()
res = max(res, len(curEl[0]))
if len(curEl[0])+len(curEl[1]) <= res or len(curEl[1]) == 0:
continue
for i in range(len(curEl[1])):
noDel = curEl[1][:i+1] in curEl[0]
if noDel: continue
curEl[0].add(curEl[1][:i+1])
dfs.append([curEl[0].copy(), curEl[1][i+1:]])
if not noDel:
curEl[0].remove(curEl[1][:i+1])
return res
|
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 collections
import itertools
class Solution:
def maxUniqueSplit(self, s: str) -> int:
def check(b):
h = {}
w = ''
b = list(b) + [0] # b[len(s)-1] should be zero
for i, v in enumerate(b):
w += s[i]
if v == 0:
if w in h: return -1
h[w] = 1
w = ''
return len(h)
ans = 1
for b in itertools.product([0,1], repeat=len(s)-1):
# if b[i]==1, b[i] is concatenated to b[i+1]
n = check(b)
ans = max(ans, n)
return ans
|
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 collections
import itertools
class Solution:
def maxUniqueSplit(self, s: str) -> int:
def check(b):
h = {}
w = ''
for i, v in enumerate(b):
w += s[i]
if v == 0:
if w in h:
return -1
else:
h[w] = 1
w = ''
w += s[-1]
if w != '':
if w in h:
return -1
else:
h[w] = 1
#print(h)
return len(h)
ans = 1
for b in itertools.product([0,1], repeat=len(s)-1):
n = check(b)
if n > 0:
ans = max(ans, n)
return ans
|
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:
# len(s) <= 16, only lower_case
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
|
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
|
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:
# dp = [[\"\"] for _ in range(len(s))]
# dp[0] = [s[0]]
# for i in range(1, len(s)):
# maxnum_ = 1
# for j in range(i):
# if s[j+1:i] not in dp[j]:
# maxnum = len(dp[j]) + 1
# if maxnum > maxnum_:
# maxnum_ = maxnum
# dp[i] = dp[j].copy()
# dp[i].append(s[j+1:i])
# print(dp)
# return len(dp[len(s)-1])
# return len(self.maxUniqueSplit_(s, len(s)-1)[-1])
# @lru_cache
# def maxUniqueSplit_(self, s, i):
# if i == 0:
# return [[s[i]]]
# else:
# max_ = 0
# toreturn = []
# for j in range(i):
# maxUniqueSplit_j = self.maxUniqueSplit_(s, j)
# for uniquesplit_j in maxUniqueSplit_j:
# if s[j+1:i+1] not in uniquesplit_j:
# if len(uniquesplit_j) + 1 > max_:
# toreturn = [uniquesplit_j + [s[j+1:i+1]]]
# max_ = len(uniquesplit_j) + 1
# elif len(uniquesplit_j) + 1 == max_:
# toreturn.append(uniquesplit_j + [s[j+1:i+1]])
# if j == i - 1 and len(toreturn) == 0:
# toreturn = [uniquesplit_j]
# print(toreturn, i)
# return toreturn
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
|
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:
dp = [[] for i in range(len(s))]
dp[0].append([s[0]])
for i in range(1, len(s)):
for j in range(len(dp[i-1])):
comb = dp[i-1][j]
dp[i].append(comb[:-1]+[comb[-1]+s[i]])
dp[i].append(comb+[s[i]])
_max = 0
for i in range(len(s)):
for j in range(len(dp[i])):
length = len(set(dp[i][j]))
if length >_max:
_max = length
return _max
|
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 collections import defaultdict
class Solution:
def maxUniqueSplit(self, s: str) -> int:
n = len(s)
memo = [[] for _ in range(n)]
def unique_split(i):
if memo[i]:
return memo[i]
memo[i].append(set([s[:i+1]]))
for j in range(i):
sets = unique_split(j)
for sub in sets:
if s[j+1:i+1] not in sub:
memo[i].append(sub|set([s[j+1:i+1]]))
return memo[i]
sublen = [len(lst) for lst in unique_split(n-1)]
# print(memo)
return max(sublen)
|
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:
from itertools import combinations
for k in reversed(list(range(len(s) + 1))):
for comb in combinations(list(range(1, len(s))), k):
split_idxs = [0, *comb, len(s)]
splits = []
for prev, next in zip(split_idxs, split_idxs[1:]):
splits.append(s[prev:next])
if len(set(splits)) == len(splits):
return len(splits)
return 0
|
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):
# print(sep)
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)
# print(sep, sett)
maxres = max(maxres, len(sett))
return maxres
|
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:
# take greedy
words = list(s)
N = len(words)
ans = 0
def backtrack(words, s):
nonlocal ans
n = len(words)
if n==0:
ans = max(len(s), ans)
for i in range(1, n+1):
if str(words[:i]) not in s:
s.add(str(words[:i]))
backtrack(words[i:], s)
s.remove(str(words[:i]))
backtrack(words, set())
return ans
|
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:
# print(s)
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
|
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
n = len(s)
# dic = {}
# def helper(length,s,res):
# if idx == n:
# return
# s = s[:]
dp = [0] * (n+1)
dp[0] = [set()]
for end in range(1,n+1):
ls = []
sub = set()
for start in range(end):
for ele in dp[start]:
last = ele.copy()
node = s[start:end]
last.add(node)
ls.append(last)
#sub = last
dp[end] = ls
res = 0
for i in dp[-1]:
if len(i) > res:
res = len(i)
return res
|
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:
to_ret = 0
for bin_i in range(2**(len(s)-1)) :
to_deal = set()
mark=True
last = s[0]
for i in range(len(s)-1) :
cn = s[i+1]
if (bin_i & (1<<i)) :
if last in to_deal :
mark = False
break
to_deal.add(last)
last = cn
else :
last += cn
if last in to_deal :
mark = False
else :
to_deal.add(last)
if mark :
to_ret = max(to_ret, len(to_deal))
return to_ret
|
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 = []
# add s[start: end]
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
|
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=[[s[0]]]
for i in s[1:]:
for _ in range(len(res)):
j=res.pop(0)
res.append(j+[i])
res.append(j[:-1]+[j[-1]+i])
return max(list(map(len,list(map(set,res)))))
|
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:
to_ret = 0
for bin_i in range(2**(len(s)-1)) :
to_deal = set()
mark=True
last = s[0]
for i in range(len(s)-1) :
cn = s[i+1]
if (bin_i & (1<<i)) > 0 :
if last in to_deal :
mark = False
break
to_deal.add(last)
last = cn
else :
last += cn
if last in to_deal :
mark = False
else :
to_deal.add(last)
if mark :
to_ret = max(to_ret, len(to_deal))
return to_ret
|
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:
# Loop over possible number of splits
for num_splits in reversed(range(1, len(s))):
num_strings = num_splits + 1
# Split the string at those starting indices
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 # We did it!
# We couldn't find a splitting
return 1
|
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 = {}
for i in s:
if i in d:
d[i] += 1
else:
d[i] = 1
if len(d) == len(s):
return len(d)
def waysToSplit(s): # return list of sequences
if not s:
return []
if len(s) == 1:
return [[s]]
else:
res = []
for i in range(len(s)):
first = [s[:i+1]]
second = waysToSplit(s[i+1:])
for seq in second:
res.append(first + seq)
res.append([s])
return res
maxCount = 1
sequences = waysToSplit(s)
#print(sequences)
for seq in sequences:
s = set()
count = 0
foundDup = False
for element in seq:
if element in s:
foundDup = True
break
else:
count += 1
s.add(element)
if not foundDup:
maxCount = max(maxCount, count)
return maxCount
|
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 functools import lru_cache
class Solution:
def maxUniqueSplit(self, s: str) -> int:
@lru_cache(None)
def rec(i, fs):
if i == len(s):
return 0
ans = float('-inf')
sub = ''
for j in range(i, len(s)):
sub += s[j]
if sub not in fs:
ans = max(ans, rec(j+1, fs | frozenset([sub])) + 1)
return ans
return rec(0, frozenset())
|
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
|
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 permutations(s):
result = [[s]]
for i in range(1,len(s)):
start = [s[:i]]
end = s[i:]
for p in permutations(end):
result.append(start + p)
return result
l = permutations(s)
#print(l)
maxt = 0
totalset = set()
for i in l:
if len(set(i)) == len(i):
if len(i) > maxt:
maxt = len(i)
return maxt
|
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
|
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 functools import lru_cache
class Solution:
def maxUniqueSplit(self, s: str) -> int:
@lru_cache(None)
def helper(s, n):
# print(s, n)
ts = set()
cs = ''
for i in range(len(s)):
cs += s[i]
if n % 2 == 1:
if cs in ts:
# print(ts, cs)
return -1
else:
ts.add(cs)
cs = ''
n = n//2
else:
n = n//2
continue
if cs != '':
if cs in ts:
# print(ts, cs)
return -1
else:
ts.add(cs)
# print(ts)
return len(ts)
r = -1
for i in range(0, 2**(len(s)-1)):
r = max(r, helper(s, i))
return r
|
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
from itertools import combinations
L=list(range(1,n))
for d in range(n-1,0,-1):
for x in combinations(L,d):
hh=[0]+list(x)+[n]
tmep=[s[hh[j]:hh[j+1]] for j in range(len(hh)-1)]
if len(set(tmep))==len(tmep):
return d+1
return 1
|
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 i in range(2 ** (n-1)):
b = '1' + bin(i)[2:].zfill(n-1) + '1'
pr = 0
w = set()
flag = True
for k in range(1, n+1):
if k == n or b[k] == '1':
chrs = s[pr: k]
if chrs in w:
flag = False
break
w.add(chrs)
pr = k
if flag:
ans = max(ans, len(w))
return ans
# @lru_cache(None)
# def dp(s):
# if s == '':
# return set()
# if len(s) == 1:
# return set([s[0]])
# ans = 0
# com = set()
# for i in range(1,len(s)+1):
# l = set([s[:i]])
# r = dp(s[i:])
# if i != len(s) and len(r) == 0: # unvalid right
# continue
# if len(l.intersection(r)) == 0:
# if len(l) + len(r) > ans:
# ans = len(l) + len(r)
# com = l.union(r)
# return com
# print(dp(s))
# return len(dp(s))
|
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 len(s) == 0:
return []
if len(s) == 1:
return [[s]]
res = []
for i in range(1,len(s) + 1):
ans = [s[:i]]
splits = split(s[i:])
if splits:
for sub in splits:
res.append(ans + sub)
else:
res.append(ans)
return res
options = split(s)
m = 0
for o in options:
if len(o) == len(set(o)):
m = max(m, len(o))
return m
|
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
|
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(s):
if len(s)==1:
return [[s]]
if len(s)==0:
return[[]]
ans = []
for i in range(1,len(s)+1):
for item in check(s[i:]):
ans.append([s[:i]]+item)
return ans
result = check(s)
ans = 0
for item in result:
if len(set(item)) == len(item):
ans = max(len(item),ans)
return ans
|
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
for i in range(1, 2**(len(s) - 1) + 1):
prev = 0
seen = set()
for j in range(0, len(s)):
if i & (1 << j):
seen.add(s[prev:(j+1)])
prev = j + 1
final = s[prev:len(s)]
if len(final):
seen.add(final)
ans = max(ans, len(seen))
return ans
|
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, ss: str) -> int:
@lru_cache(None)
def dfs(s, words):
if not s: return 0
res = 0
words = set(words)
for i in range(len(s)):
if s[:i+1] not in words:
res = max(res, 1 + dfs(s[i+1:], tuple(words | set([s[:i+1]]))))
return res
return dfs(ss, ())
|
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
|
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 < 12:
ws = split(s)
return max(map(len, ws))
else:
ans = 0
ls = split(s[:8])
rs = split(s[8:])
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
|
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): #split into substrings given bit string 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 # padding + bits
res = split(s, bb)
#print(bb, res)
if len(res) == len(set(res)):
count = max(count, len(res))
return count
|
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:
l = len(s)
for i in range(l - 1, 0, -1):
for j in itertools.combinations(range(1, l), i):
cut = [0] + list(j)
subs = []
for k in range(len(cut)):
if k == len(cut) - 1:
subs.append(s[cut[k]:])
else:
subs.append(s[cut[k]: cut[k + 1]])
if len(subs) == len(set(subs)):
return len(subs)
return 1
|
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): #split into substrings given bit string 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
max_len = 0
for i in range(2**(len(s)-1)):
b = bin(i)[2:]
b = '0'*(len(s)-len(b)) + b
res = split(s,b) #split based on bit string
if len(res)==len(set(res)): #check if the split forms valid substrings
max_len = max(max_len, len(res)) #compare against max length so far
return max_len
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.