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:
res = 0
for count in range(len(s)):
for partitions in combinations(range(1, len(s)), count):
partitions = [0] + list(partitions) + [len(s)]
parts = [s[a:b] for a, b in zip(partitions, partitions[1:])]
if len(parts) == len(set(parts)):
res = max(res, len(parts))
return res
|
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters.
|
class Solution:
def maxUniqueSplit(self, s: str) -> int:
result = 1
for l in itertools.product([True, False], repeat=len(s)):
if l[-1] == False:
continue
cache = list()
now = ''
for c, end in zip(s, l):
now += c
if end:
cache.append(now)
now = ''
if len(cache) == len(set(cache)):
result = max(result, len(cache))
return result
# length = len(s)
# cache = set()
# now = 0
# l = 1
# while now + l <= length:
# if s[now: now + l] in cache:
# l += 1
# else:
# cache.add(s[now: now + l])
# now += l
# l = 1
# return len(cache)
|
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters.
|
class Solution:
def maxUniqueSplit(self, s: str) -> int:
n = len(s)-1
ans = 1
for i in range(2**n+1):
prev = 0
ok = True
visited = set()
for j in range(n):
if i & (1<<j):
tmp = s[prev:j+1]
prev = j+1
if tmp in visited:
ok = False
break
visited.add(tmp)
#print(visited)
if not ok:
break
if not ok:
continue
tmp = s[prev:]
#print(tmp, visited)
if tmp not in visited:
#print(visited)
ans = max(ans, len(visited) + 1)
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 allsplit(s):
if len(s)==1:
return [[s]]
res = [[s]]
for i in range(1,len(s)):
res += [[s[:i]] + x for x in allsplit(s[i:])]
return res
ans = 0
for sp in allsplit(s):
# print(sp)
if len(sp) == len(set(sp)):
ans = max(ans, len(sp))
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:
max_length = 1
for code in range(2 ** (len(s) - 1)):
subs = set()
cur = s[0]
for i in range(1, len(s)):
if code % 2 == 1:
if cur in subs:
break
subs.add(cur)
cur = s[i]
else:
cur += s[i]
code = code >> 1
if cur not in subs:
max_length = max(max_length, len(subs) + 1)
return max_length
|
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters.
|
def count1(x):
xx = x
ans = 0
while x != 0:
x &= x - 1
ans += 1
return ans
def ok(mask, s):
strs = []
prev = 0
for i in range(len(s) - 1):
if (1 << i) & mask:
strs.append(s[prev: i+1])
prev = i + 1
strs.append(s[prev:])
# print(strs)
return len(set(strs)) == len(strs)
class Solution:
def maxUniqueSplit(self, s: str) -> int:
n = len(s)
ans = 0
for i in range(1, 1 << (n-1)):
if ok(i, s):
ans = max(count1(i), ans)
return ans + 1
|
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters.
|
class Solution:
def maxUniqueSplit(self, s: str) -> int:
n = len(s)-1
ans = 1
for i in range(1 << n):
subs = set()
start = 0
flag = False
for j in range(n):
if 1 << j & i:
tmp = s[start:j+1]
if tmp in subs:
flag = True
break
subs.add(tmp)
start = j+1
if flag:
continue
tmp = s[start:]
if tmp in subs:
continue
# print(subs, i)
ans = max(ans, len(subs)+1)
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:
## splitting procedure must be recursive
# \"addbsd\" a(dd)(b)(s)(d) vs (a)(d)(db)(sd)
# in general we want unique chars to be alone...but (adbdsd), can't always be satisfied
#
N = len(s);
possibilities = []
for k in range(1, N):
comb = itertools.combinations(list(range(1,N)), k)
possibilities+= [[s[0:x[0]]] + [s[x[i]:x[i+1]] for i in range(len(x)-1)] + [s[x[-1]:]] for x in comb]
ansr = 1;
for k in possibilities:
if(len(k) == len(set(k))):
ansr = max(ansr, len(k));
return ansr;
|
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)
#print(res)
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
|
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 collections import defaultdict
self.ans=1
def get(s,l,n):
# print(l,n,len(s))
if n==len(s):
dic=defaultdict(int)
for i in l:
dic[i]+=1
z=True
for i in dic:
if dic[i]>1:
z=False
break
if z:
self.ans=max(self.ans,len(l))
return
i=n
cur=''
# print(\"here\")
for j in range(i,len(s)):
# print(j)
cur+=s[j]
# print(cur)
get(s,l+[cur],j+1)
get(s,[],0)
return self.ans
# dic=defaultdict(int)
# n=len(s)
# i=0
# ans=[]
# while i<n:
# cur=''
# j=i
# z=True
# while j<n:
# cur+=s[j]
# if dic[cur]==0:
# ans.append(cur)
# z=False
# dic[cur]=1
# j+=1
# break
# else:
# j+=1
# i=j
# if z:
# ans[-1]=ans[-1]+cur
# return len(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:
to_ret = 0
for bin_i in range(2**(len(s)-1)) :
to_deal = []
last = s[0]
for i in range(len(s)-1) :
cn = s[i+1]
# print(bin_i, i, (bin_i | (1<<i)))
if (bin_i & (1<<i)) > 0 :
to_deal.append(last)
last = cn
else :
last += cn
if not last == '' :
to_deal.append(last)
if len(to_deal) == len(set(to_deal)) :
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:
def split(i):
if i >= len(s):
return len([v for v in t.values() if v])
best = 0
for j in range(i, len(s)):
ss = s[i:j+1]
t[ss] += 1
best = max(best, split(j+1))
t[ss] -= 1
return best
t = collections.defaultdict(int)
return split(0)
|
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()
#@lru_cache(None)
def solve(cur: str) -> int:
ans = 0
if not cur:
return 0
for i in range(1, len(s) + 1):
candidate = cur[:i]
if candidate not in visited:
visited.add(candidate)
ans = max(ans, 1 + solve(cur[i:]))
visited.remove(candidate)
return ans
return solve(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:
visited = set()
def solve(cur: str) -> int:
ans = 0
if not cur:
return 0
for i in range(1, len(s) + 1):
candidate = cur[:i]
if candidate not in visited:
visited.add(candidate)
ans = max(ans, 1 + solve(cur[i:]))
visited.remove(candidate)
return ans
return solve(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.
|
#Bit Mask: use bit 1 as fence. chars between two bit 1's form a substring
#Possible choices: 2 ** N (0 ~ 2 **N - 1)
#Note: that last bit/fence does not matter so we can save computations by half
class Solution:
def maxUniqueSplit(self, s: str) -> int:
N, res = len(s), 0
for i in range(2 ** (N - 1)):
start, seen = 0, set()
for j in range(N):
#test to see if j-th bit is 1
if (i + (1 << N - 1)) & (1 << j):
seen.add(s[start : j + 1])
start = j + 1
res = max(res, len(seen))
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:
N = len(s) - 1
res = 1
for m in range(1 << N):
I = [i for i in range(N) if (m >> i) & 1] + [N]
K = len(I)
ss = {s[:I[0]+1]}
for k in range(K - 1):
ss.add(s[I[k]+1: I[k+1]+1])
# print(ss)
if len(ss) == K:
res = max(res, K)
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 all_partitions(string):
for cutpoints in range(1 << (len(string)-1)):
result = []
lastcut = 0
for i in range(len(string)-1):
if (1<<i) & cutpoints != 0:
result.append(string[lastcut:(i+1)])
lastcut = i+1
result.append(string[lastcut:])
yield result
# d = {}
ps = all_partitions(s)
# for p in ps:
# print(p)
ret = 0
for p in ps:
l = list(dict.fromkeys(p))
if (len(p) == len(l)):
ret = max(ret, len(p))
# print(\"OK\")
# print(p)
# print(l)
# print()
#else:
# print(\"NG\")
# print(p)
# print(l)
# print()
return ret
# sps = []
# for p in ps:
# sps.append(sorted(p))
# for p in sps:
# print(p)
# for p in ps:
# d[p.sort()] = 0
# for p in ps:
# d[p.sort()] += 1
# ret = 0
# for p in ps:
# if d[p.sort()] == 1:
# ret += 1
# 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:
def dfs(s, n):
if n < 0:
return [[s]]
else:
res = []
for i in range(1, len(s) - n):
for group in dfs(s[i:], n - 1):
res.append([s[:i]] + group)
return res
res = 1
for i in range(len(s)):
for group in dfs(s, i):
res = max(res, len(set(group)))
return res
|
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 U in range(1 << (n - 1)):
word = []
now = S[0]
for j in range(n - 1):
if U >> j & 1:
word.append(now)
now = S[j + 1]
else:
now += S[j + 1]
word.append(now)
if len(word) == len(set(word)):
ans = max(ans, len(word))
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:
N = len(s)
res = 0
for mask in itertools.product('10', repeat=N-1):
mask = list(mask) + ['1']
substr = set()
begin = 0
i = 0
valid = True
# print('mask', mask)
while True:
try:
idx = mask.index('1', begin)
# print('idx', idx, i, s[i:idx+1])
begin = idx+1
if s[i:idx+1] in substr:
valid = False
break
substr.add(s[i:idx+1])
i = idx+1
except ValueError:
break
if valid:
res = max(res, mask.count('1'))
# print('substr', substr, valid)
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.
|
def get_sequences(symbols, length):
if length == 0:
return [[]]
sequences = []
tail_sequences = get_sequences(symbols, length-1)
for tail_sequence in tail_sequences:
for symbol in symbols:
sequences.append(list(tail_sequence)+[symbol])
return sequences
def is_unique(sequence, s):
if s=='':
return 0
words = set()
letters = [s[0]]
for i in range(len(sequence)):
if sequence[i] == 1:
word = ''.join(letters)
if word in words:
return False
words.add(word)
letters = [s[i+1]]
else:
letters.append(s[i+1])
word = ''.join(letters)
if word in words:
return False
return True
class Solution:
def maxUniqueSplit(self, s: str) -> int:
sequences = get_sequences([0,1], len(s)-1)
max_split = 1
for sequence in sequences:
if is_unique(sequence, s):
max_split = max(max_split, sum(sequence)+1)
return max_split
|
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters.
|
class Solution:
def maxUniqueSplit(self, s: str) -> int:
if len(s) == 1:
return 1
if len(s) == 2:
if s[0] == s[1]:
return 1
return 2
ans = 0
for i in range(2 ** (len(s) - 1)):
ss = format(i, '0' + str(len(s) - 1) + 'b') + '1'
#print(ss)
st = set()
count = 0
prev = 0
for j in range(len(ss)):
lth = len(st)
if ss[j] == '1':
st.add(s[prev:j + 1])
if len(st) == lth:
break
prev = j + 1
count += 1
#print(st)
ans = max(ans, count)
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:
ls=len(s)
sl=ls-1
ps=1<<(sl)
ans=0
for i in range(ps):
bi=bin(i)[2:].zfill(sl) #0 is a stick
si=[0]+[j+1 for j in range(sl) if bi[j]=='0']+[sl+1]
lsi=len(si)
ds=set([s[si[j]:si[j+1]] for j in range(lsi-1)])
if len(ds)==lsi-1:
ans=max(ans,lsi-1)
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:
options = []
options = self.recursiveSplit(s)
bestcount = 0
for option in options:
count = 0
encountered = {}
valid = True
for substr in option:
if substr not in encountered:
encountered[substr] = 1
count += 1
else:
valid = False
break
if valid and count > bestcount:
bestcount = count
return bestcount
def recursiveSplit(self, s:str) -> str:
if len(s) == 1:
return [[s]]
elif len(s) == 0:
return [[]]
options = []
for i in range(0, len(s)):
recursiveOptions = self.recursiveSplit(s[:i])
for option in recursiveOptions:
newoption = option.copy()
newoption.append(s[i:])
options.append(newoption)
return options
|
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:
c=collections.Counter(s)
#print(c,c.keys(),len(c.keys()))
res=len(c.keys())
l=len(s)
ways=int(math.pow(2,l-1))
for i in range(ways):
bit=str(bin(i))[2:].zfill(l-1)
#print(bit)
cand=[s[0]]
for i,x in enumerate(bit):
if x=='0':
cand[-1]+=s[i+1]
else:
cand.append(s[i+1])
#cand[-1]+=s[-1]
#print(cand)
nodup=set(cand)
if len(nodup)==len(cand):
res=max(res,len(cand))
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 collections import Counter
class Solution:
def maxUniqueSplit(self, s: str) -> int:
n = len(s)
ans = 0
def check(C):
for char in list(C.keys()):
if C[char] > 1:
return False
return True
curr = []
def dfs(i):
nonlocal ans
if i == n:
C = Counter(curr)
if (check(C)):
ans = max(ans, len(list(C.keys())))
return
for j in range(i, n):
curr.append(s[i:j+1])
dfs(j+1)
curr.pop()
dfs(0)
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:
seen = set()
def helper(start):
nonlocal seen, s
length = len(s)
if start == length:
return 0
max_split, cur_split = 0, 0
for split in range(start+1, length):
left, right = s[start:split], s[split:length]
if left in seen or right in seen or left == right:
continue
seen.add(left)
cur_split = 1 + helper(split)
seen.remove(left)
max_split = max(max_split, cur_split)
return max_split
return 1 + helper(0)
|
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.
|
#Brute Force
class Solution:
def maxUniqueSplit(self, s: str) -> int:
N = len(s)
res = 0
def findSplit(num):
bin_str = (bin(num)[2:] + '0').zfill(N)
words = []
word = []
for c, binary in zip(s, bin_str):
word.append(c)
if binary == '1':
words.append(''.join(word))
word = []
if word: words.append(''.join(word))
#print(num, words)
return len(set(words))
ways = 2 ** (N - 1)
for i in range(ways):
res = max(res, findSplit(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:
res = 1
upper = 2 ** (len(s) - 1) # so many possible breaks
for i in range(upper):
words = []
prev = 0
for pos in range(len(s) - 1):
t = pos + 1
if 1 << pos & i:
words.append(s[prev:t])
prev = t
words.append(s[prev:])
if len(words) == len(set(words)):
#print(words)
res = max(res, len(words))
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 search(s):
ans = [[s]]
for i in range(1, len(s)):
ans += [[s[:i]] + comb for comb in search(s[i:])]
return ans
a = search(s)
r = 1
for comb in a:
# print(comb)
if len(set(comb)) == len(comb):
r = max(r, len(comb))
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:
r=0
m=len(s)
for i in range(2**(m-1)):
#b='{0:07b}'.format(i)
bb='{0:0'+str(m-1)+'b}'
b=bb.format(i)
#print(b)
st=0
l=[]
for k in range(len(b)):
if b[k]=='1':
#print(st,k+1)
if k+1>st:
l.append(s[st:k+1])
st=k+1
if st<len(b)+1:
l.append(s[st:len(b)+1])
#print(l)
if len(l)==len(set(l)):
r=max(r,len(l))
print(r)
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:
best = -1
ans = []
for i in range(2**(len(s)-1)):
x = bin(i)
x = x[2:]
x = (len(s) - len(x) - 1) * '0' + x
tmp = ''
a = []
c = 0
for char in s:
tmp += char
if c<len(x):
if x[c]=='1':
a.append(tmp)
tmp = ''
c+=1
a.append(tmp)
if len(a)==len(set(a)):
if len(a)>best:
best = len(a)
ans = a
return len(ans)
|
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:
#print(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]
curEl[0].add(curEl[1][:i+1])
dfs.append([curEl[0].copy(), curEl[1][i+1:]])
#print([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.
|
class Solution:
def maxUniqueSplit(self, s: str) -> int:
# dfs O(2^n) / O(n)
def dfs(index, visited):
nonlocal ans
# if we have not seen the rest, then our answer is at least 1 larger
if s[index:n] and s[index:n] not in visited:
ans = max(ans, len(visited) + 1)
#try each section of the remaining
for i in range(index, n):
# only if it doesn't exist yet, and it is not impossible to beat current max
if s[index:i] and s[index:i] not in visited and len(visited) + 1 + n - i > ans: # prune
visited.add(s[index:i])
dfs(i, visited)
visited.remove(s[index:i])
n, ans = len(s), 0
dfs(0, 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:
n=len(s)
if n==1:
return 1
res=1
seen=set()
for m in range(1,1<<(n-1)): #尝试所有切割位置
if bin(m).count('1')<res:
continue
valid=True
p=0
for i in range(1,n+1):
if valid:
if m & (1 << (i - 1)) or i == n:
if s[p:i] in seen:
valid=False
seen.add(s[p:i])
p=i
if valid:
res=max(res,len(seen))
seen=set()
return res
|
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:
#1593
def maxUniqueSplit(self, s: str) -> int:
def helper(s1_set, s2):
N = len(s2)
if N == 1:
if s2 in s1_set:
return 0
return len(s1_set)+1
res = 0 if s2 in s1_set else len(s1_set)+1
for i in range(1, N):
if s2[:i] in s1_set:
continue
s1_set.add(s2[:i])
res = max(res, helper(s1_set,s2[i:]))
s1_set.discard(s2[:i])
return res
res = helper(set(), s)
return res
|
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters.
|
class Solution:
def maxUniqueSplit(self, s: str) -> int:
def helper(s, strings=set()):
leng_s = len(s)
leng_set = len(strings)
if leng_s == 1 and s in strings:
return 0
else:
ans = leng_set + 1
for i in range(1, leng_s):
substr = s[:i]
if substr not in strings:
ans = max(ans, helper(s[i:], strings|{substr}))
return ans
return helper(s)
# if len(s) == 1 and s in soFar:
# return 0
# maxSplit = len(soFar)+1
# for partition in range(1, len(s)):
# a = s[:partition]
# b = s[partition:]
# if a not in soFar:
# maxSplit = max(maxSplit, maxU(b, soFar|{a}))
# return maxSplit
# return maxU(s)
|
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
def _helper(s: str, visited) -> int:
# s: remaining string to be split
# count: number of unique pieces already
# visited: set of string that already be used
# Returns: the maximum number of unique
# string pieces we can get
# Iterate the first split point.
n = len(s)
result = 0
for i in range(n - 1):
# Cut after i-th char.
cut, remain = s[:(i + 1)], s[(i + 1):]
if cut in visited:
continue
visited.add(cut)
result = max(result, 1 + _helper(remain, visited))
visited.remove(cut)
# Another option is no cutting
if not s in visited:
result = max(result, 1)
return result
class Solution:
def maxUniqueSplit(self, s: str) -> int:
# DFS/Backtracking,
# Consider caching to reduce the change of TLE
if not s:
return 0
visited = set()
return _helper(s, visited)
|
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters.
|
class Solution:
def maxUniqueSplit(self, s: str) -> int:
def maxU(s, soFar=set()):
if len(s) == 1:
if s in soFar: return 0
maxSplit = len(soFar)+1
for partition in range(1, len(s)):
a = s[:partition]
b = s[partition:]
if a not in soFar:
maxSplit = max(maxSplit, maxU(b, soFar|{a}))
return maxSplit
return maxU(s)
|
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters.
|
class Solution:
def maxUniqueSplit(self, string: str) -> int:
def split(s, splits):
maxs = 1 if s not in splits else 0
for i in range(1, len(s)):
if s[:i] in splits:
continue
rst = 1 + split(s[i:], splits+[s[:i]])
maxs = max(rst, maxs)
return maxs
return split(string, [])
|
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.N=len(s)
wordset=set(s)
if len(wordset)>=self.N-1:
return len(wordset)
self.cur=0
def dfs(i,visit,num):
if self.N-i+num<=self.cur:
return
if s[i:] not in visit:
self.cur=max(self.cur,num+1)
for j in range(i+1,self.N):
new=s[i:j]
if new not in visit:
visit.add(new)
dfs(j,visit,num+1)
visit.remove(new)
visit=set()
dfs(0,visit,0)
return self.cur
|
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters.
|
class Solution:
def maxUniqueSplit(self, s: str) -> int:
def check(st, cm):
mx = 0 if st in cm else 1
for i in range(1,len(st)):
a = st[:i]
b = st[i:]
if a not in cm:
ncm = set(cm)
ncm.add(a)
c = check(b, ncm) + 1
if c > mx:
mx = c
return mx
return check(s, set())
|
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 explore(seen, s):
for i in range(1, len(s)):
word, other = s[:i], s[i:]
if word in seen: continue
explore(seen + [word], other)
else:
if s in seen:
self.res = max(len(seen), self.res)
else:
self.res = max(len(seen) + 1, self.res)
self.res = 0
explore([], 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:
self.mx = 0
self.lim = len(s)
def r(i,l,c):
if i==self.lim-1:
if s[i] not in l:
if c+1>self.mx:self.mx = c+1
else:
if c>self.mx:self.mx = c
else:
for k in range(i+1,self.lim+1):
if k==self.lim:
if s[i:k] not in l:
if c+1>self.mx:self.mx = c+1
else:
if s[i:k] not in l:
r(k,l|{s[i:k]},c+1)
r(0,set(),0)
return self.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:
def dfs(i):
nonlocal used, ret
if i == n:
ret = max(ret, len(used))
return
for j in range(i + 1, n + 1):
w = s[i: j]
if w in used:
continue
used.add(w)
dfs(j)
used.discard(w)
n = len(s)
used = set()
ret = 0
dfs(0)
return ret
|
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters.
|
class Solution:
def maxUniqueSplit(self, s: str) -> int:
@lru_cache(None)
def helper(s):
if not s:
return [[]]
splits = []
for i in range(1, len(s) + 1):
word = s[:i]
next_splits = helper(s[i:])
for split in next_splits:
if word not in split:
splits.append(split + [word])
return splits
ret = helper(s)
max_len = max([len(x) for x in ret])
return max_len
|
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters.
|
class Solution:
def maxUniqueSplit(self, s: str) -> int:
self.ans = 0
n = len(s)
def backtrack(start, have):
if start>=n:
self.ans=max(self.ans, len(have))
return
for i in range(start+1, n+1):
tmp = s[start:i]
if tmp in have: continue
else:
have.add(tmp)
backtrack(i, have)
have.remove(tmp)
backtrack(0, set())
return self.ans
|
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(set(s)) == 1: return 1
return self.dfs(s, 0, 1, set())
def dfs(self, s, start, end, dic):
if end>=len(s): return 0 if s[start:] in dic else 1
k1 = 0
if s[start:end] not in dic:
dic.add(s[start:end])
k1 = 1+self.dfs(s, end, end+1, dic)
dic.remove(s[start:end])
k2 = self.dfs(s, start, end+1, dic)
return max(k1, k2)
|
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 dfs(start, seen):
ans = 0
if start == n:
return ans
for end in range(start + 1, n + 1):
word = s[start:end]
if word in seen:
continue
seen.add(word)
ans = max(ans, 1 + dfs(end, seen))
seen.discard(word)
return ans
return dfs(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:
seen = set()
return self.helper(s, seen)
def helper(self, s, seen):
ans = 0
if s:
for i in range(1, len(s) + 1):
candidate = s[:i]
if candidate not in seen:
seen.add(candidate)
ans = max(ans, 1 + self.helper(s[i:], seen))
seen.remove(candidate)
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 builder(s, seen):
if not s:
return 0
res = 0
for i, c in enumerate(s):
candidate = s[:i+1]
if candidate not in seen:
seen.add(candidate)
res = max(res, 1 + builder(s[i+1:], seen))
seen.remove(candidate)
return res
return builder(s, set())
|
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()
return self.helper(s, seen)
def helper(self, s, seen):
ans = 0
if s:
for i in range(1, len(s) + 1):
candidate = s[:i]
if candidate not in seen:
seen.add(candidate)
ans = max(ans, 1 + self.helper(s[i:], seen))
seen.remove(candidate)
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:
seen = set()
return self.helper(s, seen)
def helper(self, s, seen):
ans = 0
if s:
for i in range(1, len(s) + 1):
candidate = s[:i]
if candidate not in seen:
seen.add(candidate)
ans = max(ans, 1 + self.helper(s[i:], seen))
seen.remove(candidate)
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:
seen = set()
return self.helper(s, seen)
def helper(self, s, seen):
ans = 0
if s:
for i in range(1, len(s) + 1):
candidate = s[:i]
if candidate not in seen:
seen.add(candidate)
ans = max(ans, 1 + self.helper(s[i:], seen))
seen.remove(candidate)
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:
N = len(s)
def splitLookup(ss, used):
Ns = len(ss)
if ss in used:
maxSs = 0
else:
maxSs = 1
i = 1;
while i < Ns and Ns-i > maxSs - 1:
sub = ss[:i]
if sub not in used:
Nsplit = splitLookup(ss[i:], used | {sub})
if Nsplit + 1 > maxSs:
maxSs = Nsplit + 1
i += 1
return maxSs
return splitLookup(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:
def maxU(s, soFar=set()):
if len(s) == 1 and s in soFar:
return 0
maxSplit = len(soFar)+1
for partition in range(1, len(s)):
a = s[:partition]
b = s[partition:]
print(a, b)
if a not in soFar:
maxSplit = max(maxSplit, maxU(b, soFar|{a}))
return maxSplit
return maxU(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 math
# class Solution(object):
# def maxUniqueSplit(self, s):
# \"\"\"
# :type s: str
# :rtype: int
# \"\"\"
# account = 0
# solutions = set()
# account = self.findSolutions(s, solutions)
# print(\"finished\")
# print(solutions)
# print(account)
# # if substring are the same than the solution is not right
# # if one solution has more substring than another adopt this one
# # could use divide and conqure to go done a string
# def findSolutions(self, s, solutions):
# strLen = len(s)
# if strLen != 1:
# halfPoint = math.ceil(strLen / 2) # use floor so that the left side is always smaller string
# leftSubStr = s[:halfPoint]
# rightSubStr = s[halfPoint:]
# print(\"strlength is: {} mid point : {} left string: {} right string: {} \".format(strLen, halfPoint, leftSubStr, rightSubStr))
# # only keep spliting when the substring is unique in solutions
# if leftSubStr not in solutions:
# # remove previous solution if it is in it
# solutions.add(leftSubStr)
# return self.findSolutions(leftSubStr, solutions) + 1
# solutions.remove(leftSubStr)
# if rightSubStr not in solutions:
# solutions.add(rightSubStr)
# return account + self.findSolutions(rightSubStr, solutions) + 1
# solutions.remove(rightSubStr)
# else:
# print(s)
# return 1
class Solution:
book = set()
def maxUniqueSplit(self, s):
result = 0
for i in range(1, len(s)+1): #go through all the length of sub strings from left to right
curr = s[:i]
if curr not in self.book:
self.book.add(curr) # add a type of sub string if it is not in it
result = max(result, 1 + self.maxUniqueSplit(s[i:]))
self.book.remove(curr)
return 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 maxUniqueSplit(self, s: str) -> int:
self.ans=0
seen=set()
def search(i):
if i==len(s):
self.ans=max(len(seen),self.ans)
return
for j in range(i,len(s)):
word=s[i:j+1]
if word not in seen:
seen.add(word)
search(j+1)
seen.discard(word)
search(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.
|
def aux(s,lst,i):
if(i == len(s)):
return 0
mx = 0
for j in range(i,len(s)):
#print(lst)
if(s[i:j+1] not in lst):
tmp = 1 + aux(s,lst+[s[i:j+1]],j+1)
if tmp>mx:
mx = tmp
return mx
class Solution:
def maxUniqueSplit(self, s: str) -> int:
lst = []
return aux(s,lst,0)
|
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 = 0
def dfs(s, tmp):
if s == '': self.S = max(self.S, len(tmp))
for i in range(1, len(s) + 1):
if s[:i] not in tmp:
dfs(s[i:], tmp + [s[:i]])
dfs(s, [])
return self.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:
return self.helper(s, set())
def helper(self, s, set):
if len(s) == 1 and s in set:
return 0
max_split = len(set) + 1
for i in range(1, len(s)):
a = s[:i]
b = s[i:]
max_split = max(max_split, self.helper(b, set|{a}))
return max_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.
|
class Solution:
def maxUniqueSplit(self, s: str) -> int:
ans, n = 0, len(s)
def dfs(i, cnt, visited):
nonlocal ans, n
if i == n:
ans = max(ans, cnt)
for j in range(i + 1, n + 1):
if s[i:j] in visited:
continue
visited.add(s[i:j])
dfs(j, cnt + 1, visited)
visited.remove(s[i:j])
dfs(0, 0, set()) # function call
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 __init__(self):
self.ans = 0
self.set = set()
def maxUniqueSplit(self, s: str) -> int:
self.solve(s, 0)
return self.ans
def solve(self, s: str, pos: int) -> None:
if pos > len(s):
return
self.ans = max(self.ans, len(self.set))
if self.ans - len(self.set) >= len(s) - pos:
return
for i in range(pos, len(s)):
v = s[pos:i+1]
if v in self.set:
continue
self.set.add(v)
# print(self.set)
self.solve(s, i + 1)
self.set.remove(v)
|
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, n = 0, len(s)
def dfs(i, cnt, visited):
nonlocal ans, n
if i == n: ans = max(ans, cnt) # stop condition
for j in range(i+1, n+1):
if s[i:j] in visited: continue # avoid re-visit/duplicates
visited.add(s[i:j]) # update visited set
dfs(j, cnt+1, visited) # backtracking
visited.remove(s[i:j]) # recover visited set for next possibility
dfs(0, 0, set()) # function call
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:
maximum=0
seen=set()
def dfs(idx,seen):
if idx==len(s):
nonlocal maximum
maximum=max(len(seen),maximum)
return
for i in range(idx,len(s)):
if s[idx:i+1] not in seen:
seen.add(s[idx:i+1])
dfs(i+1,seen)
seen.remove(s[idx:i+1])
dfs(0,seen)
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:
ret = 0
def backtrack(start, currSet):
if start == len(s):
nonlocal ret
ret = max(ret, len(currSet))
return
for i in range(start, len(s)):
if s[start:i+1] not in currSet:
currSet.add(s[start:i+1])
backtrack(i+1, currSet)
currSet.remove(s[start:i+1])
backtrack(0, set())
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:
def numway(s):
#if not s: return 0
nw=0
for i in range(1,len(s)+1):
if s[:i] not in nq:
nq.add(s[:i])
nw=max(nw,1+numway(s[i:]))
nq.remove(s[:i])
return nw
nq=set()
return numway(s)
|
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters.
|
class Solution:
def maxUniqueSplit(self, s: str) -> int:
def helper(curr_set, s):
if len(s) == 0:
return len(s)
max_split = 0
for i in range(len(s)):
if s[:i+1] not in curr_set:
# curr_set2 = set(curr_set)
curr_set.add(s[:i+1])
max_split = max(max_split, 1 + helper(curr_set, s[i+1:]))
curr_set.remove(s[:i+1])
return max_split
return helper(set(), s)
# curr_set = set()
# i,j = 0,1
# while j<=len(s):
# if s[i:j] not in curr_set:
# curr_set.add(s[i:j])
# print(s[i:j])
# i, j = j, j+1
# else:
# j += 1
# return len(curr_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:
return get_max(s, set())
def get_max(s, set):
maxi=0
for i in range(1,len(s)+1):
if s[:i] not in set:
set.add(s[:i])
# print(set)
maxi=max(maxi, 1+get_max(s[i:],set))
set.remove(s[:i])
return maxi
|
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 func(string: str, visited: set):
if string in visited or len(string) == 0:
return 0
res = 1
for i in range(len(string)):
s = string[:i + 1]
if s in visited:
continue
visited.add(s)
temp = func(string[i + 1:], visited) + 1
visited.remove(s)
res = max(res, temp)
return res
return func(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:
d = set()
res = 1
n = len(s)
def solve(p, ans):
nonlocal res
if p == n:
res = max(res, ans)
for i in range(p, n):
ss = ''.join(s[p:i+1])
if ss in d: continue
d.add(ss)
solve(i + 1, ans + 1)
d.remove(ss)
solve(0, 0)
return res
|
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 backtracking(ind):
nonlocal ans
if ind==len(s):
ans = max(ans,len(split))
for i in range(ind,len(s)):
if s[ind:i+1] not in split:
split.add(s[ind:i+1])
backtracking(i+1)
split.remove(s[ind:i+1])
ans = 0
split = set()
backtracking(0)
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:
n = len(s)
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:
cur.append(cand)
dp(i+k, cur)
cur.pop()
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.result = []
n = len(s)
self.backtrack(s, 0, n, [])
return len(max(self.result, key=len))
def backtrack(self, s, left, n, currArr):
if left >= n:
self.result.append(currArr)
for i in range(left, n):
if s[left:i+1] in currArr:
continue
newArr = currArr.copy()
newArr.append(s[left:i+1])
self.backtrack(s, i+1, n, newArr)
|
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters.
|
class Solution:
def maxUniqueSplit(self, s: str) -> int:
'''
result=set()
i=0
j=1
while(i<len(s)):
if s[i:j] not in result:
if s[j:len(s)] in result:
result.add(s[i:])
break
elif s[i:j]==s[j:len(s)]:
result.add(s[i:])
break
else:
result.add(s[i:j])
i=j
j=i+1
else:
j+=1
return len(result)
'''
seen=set()
n=len(s)
def dfs(i):
if i==n:
return 0
res=1
for j in range(i+1,n+1):
if s[i:j] not in seen and s[j:] not in seen and s[i:j]!=s[j:]:
seen.add(s[i:j])
res=max(res,dfs(j)+1)
seen.remove(s[i:j])
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:
def helper(s, start, seen):
ans = 0
for end in range(start + 1, len(s) + 1):
added = s[start:end]
print(added)
if added not in seen:
seen.add(added)
ans = max(ans, 1 + helper(s, end, seen))
seen.remove(added)
return ans
seen = set()
return helper(s, 0, seen)
|
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.dfs(s, 0, [])
def dfs(self, s, index, ele):
currMax = 0
for i in range(index, len(s)):
if s[index:i+1] not in ele:
currMax = max(currMax, 1+self.dfs(s, i+1, ele[:] + [s[index:i+1]]))
return currMax
|
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.cnt=0
def getstring(start,visited):
if start==len(s):
self.cnt=max(self.cnt,len(visited))
return
for end in range(start+1,len(s)+1):
temp=s[start:end]
if temp in visited:
continue
visited.add(temp)
getstring(end,visited)
visited.remove(temp)
getstring(0,set())
return self.cnt
|
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
splits = [[] for _ in range(len(s))]
for i, c in enumerate(s):
splits[i].append(set([s[:i + 1]]))
for j in range(1, i + 1):
ss = s[j: i + 1]
for spl in splits[j - 1]:
if ss not in spl:
splits[i].append(spl | set([ss]))
return max([len(spl) for spl in splits[-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.
|
from copy import deepcopy
class Solution:
def __init__(self):
self.strings = []
def maxUniqueSplit(self, s):
n = len(s)
result = []
lookUp = set()
self.dfs(s, lookUp, result)
return len(self.strings)
def dfs(self, s, lookUp, result):
if len(s) == 0:
if len(result) > len(self.strings):
self.strings[:] = result
return
for i in range(0, len(s)):
if s[0:i+1] not in lookUp:
lookUp.add(s[0:i+1])
result.append(s[0:i+1])
self.dfs(s[i+1:], lookUp, result)
lookUp.remove(s[0:i+1])
result.pop()
|
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 backtracking(ind):
nonlocal ans
if ind==len(s):
ans = max(ans,len(split))
for i in range(ind,len(s)):
if s[ind:i+1] not in split:
split.append(s[ind:i+1])
backtracking(i+1)
split.remove(s[ind:i+1])
ans = 0
split = []
backtracking(0)
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.ans = 0
seen = set()
def dp(i):
if i == len(s):
self.ans = max(self.ans, len(seen))
for j in range(i, len(s)):
word = s[i:j+1]
if word not in seen:
seen.add(word)
dp(j+1)
seen.discard(word)
dp(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:
def dfs(i, vis):
nonlocal ans
if len(vis) + n-i < ans: return
if i >= n: ans = max(ans, len(vis))
l = n-i
for k in range(1, l+1):
x = s[i:i+k]
if x not in vis:
vis.add(x)
dfs(i+k, vis)
vis.discard(x)
ans, n, vis = 0, len(s), set()
dfs(0, vis)
return ans
|
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.strings = []
def maxUniqueSplit(self, s):
n = len(s)
result = []
lookUp = set()
self.dfs(s, lookUp, result)
return len(self.strings)
def dfs(self, s, lookUp, result):
if len(s) == 0:
if len(result) > len(self.strings):
self.strings[:] = result
return
for i in range(0, len(s)):
if s[0:i+1] not in lookUp:
lookUp.add(s[0:i+1])
result.append(s[0:i+1])
self.dfs(s[i+1:], lookUp, result)
lookUp.remove(s[0:i+1])
result.pop()
|
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters.
|
class Solution:
def maxUniqueSplit(self, s: str) -> int:
self.ans = 0
def dfs(node,seen):
if node == len(s):
self.ans = max(self.ans,len(seen))
for i in range(node,len(s)):
if s[node:i+1] not in seen:
dfs(i+1,seen|{s[node:i+1]})
dfs(0,set())
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:
def backtrack(m, s):
if len(s) == 0:
return 0
ans = 0
for i in range(1, len(s) + 1):
candidate = s[:i]
if candidate not in m:
m.add(candidate)
ans = max(ans, 1 + backtrack(m, s[i:]))
m.remove(candidate)
return ans
m = set()
return backtrack(m, s)
|
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.max = 0
self.backtrack(s,set(),0)
return self.max
def backtrack(self,s,store,l):
if l >=len(s):
self.max = max(self.max,len(store))
for i in range(l,len(s)+1):
if s[l:i] not in store and l!=i:
store.add(s[l:i])
self.backtrack(s,store,i)
store.remove(s[l: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:
return self.dp(s,0,0,[])
def dp(self,s,start,pos,ans):
if pos == len(s):
return len(ans)
a = 0
b = self.dp(s,start,pos+1,list(ans))
if s[start:pos+1] not in ans:
ans.append(s[start:pos+1])
a = self.dp(s,pos+1,pos+1,list(ans))
# ans.pop()
# ans.remove(s[start:pos+1])
return a if a > b else b
# 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:
n = len(s)
self.res = 0
def helper(curIdx, curW, curSets):
if (curIdx == n):
self.res = max(self.res, len(curSets))
return
#don't split
helper(curIdx+1, curW+s[curIdx], curSets)
#split
if (curW+s[curIdx] not in curSets):
helper(curIdx+1, '', curSets | {(curW+s[curIdx])})
helper(0, '', 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:
return self.dp(s,0,0,[])
def dp(self,s,start,pos,ans):
if pos == len(s):
return len(ans)
a = 0
b = self.dp(s,start,pos+1,list(ans))
if s[start:pos+1] not in ans:
ans.append(s[start:pos+1])
a = self.dp(s,pos+1,pos+1,list(ans))
ans.pop()
# ans.remove(s[start:pos+1])
return a if a > b else b
# return ans
|
Given an array of unique integers, each integer is strictly greater than 1.
We make a binary tree using these integers and each number may be used for any number of times.
Each non-leaf node's value should be equal to the product of the values of it's children.
How many binary trees can we make? Return the answer modulo 10 ** 9 + 7.
Example 1:
Input: A = [2, 4]
Output: 3
Explanation: We can make these trees: [2], [4], [4, 2, 2]
Example 2:
Input: A = [2, 4, 5, 10]
Output: 7
Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].
Note:
1 <= A.length <= 1000.
2 <= A[i] <= 10 ^ 9.
|
class Solution:
def numFactoredBinaryTrees(self, A: List[int]) -> int:
mod = 10**9 + 7
nums_set = set(A)
nums = A.copy()
nums.sort()
counts = {}
total = 0
for n in nums:
n_count = 1
for d in nums:
if d * d > n:
break
if n % d != 0:
continue
e = n // d
if e not in nums_set:
continue
subtrees = (counts[d] * counts[e]) % mod
if d != e:
subtrees = (subtrees * 2) % mod
n_count = (n_count + subtrees) % mod
counts[n] = n_count % mod
total = (total + n_count) % mod
return total
|
Given an array of unique integers, each integer is strictly greater than 1.
We make a binary tree using these integers and each number may be used for any number of times.
Each non-leaf node's value should be equal to the product of the values of it's children.
How many binary trees can we make? Return the answer modulo 10 ** 9 + 7.
Example 1:
Input: A = [2, 4]
Output: 3
Explanation: We can make these trees: [2], [4], [4, 2, 2]
Example 2:
Input: A = [2, 4, 5, 10]
Output: 7
Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].
Note:
1 <= A.length <= 1000.
2 <= A[i] <= 10 ^ 9.
|
class Solution:
def numFactoredBinaryTrees(self, A: List[int]) -> int:
factorSet = set(A)
A.sort()
treeCount = collections.defaultdict(lambda : 1)
n = len(A)
end = 0
for num in A:
sqrt = int(math.sqrt(num)+1)
while end < n and A[end] <= sqrt:
end += 1
count = 0
for _, p in zip(list(range(end)), A):
if num % p == 0 and (q := num // p) in factorSet and p <= q:
count += 2*treeCount[p]*treeCount[q] if p != q else treeCount[p]*treeCount[q]
treeCount[num] += count
return sum(treeCount.values()) % (1000_000_000 + 7)
|
Given an array of unique integers, each integer is strictly greater than 1.
We make a binary tree using these integers and each number may be used for any number of times.
Each non-leaf node's value should be equal to the product of the values of it's children.
How many binary trees can we make? Return the answer modulo 10 ** 9 + 7.
Example 1:
Input: A = [2, 4]
Output: 3
Explanation: We can make these trees: [2], [4], [4, 2, 2]
Example 2:
Input: A = [2, 4, 5, 10]
Output: 7
Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].
Note:
1 <= A.length <= 1000.
2 <= A[i] <= 10 ^ 9.
|
class Solution:
def numFactoredBinaryTrees(self, A: List[int]) -> int:
factorSet = set(A)
A.sort()
treeCount = {}
for num in A:
count = 1
for p, pCount in list(treeCount.items()):
q, rem = divmod(num, p)
if p > q:
break
if rem == 0:
tmp = pCount * treeCount.get(q, 0)
count += tmp if p == q else 2*tmp
treeCount[num] = count
return sum(treeCount.values()) % (1000_000_000 + 7)
|
Given an array of unique integers, each integer is strictly greater than 1.
We make a binary tree using these integers and each number may be used for any number of times.
Each non-leaf node's value should be equal to the product of the values of it's children.
How many binary trees can we make? Return the answer modulo 10 ** 9 + 7.
Example 1:
Input: A = [2, 4]
Output: 3
Explanation: We can make these trees: [2], [4], [4, 2, 2]
Example 2:
Input: A = [2, 4, 5, 10]
Output: 7
Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].
Note:
1 <= A.length <= 1000.
2 <= A[i] <= 10 ^ 9.
|
class Solution:
def numFactoredBinaryTrees(self, A: List[int]) -> int:
A.sort()
dp = {}
res = 0
for i in range(len(A)):
dp[A[i]] = 1
for j in range(i):
if A[j] * A[j] == A[i]:
dp[A[i]] += dp[A[j]] * dp[A[j]]
dp[A[i]] %= 1000_000_007
elif A[j] * A[j] > A[i] and A[i] % A[j] == 0:
dp[A[i]] += dp[A[j]] * dp.get(A[i] // A[j], 0) * 2
dp[A[i]] %= 1000_000_007
res = sum([v for _, v in dp.items()])
res %= 1000_000_007
return res
|
Given an array of unique integers, each integer is strictly greater than 1.
We make a binary tree using these integers and each number may be used for any number of times.
Each non-leaf node's value should be equal to the product of the values of it's children.
How many binary trees can we make? Return the answer modulo 10 ** 9 + 7.
Example 1:
Input: A = [2, 4]
Output: 3
Explanation: We can make these trees: [2], [4], [4, 2, 2]
Example 2:
Input: A = [2, 4, 5, 10]
Output: 7
Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].
Note:
1 <= A.length <= 1000.
2 <= A[i] <= 10 ^ 9.
|
class Solution:
def numFactoredBinaryTrees(self, A: List[int]) -> int:
A.sort()
dp = {}
res = 0
for i in range(len(A)):
dp[A[i]] = 1
for j in range(i):
if A[j] * A[j] == A[i]:
dp[A[i]] += dp[A[j]] * dp[A[j]]
dp[A[i]] %= 1000_000_007
elif A[j] * A[j] > A[i]:
div = A[i] // A[j]
if div * A[j] == A[i] and div in dp:
dp[A[i]] += dp[A[j]] * dp[div] * 2
dp[A[i]] %= 1000_000_007
res += dp[A[i]]
res %= 1000_000_007
return res
|
Given an array of unique integers, each integer is strictly greater than 1.
We make a binary tree using these integers and each number may be used for any number of times.
Each non-leaf node's value should be equal to the product of the values of it's children.
How many binary trees can we make? Return the answer modulo 10 ** 9 + 7.
Example 1:
Input: A = [2, 4]
Output: 3
Explanation: We can make these trees: [2], [4], [4, 2, 2]
Example 2:
Input: A = [2, 4, 5, 10]
Output: 7
Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].
Note:
1 <= A.length <= 1000.
2 <= A[i] <= 10 ^ 9.
|
class Solution:
def numFactoredBinaryTrees(self, A: List[int]) -> int:
A.sort()
dp = {}
res = 0
for i in range(len(A)):
dp[A[i]] = 1
for j in range(i):
if A[j] * A[j] == A[i]:
dp[A[i]] += dp[A[j]] * dp[A[j]]
dp[A[i]] %= 1000_000_007
elif A[j] * A[j] > A[i] and A[i] % A[j] == 0:
dp[A[i]] += dp[A[j]] * dp.get(A[i] // A[j], 0) * 2
dp[A[i]] %= 1000_000_007
res += dp[A[i]]
res %= 1000_000_007
return res
|
Given an array of unique integers, each integer is strictly greater than 1.
We make a binary tree using these integers and each number may be used for any number of times.
Each non-leaf node's value should be equal to the product of the values of it's children.
How many binary trees can we make? Return the answer modulo 10 ** 9 + 7.
Example 1:
Input: A = [2, 4]
Output: 3
Explanation: We can make these trees: [2], [4], [4, 2, 2]
Example 2:
Input: A = [2, 4, 5, 10]
Output: 7
Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].
Note:
1 <= A.length <= 1000.
2 <= A[i] <= 10 ^ 9.
|
class Solution:
def numFactoredBinaryTrees(self, A: List[int]) -> int:
cache = dict()
iSet = set(A)
def helper(i):
if i not in cache:
count = 1
for j in range(len(A)):
if i != A[j] and i % A[j] == 0:
if i//A[j] in iSet:
count += helper(A[j]) * helper(i//A[j])
cache[i] = count
return cache[i]
ans = 0
for i in range(len(A)):
ans += helper(A[i])
return ans % 1000000007
|
Given an array of unique integers, each integer is strictly greater than 1.
We make a binary tree using these integers and each number may be used for any number of times.
Each non-leaf node's value should be equal to the product of the values of it's children.
How many binary trees can we make? Return the answer modulo 10 ** 9 + 7.
Example 1:
Input: A = [2, 4]
Output: 3
Explanation: We can make these trees: [2], [4], [4, 2, 2]
Example 2:
Input: A = [2, 4, 5, 10]
Output: 7
Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].
Note:
1 <= A.length <= 1000.
2 <= A[i] <= 10 ^ 9.
|
class Solution:
def numFactoredBinaryTrees(self, A: List[int]) -> int:
A=sorted(A)
res=dict()
for idx, a in enumerate(A):
if idx==0:
res[a]=1
else:
res[a]=1
for j in range(idx):
if (a%A[j]==0) and (a//A[j] in A):
res[a]+=res[A[j]]*res[a//A[j]]
return sum(res.values())%(10**9+7)
|
Given an array of unique integers, each integer is strictly greater than 1.
We make a binary tree using these integers and each number may be used for any number of times.
Each non-leaf node's value should be equal to the product of the values of it's children.
How many binary trees can we make? Return the answer modulo 10 ** 9 + 7.
Example 1:
Input: A = [2, 4]
Output: 3
Explanation: We can make these trees: [2], [4], [4, 2, 2]
Example 2:
Input: A = [2, 4, 5, 10]
Output: 7
Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].
Note:
1 <= A.length <= 1000.
2 <= A[i] <= 10 ^ 9.
|
class Solution:
def numFactoredBinaryTrees(self, A: List[int]) -> int:
A.sort()
dp = [1 for _ in range(len(A))]
for i in range(len(A)):
for j in range(i):
time, remain = divmod(A[i], A[j])
if remain == 0 and time in A:
dp[i] += (dp[j] * dp[A.index(time)])
return sum(dp) % (10**9 + 7)
|
Given an array of unique integers, each integer is strictly greater than 1.
We make a binary tree using these integers and each number may be used for any number of times.
Each non-leaf node's value should be equal to the product of the values of it's children.
How many binary trees can we make? Return the answer modulo 10 ** 9 + 7.
Example 1:
Input: A = [2, 4]
Output: 3
Explanation: We can make these trees: [2], [4], [4, 2, 2]
Example 2:
Input: A = [2, 4, 5, 10]
Output: 7
Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].
Note:
1 <= A.length <= 1000.
2 <= A[i] <= 10 ^ 9.
|
class Solution:
def numFactoredBinaryTrees(self, A: List[int]) -> int:
d = {}
Aset = set(A)
A.sort()
ans = 0
for i in range(len(A)):
d[A[i]] = 1
for j in range(i):
if A[i]%A[j] == 0:
k = A[i]//A[j]
if k in set(A):
d[A[i]] += d[A[j]]*d[k]
d[A[i]] %= 10**9+7
ans += d[A[i]]
ans %= 10**9+7
# print(d)
return ans
# return sum(d[key] for key in d)
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.