description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Doraemon has a matrix with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Let's denote the element in row $i$ and column $j$ by $A_{i,j}$. Next, let's define a sub-row of a row $r$ as a sequence $A_{r, x}, A_{r, x+1}, \ldots, A_{r, y}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ M$) and a sub-column of a column $c$ by $A_{x, c}, A_{x+1, c}, \ldots, A_{y, c}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ N$).
You need to find the number of pairs (sub-row of some row, sub-column of some column) with the following properties:
1. Both sequences (the sub-row and the sub-column) have the same length.
2. This length is odd.
3. The central elements of both sequences are the same (they correspond to the same element of the matrix).
4. Both sequences are palindromes.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains $M$ space-separated integers $A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}$.
------ Output ------
For each test case, print a single line containing one integer ― the number of valid pairs.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N, M$
$N \cdot M ≤ 10^{5}$
$1 ≤ A_{i, j} ≤ 10^{6}$ for each valid $i, j$
the sum of $N \cdot M$ over all test cases does not exceed $3 \cdot 10^{5}$
------ Subtasks ------
Subtask #1 (30 points):
$T ≤ 10$
$N \cdot M ≤ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
3 3
2 1 2
1 1 1
2 1 2
----- Sample Output 1 ------
10
----- explanation 1 ------
Example case 1: Each element forms a valid pair (where the sub-row and sub-column contain only this element) and the middle row and middle column also form one valid pair. | def is_palindrome(i, j, mat, m, n):
count = 0
t = 0
while True:
try:
if (i - t < 0 or j - t < 0) or i + t > m or j + t > n:
return count
if mat[i - t][j] == mat[i + t][j] and mat[i][j - t] == mat[i][j + t]:
count += 1
t += 1
else:
return count
except:
return count
t = int(input())
for _ in range(t):
M, N = map(int, input().split())
mat = []
for i in range(M):
mat.append(input().split())
count = 0
i = 0
while i < M:
j = 0
while j < N:
count += is_palindrome(i, j, mat, M - 1, N - 1)
j += 1
i += 1
print(count) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR IF VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Doraemon has a matrix with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Let's denote the element in row $i$ and column $j$ by $A_{i,j}$. Next, let's define a sub-row of a row $r$ as a sequence $A_{r, x}, A_{r, x+1}, \ldots, A_{r, y}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ M$) and a sub-column of a column $c$ by $A_{x, c}, A_{x+1, c}, \ldots, A_{y, c}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ N$).
You need to find the number of pairs (sub-row of some row, sub-column of some column) with the following properties:
1. Both sequences (the sub-row and the sub-column) have the same length.
2. This length is odd.
3. The central elements of both sequences are the same (they correspond to the same element of the matrix).
4. Both sequences are palindromes.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains $M$ space-separated integers $A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}$.
------ Output ------
For each test case, print a single line containing one integer ― the number of valid pairs.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N, M$
$N \cdot M ≤ 10^{5}$
$1 ≤ A_{i, j} ≤ 10^{6}$ for each valid $i, j$
the sum of $N \cdot M$ over all test cases does not exceed $3 \cdot 10^{5}$
------ Subtasks ------
Subtask #1 (30 points):
$T ≤ 10$
$N \cdot M ≤ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
3 3
2 1 2
1 1 1
2 1 2
----- Sample Output 1 ------
10
----- explanation 1 ------
Example case 1: Each element forms a valid pair (where the sub-row and sub-column contain only this element) and the middle row and middle column also form one valid pair. | def main():
for _ in range(int(input())):
n, m = map(int, input().split())
a = [0] * n
for i in range(n):
a[i] = list(map(int, input().split()))
ans = 0
for i in range(n):
if i == 0 or i == n - 1:
ans += m
else:
for j in range(m):
if j == 0 or j == m - 1:
ans += 1
else:
l, r, u, d = j, j, i, i
ans += 1
l -= 1
r += 1
u -= 1
d += 1
while l >= 0 and r < m and u >= 0 and d < n:
if a[i][l] == a[i][r] and a[u][j] == a[d][j]:
ans += 1
l -= 1
r += 1
u -= 1
d += 1
else:
break
print(ans)
main() | FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Doraemon has a matrix with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Let's denote the element in row $i$ and column $j$ by $A_{i,j}$. Next, let's define a sub-row of a row $r$ as a sequence $A_{r, x}, A_{r, x+1}, \ldots, A_{r, y}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ M$) and a sub-column of a column $c$ by $A_{x, c}, A_{x+1, c}, \ldots, A_{y, c}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ N$).
You need to find the number of pairs (sub-row of some row, sub-column of some column) with the following properties:
1. Both sequences (the sub-row and the sub-column) have the same length.
2. This length is odd.
3. The central elements of both sequences are the same (they correspond to the same element of the matrix).
4. Both sequences are palindromes.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains $M$ space-separated integers $A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}$.
------ Output ------
For each test case, print a single line containing one integer ― the number of valid pairs.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N, M$
$N \cdot M ≤ 10^{5}$
$1 ≤ A_{i, j} ≤ 10^{6}$ for each valid $i, j$
the sum of $N \cdot M$ over all test cases does not exceed $3 \cdot 10^{5}$
------ Subtasks ------
Subtask #1 (30 points):
$T ≤ 10$
$N \cdot M ≤ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
3 3
2 1 2
1 1 1
2 1 2
----- Sample Output 1 ------
10
----- explanation 1 ------
Example case 1: Each element forms a valid pair (where the sub-row and sub-column contain only this element) and the middle row and middle column also form one valid pair. | def f(n, m, i, j, l):
k = min(i, n - i - 1, j, m - j - 1)
v = 1
ans = 0
while v != k + 1:
if l[i - v][j] == l[i + v][j] and l[i][j - v] == l[i][j + v]:
ans = ans + 1
else:
break
v = v + 1
return ans
t = int(input())
for x in range(t):
n, m = map(int, input().split())
l = [[]] * n
fans = m * n
for y in range(n):
l[y] = list(map(int, input().split()))
for i in range(1, n - 1):
for j in range(1, m - 1):
fans = fans + f(n, m, i, j, l)
print(fans) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST LIST VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Doraemon has a matrix with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Let's denote the element in row $i$ and column $j$ by $A_{i,j}$. Next, let's define a sub-row of a row $r$ as a sequence $A_{r, x}, A_{r, x+1}, \ldots, A_{r, y}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ M$) and a sub-column of a column $c$ by $A_{x, c}, A_{x+1, c}, \ldots, A_{y, c}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ N$).
You need to find the number of pairs (sub-row of some row, sub-column of some column) with the following properties:
1. Both sequences (the sub-row and the sub-column) have the same length.
2. This length is odd.
3. The central elements of both sequences are the same (they correspond to the same element of the matrix).
4. Both sequences are palindromes.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains $M$ space-separated integers $A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}$.
------ Output ------
For each test case, print a single line containing one integer ― the number of valid pairs.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N, M$
$N \cdot M ≤ 10^{5}$
$1 ≤ A_{i, j} ≤ 10^{6}$ for each valid $i, j$
the sum of $N \cdot M$ over all test cases does not exceed $3 \cdot 10^{5}$
------ Subtasks ------
Subtask #1 (30 points):
$T ≤ 10$
$N \cdot M ≤ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
3 3
2 1 2
1 1 1
2 1 2
----- Sample Output 1 ------
10
----- explanation 1 ------
Example case 1: Each element forms a valid pair (where the sub-row and sub-column contain only this element) and the middle row and middle column also form one valid pair. | from sys import stdin
def fun(a, i, j):
p = 1
asi = 0
while 1:
if i - p >= 0 and i + p < n and j - p >= 0 and j + p < m:
if a[i - p][j] == a[i + p][j] and a[i][j - p] == a[i][j + p]:
asi = asi + 1
p = p + 1
else:
break
else:
break
return asi
t = int(input())
for k in range(t):
n, m = map(int, input().strip().split())
a = []
for j in range(n):
a.append(list(map(int, stdin.readline().strip().split())))
ansi = n * m
for i in range(1, n - 1):
for j in range(1, m - 1):
asi = fun(a, i, j)
ansi = ansi + asi
print(ansi) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Doraemon has a matrix with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Let's denote the element in row $i$ and column $j$ by $A_{i,j}$. Next, let's define a sub-row of a row $r$ as a sequence $A_{r, x}, A_{r, x+1}, \ldots, A_{r, y}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ M$) and a sub-column of a column $c$ by $A_{x, c}, A_{x+1, c}, \ldots, A_{y, c}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ N$).
You need to find the number of pairs (sub-row of some row, sub-column of some column) with the following properties:
1. Both sequences (the sub-row and the sub-column) have the same length.
2. This length is odd.
3. The central elements of both sequences are the same (they correspond to the same element of the matrix).
4. Both sequences are palindromes.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains $M$ space-separated integers $A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}$.
------ Output ------
For each test case, print a single line containing one integer ― the number of valid pairs.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N, M$
$N \cdot M ≤ 10^{5}$
$1 ≤ A_{i, j} ≤ 10^{6}$ for each valid $i, j$
the sum of $N \cdot M$ over all test cases does not exceed $3 \cdot 10^{5}$
------ Subtasks ------
Subtask #1 (30 points):
$T ≤ 10$
$N \cdot M ≤ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
3 3
2 1 2
1 1 1
2 1 2
----- Sample Output 1 ------
10
----- explanation 1 ------
Example case 1: Each element forms a valid pair (where the sub-row and sub-column contain only this element) and the middle row and middle column also form one valid pair. | def solve(t, i, j):
count = 0
mini1 = min(i, n - 1 - i)
mini2 = min(j, m - 1 - j)
z = min(mini1, mini2) + 1
q = 1
while q < z:
if t[i][j - q] == t[i][j + q] and t[i - q][j] == t[i + q][j]:
count += 1
q += 1
else:
break
return count
for i in range(int(input())):
z = list(map(int, input().split()))
n = z[0]
m = z[-1]
t = []
for i in range(n):
t.append([int(i) for i in input().split()])
count = n * m
for i in range(1, n - 1):
for j in range(1, m - 1):
o = solve(t, i, j)
count += o
print(count) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Doraemon has a matrix with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Let's denote the element in row $i$ and column $j$ by $A_{i,j}$. Next, let's define a sub-row of a row $r$ as a sequence $A_{r, x}, A_{r, x+1}, \ldots, A_{r, y}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ M$) and a sub-column of a column $c$ by $A_{x, c}, A_{x+1, c}, \ldots, A_{y, c}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ N$).
You need to find the number of pairs (sub-row of some row, sub-column of some column) with the following properties:
1. Both sequences (the sub-row and the sub-column) have the same length.
2. This length is odd.
3. The central elements of both sequences are the same (they correspond to the same element of the matrix).
4. Both sequences are palindromes.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains $M$ space-separated integers $A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}$.
------ Output ------
For each test case, print a single line containing one integer ― the number of valid pairs.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N, M$
$N \cdot M ≤ 10^{5}$
$1 ≤ A_{i, j} ≤ 10^{6}$ for each valid $i, j$
the sum of $N \cdot M$ over all test cases does not exceed $3 \cdot 10^{5}$
------ Subtasks ------
Subtask #1 (30 points):
$T ≤ 10$
$N \cdot M ≤ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
3 3
2 1 2
1 1 1
2 1 2
----- Sample Output 1 ------
10
----- explanation 1 ------
Example case 1: Each element forms a valid pair (where the sub-row and sub-column contain only this element) and the middle row and middle column also form one valid pair. | def longestPalindrome(s):
l = len(s)
res = [0] * l
c, r = 0, 0
for i in range(l):
m = 2 * c - i
if i < r and m >= 0:
res[i] = min(r - i, res[m])
try:
while i + res[i] < l and i - res[i] >= 0 and s[i + res[i]] == s[i - res[i]]:
res[i] += 1
except IndexError as e:
pass
if i + res[i] > r:
c = i
r = i + res[i]
return res
for t in range(int(input())):
n, m = map(int, input().split())
l = []
for i in range(n):
l.append(list(map(int, input().split())))
ldash = []
ldash = list(map(list, zip(*l)))
lh = [[0] * m] * n
ldashh = [[0] * n] * m
for i in range(n):
lh[i] = longestPalindrome(l[i])
for i in range(m):
ldashh[i] = longestPalindrome(ldash[i])
ans = 0
for i in range(n):
for j in range(m):
ans += min(lh[i][j], ldashh[j][i])
print(ans) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR WHILE BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP LIST NUMBER VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP LIST NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Doraemon has a matrix with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Let's denote the element in row $i$ and column $j$ by $A_{i,j}$. Next, let's define a sub-row of a row $r$ as a sequence $A_{r, x}, A_{r, x+1}, \ldots, A_{r, y}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ M$) and a sub-column of a column $c$ by $A_{x, c}, A_{x+1, c}, \ldots, A_{y, c}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ N$).
You need to find the number of pairs (sub-row of some row, sub-column of some column) with the following properties:
1. Both sequences (the sub-row and the sub-column) have the same length.
2. This length is odd.
3. The central elements of both sequences are the same (they correspond to the same element of the matrix).
4. Both sequences are palindromes.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains $M$ space-separated integers $A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}$.
------ Output ------
For each test case, print a single line containing one integer ― the number of valid pairs.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N, M$
$N \cdot M ≤ 10^{5}$
$1 ≤ A_{i, j} ≤ 10^{6}$ for each valid $i, j$
the sum of $N \cdot M$ over all test cases does not exceed $3 \cdot 10^{5}$
------ Subtasks ------
Subtask #1 (30 points):
$T ≤ 10$
$N \cdot M ≤ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
3 3
2 1 2
1 1 1
2 1 2
----- Sample Output 1 ------
10
----- explanation 1 ------
Example case 1: Each element forms a valid pair (where the sub-row and sub-column contain only this element) and the middle row and middle column also form one valid pair. | def doremon(grid):
n = len(grid)
m = len(grid[0])
summ = 0
for i in range(1, n - 1):
for j in range(1, m - 1):
for k in range(1, min(i, j, m - j - 1, n - i - 1) + 1):
if (
grid[i + k][j] == grid[i - k][j]
and grid[i][j + k] == grid[i][j - k]
):
summ = summ + 1
else:
break
result = summ + n * m
return result
t = int(input())
for i in range(0, t):
nm = list(map(int, input().strip().split()))
n = nm[0]
m = nm[1]
arr = []
for j in range(0, n):
scratch = list(map(int, input().strip().split()))
arr.append(scratch)
print(doremon(arr)) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Doraemon has a matrix with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Let's denote the element in row $i$ and column $j$ by $A_{i,j}$. Next, let's define a sub-row of a row $r$ as a sequence $A_{r, x}, A_{r, x+1}, \ldots, A_{r, y}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ M$) and a sub-column of a column $c$ by $A_{x, c}, A_{x+1, c}, \ldots, A_{y, c}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ N$).
You need to find the number of pairs (sub-row of some row, sub-column of some column) with the following properties:
1. Both sequences (the sub-row and the sub-column) have the same length.
2. This length is odd.
3. The central elements of both sequences are the same (they correspond to the same element of the matrix).
4. Both sequences are palindromes.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains $M$ space-separated integers $A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}$.
------ Output ------
For each test case, print a single line containing one integer ― the number of valid pairs.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N, M$
$N \cdot M ≤ 10^{5}$
$1 ≤ A_{i, j} ≤ 10^{6}$ for each valid $i, j$
the sum of $N \cdot M$ over all test cases does not exceed $3 \cdot 10^{5}$
------ Subtasks ------
Subtask #1 (30 points):
$T ≤ 10$
$N \cdot M ≤ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
3 3
2 1 2
1 1 1
2 1 2
----- Sample Output 1 ------
10
----- explanation 1 ------
Example case 1: Each element forms a valid pair (where the sub-row and sub-column contain only this element) and the middle row and middle column also form one valid pair. | t = int(input())
for test in range(t):
matrix = []
n, m = map(int, input().split())
trans_matrix = [[(0) for x in range(n)] for y in range(m)]
for k in range(n):
rows = list(map(int, input().split()))
matrix.append(rows)
for i in range(m):
for j in range(n):
trans_matrix[i][j] = matrix[j][i]
if m >= 3 and n >= 3:
count = 0
for i in range(1, n - 1):
j = 1
while j < int(m // 2):
c = 1
cnt = 0
if i > int(n // 2):
min_val = min(2 * (j + 1) - 1, 2 * (n - i) - 1)
else:
i = i
min_val = min(2 * (j + 1) - 1, 2 * (i + 1) - 1)
itr = (min_val - 1) // 2
e = matrix[i][j - itr : j + itr + 1]
e.reverse()
e_rev = e
d = trans_matrix[j][i - itr : i + itr + 1]
d.reverse()
d_rev = d
if (
j - itr >= 0
and j + itr < m
and i - itr >= 0
and i + itr < n
and e_rev == matrix[i][j - itr : j + itr + 1]
and d_rev == trans_matrix[j][i - itr : i + itr + 1]
):
count += itr
else:
while c <= itr:
if (
j - c >= 0
and j + c < m
and i - c >= 0
and i + c < n
and matrix[i][j - c] == matrix[i][j + c]
and matrix[i - c][j] == matrix[i + c][j]
):
c += 1
cnt += 1
else:
break
count += cnt
j += 1
j = int(m // 2)
while j < m - 1:
cnt = 0
c = 1
less = m - j
if i > int(n // 2):
min_val = min(2 * less - 1, 2 * (n - i) - 1)
else:
i = i
min_val = min(2 * less - 1, 2 * (i + 1) - 1)
itr = (min_val - 1) // 2
e = matrix[i][j - itr : j + itr + 1]
e.reverse()
e_rev = e
d = trans_matrix[j][i - itr : i + itr + 1]
d.reverse()
d_rev = d
if (
j - itr >= 0
and j + itr < m
and i - itr >= 0
and i + itr < n
and e_rev == matrix[i][j - itr : j + itr + 1]
and d_rev == trans_matrix[j][i - itr : i + itr + 1]
):
count += itr
else:
while c <= itr:
if (
j - c >= 0
and j + c < m
and i - c >= 0
and i + c < n
and matrix[i][j - c] == matrix[i][j + c]
and matrix[i - c][j] == matrix[i + c][j]
):
c += 1
cnt += 1
else:
break
count += cnt
j += 1
ans = count + m * n
else:
ans = m * n
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR WHILE VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR WHILE VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Doraemon has a matrix with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Let's denote the element in row $i$ and column $j$ by $A_{i,j}$. Next, let's define a sub-row of a row $r$ as a sequence $A_{r, x}, A_{r, x+1}, \ldots, A_{r, y}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ M$) and a sub-column of a column $c$ by $A_{x, c}, A_{x+1, c}, \ldots, A_{y, c}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ N$).
You need to find the number of pairs (sub-row of some row, sub-column of some column) with the following properties:
1. Both sequences (the sub-row and the sub-column) have the same length.
2. This length is odd.
3. The central elements of both sequences are the same (they correspond to the same element of the matrix).
4. Both sequences are palindromes.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains $M$ space-separated integers $A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}$.
------ Output ------
For each test case, print a single line containing one integer ― the number of valid pairs.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N, M$
$N \cdot M ≤ 10^{5}$
$1 ≤ A_{i, j} ≤ 10^{6}$ for each valid $i, j$
the sum of $N \cdot M$ over all test cases does not exceed $3 \cdot 10^{5}$
------ Subtasks ------
Subtask #1 (30 points):
$T ≤ 10$
$N \cdot M ≤ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
3 3
2 1 2
1 1 1
2 1 2
----- Sample Output 1 ------
10
----- explanation 1 ------
Example case 1: Each element forms a valid pair (where the sub-row and sub-column contain only this element) and the middle row and middle column also form one valid pair. | def count_matrix(matrix, i, j, n, m):
l = min(i - 0, n - 1 - i, j - 0, m - 1 - j)
count = 0
for k in range(l):
if (
matrix[i - k - 1][j] == matrix[i + k + 1][j]
and matrix[i][j - k - 1] == matrix[i][j + k + 1]
):
count += 1
else:
break
return count
def find_pairs(matrix, n, m):
pairs = n * m
mn = min(n, m)
l = mn if mn % 2 != 0 else mn - 1
for i in range(1, n - 1):
for j in range(1, m - 1):
pairs += count_matrix(matrix, i, j, n, m)
return pairs
pairs = []
test_cases = int(input())
for test_case in range(test_cases):
size = list(map(int, input().strip().split()))
n, m = size[0], size[1]
matrix = []
for i in range(n):
rows = list(map(int, input().strip().split()))
matrix.append(rows)
pairs.append(find_pairs(matrix, n, m))
for pair in pairs:
print(pair) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Doraemon has a matrix with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Let's denote the element in row $i$ and column $j$ by $A_{i,j}$. Next, let's define a sub-row of a row $r$ as a sequence $A_{r, x}, A_{r, x+1}, \ldots, A_{r, y}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ M$) and a sub-column of a column $c$ by $A_{x, c}, A_{x+1, c}, \ldots, A_{y, c}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ N$).
You need to find the number of pairs (sub-row of some row, sub-column of some column) with the following properties:
1. Both sequences (the sub-row and the sub-column) have the same length.
2. This length is odd.
3. The central elements of both sequences are the same (they correspond to the same element of the matrix).
4. Both sequences are palindromes.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains $M$ space-separated integers $A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}$.
------ Output ------
For each test case, print a single line containing one integer ― the number of valid pairs.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N, M$
$N \cdot M ≤ 10^{5}$
$1 ≤ A_{i, j} ≤ 10^{6}$ for each valid $i, j$
the sum of $N \cdot M$ over all test cases does not exceed $3 \cdot 10^{5}$
------ Subtasks ------
Subtask #1 (30 points):
$T ≤ 10$
$N \cdot M ≤ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
3 3
2 1 2
1 1 1
2 1 2
----- Sample Output 1 ------
10
----- explanation 1 ------
Example case 1: Each element forms a valid pair (where the sub-row and sub-column contain only this element) and the middle row and middle column also form one valid pair. | pali = lambda x: x == x[::-1]
def traverse(arr, n, m):
cnt = m * n
for i in range(n):
for j in range(m):
length = 2 * min(i, j, n - i - 1, m - j - 1) + 1
if length == 1:
continue
if (
i < length // 2
or i >= n - length // 2
or j < length // 2
or j >= m - length // 2
):
continue
for x in range(1, length // 2 + 1):
if arr[i - x][j] == arr[i + x][j] and arr[i][j - x] == arr[i][j + x]:
cnt += 1
else:
break
return cnt
def brute(arr, m, n):
res = 0
res += traverse(arr, n, m)
return res
for t in range(int(input())):
n, m = map(int, input().split())
arr = []
for i in range(n):
arr.append(list(map(int, input().split())))
print(brute(arr, m, n)) | ASSIGN VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Doraemon has a matrix with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Let's denote the element in row $i$ and column $j$ by $A_{i,j}$. Next, let's define a sub-row of a row $r$ as a sequence $A_{r, x}, A_{r, x+1}, \ldots, A_{r, y}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ M$) and a sub-column of a column $c$ by $A_{x, c}, A_{x+1, c}, \ldots, A_{y, c}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ N$).
You need to find the number of pairs (sub-row of some row, sub-column of some column) with the following properties:
1. Both sequences (the sub-row and the sub-column) have the same length.
2. This length is odd.
3. The central elements of both sequences are the same (they correspond to the same element of the matrix).
4. Both sequences are palindromes.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains $M$ space-separated integers $A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}$.
------ Output ------
For each test case, print a single line containing one integer ― the number of valid pairs.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N, M$
$N \cdot M ≤ 10^{5}$
$1 ≤ A_{i, j} ≤ 10^{6}$ for each valid $i, j$
the sum of $N \cdot M$ over all test cases does not exceed $3 \cdot 10^{5}$
------ Subtasks ------
Subtask #1 (30 points):
$T ≤ 10$
$N \cdot M ≤ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
3 3
2 1 2
1 1 1
2 1 2
----- Sample Output 1 ------
10
----- explanation 1 ------
Example case 1: Each element forms a valid pair (where the sub-row and sub-column contain only this element) and the middle row and middle column also form one valid pair. | def pairs(matrix, n, m):
pairs = n * m
for i in range(1, n - 1):
for j in range(1, m - 1):
for k in range(min(min(n - i - 1, i), min(m - j - 1, j))):
k += 1
if (
matrix[i - k][j] == matrix[i + k][j]
and matrix[i][j + k] == matrix[i][j - k]
):
pairs += 1
else:
break
return pairs
for test in range(int(input())):
N, M = [int(x) for x in input().split()]
MATRIX = [[int(x) for x in input().split()] for x in range(N)]
print(pairs(MATRIX, N, M)) | FUNC_DEF ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Doraemon has a matrix with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Let's denote the element in row $i$ and column $j$ by $A_{i,j}$. Next, let's define a sub-row of a row $r$ as a sequence $A_{r, x}, A_{r, x+1}, \ldots, A_{r, y}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ M$) and a sub-column of a column $c$ by $A_{x, c}, A_{x+1, c}, \ldots, A_{y, c}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ N$).
You need to find the number of pairs (sub-row of some row, sub-column of some column) with the following properties:
1. Both sequences (the sub-row and the sub-column) have the same length.
2. This length is odd.
3. The central elements of both sequences are the same (they correspond to the same element of the matrix).
4. Both sequences are palindromes.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains $M$ space-separated integers $A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}$.
------ Output ------
For each test case, print a single line containing one integer ― the number of valid pairs.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N, M$
$N \cdot M ≤ 10^{5}$
$1 ≤ A_{i, j} ≤ 10^{6}$ for each valid $i, j$
the sum of $N \cdot M$ over all test cases does not exceed $3 \cdot 10^{5}$
------ Subtasks ------
Subtask #1 (30 points):
$T ≤ 10$
$N \cdot M ≤ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
3 3
2 1 2
1 1 1
2 1 2
----- Sample Output 1 ------
10
----- explanation 1 ------
Example case 1: Each element forms a valid pair (where the sub-row and sub-column contain only this element) and the middle row and middle column also form one valid pair. | def getPairs(i, j, M, N, mat):
minSteps = min(i - 0, M - i - 1, j - 0, N - j - 1)
count = 0
for x in range(1, minSteps + 1):
if mat[i - x][j] == mat[i + x][j] and mat[i][j - x] == mat[i][j + x]:
count += 1
else:
break
return count
T = int(input())
for t in range(T):
M, N = map(int, list(input().strip().split()))
matrix = list()
for mat in range(M):
matrix.append(list(map(int, input().strip().split())))
count = M * N
for i in range(1, M - 1):
for j in range(1, N - 1):
count = count + getPairs(i, j, M, N, matrix)
print(count) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Doraemon has a matrix with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Let's denote the element in row $i$ and column $j$ by $A_{i,j}$. Next, let's define a sub-row of a row $r$ as a sequence $A_{r, x}, A_{r, x+1}, \ldots, A_{r, y}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ M$) and a sub-column of a column $c$ by $A_{x, c}, A_{x+1, c}, \ldots, A_{y, c}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ N$).
You need to find the number of pairs (sub-row of some row, sub-column of some column) with the following properties:
1. Both sequences (the sub-row and the sub-column) have the same length.
2. This length is odd.
3. The central elements of both sequences are the same (they correspond to the same element of the matrix).
4. Both sequences are palindromes.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains $M$ space-separated integers $A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}$.
------ Output ------
For each test case, print a single line containing one integer ― the number of valid pairs.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N, M$
$N \cdot M ≤ 10^{5}$
$1 ≤ A_{i, j} ≤ 10^{6}$ for each valid $i, j$
the sum of $N \cdot M$ over all test cases does not exceed $3 \cdot 10^{5}$
------ Subtasks ------
Subtask #1 (30 points):
$T ≤ 10$
$N \cdot M ≤ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
3 3
2 1 2
1 1 1
2 1 2
----- Sample Output 1 ------
10
----- explanation 1 ------
Example case 1: Each element forms a valid pair (where the sub-row and sub-column contain only this element) and the middle row and middle column also form one valid pair. | def calcPairs(A, i, j, xR, M, N):
p = 0
for x in range(1, xR):
if j + x < M and j - x >= 0 and i + x < N and i - x >= 0:
if A[i][j - x] == A[i][j + x] and A[i + x][j] == A[i - x][j]:
p += 1
continue
break
break
return p
for _ in range(int(input())):
N, M = map(int, input().split())
A = []
for i in range(N):
A.append(list(map(int, input().split())))
pairs = N * M
if N < 3 or M < 3:
print(pairs)
else:
xR = min(N - 1, M - 1)
for i in range(1, N - 1):
for j in range(1, M - 1):
pairs += calcPairs(A, i, j, xR, M, N)
print(pairs) | FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Doraemon has a matrix with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Let's denote the element in row $i$ and column $j$ by $A_{i,j}$. Next, let's define a sub-row of a row $r$ as a sequence $A_{r, x}, A_{r, x+1}, \ldots, A_{r, y}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ M$) and a sub-column of a column $c$ by $A_{x, c}, A_{x+1, c}, \ldots, A_{y, c}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ N$).
You need to find the number of pairs (sub-row of some row, sub-column of some column) with the following properties:
1. Both sequences (the sub-row and the sub-column) have the same length.
2. This length is odd.
3. The central elements of both sequences are the same (they correspond to the same element of the matrix).
4. Both sequences are palindromes.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains $M$ space-separated integers $A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}$.
------ Output ------
For each test case, print a single line containing one integer ― the number of valid pairs.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N, M$
$N \cdot M ≤ 10^{5}$
$1 ≤ A_{i, j} ≤ 10^{6}$ for each valid $i, j$
the sum of $N \cdot M$ over all test cases does not exceed $3 \cdot 10^{5}$
------ Subtasks ------
Subtask #1 (30 points):
$T ≤ 10$
$N \cdot M ≤ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
3 3
2 1 2
1 1 1
2 1 2
----- Sample Output 1 ------
10
----- explanation 1 ------
Example case 1: Each element forms a valid pair (where the sub-row and sub-column contain only this element) and the middle row and middle column also form one valid pair. | def manachars(arr, n, l):
n = 2 * n + 1
c = 1
r = 2
diff = -1
imirror = -1
l[1] = 1
for i in range(2, n):
imirror = 2 * c - i
diff = r - i
if diff > 0:
l[i] = min(diff, l[imirror])
try:
while (
i + l[i] < n
and i - l[i] > 0
and (
(i + l[i] + 1) % 2 == 0
or arr[(i - l[i] - 1) // 2] == arr[(i + l[i] + 1) // 2]
)
):
l[i] += 1
except:
pass
if i + l[i] > r:
c = i
r = i + l[i]
try:
t = int(input())
for _ in range(t):
n, m = list(map(int, input().split()))
arr = []
arrtrans = [[(0) for i in range(n)] for j in range(m)]
for i in range(n):
x = list(map(int, input().split()))
arr.append(x)
for j in range(m):
arrtrans[j][i] = arr[i][j]
ans = 0
l = [[(0) for i in range(2 * m + 1)] for j in range(n)]
for i in range(n):
manachars(arr[i], m, l[i])
l1 = [[(0) for i in range(2 * n + 1)] for j in range(m)]
for j in range(m):
manachars(arrtrans[j], n, l1[j])
ans = 0
for i in range(n):
for j in range(m):
a = 2 * i + 1
b = 2 * j + 1
ans += (min(l[i][b], l1[j][a]) - 1) // 2
print(m * n + ans)
except:
pass | FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR WHILE BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR |
Implement a basic calculator to evaluate a simple expression string.
The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces .
Example 1:
Input: "1 + 1"
Output: 2
Example 2:
Input: " 2-1 + 2 "
Output: 3
Example 3:
Input: "(1+(4+5+2)-3)+(6+8)"
Output: 23
Note:
You may assume that the given expression is always valid.
Do not use the eval built-in library function. | class Solution:
def calculate(self, s):
sign = [1]
nums = 0
total = 0
latestsign = 1
for c in s:
if c.isdigit():
nums = 10 * nums + int(c)
elif c == "(":
nums = 0
sign.append(latestsign)
elif c == ")":
total = total + latestsign * nums
sign.pop()
nums = 0
elif c in ["+", "-"]:
total = total + latestsign * nums
latestsign = sign[-1] * (+1 if c == "+" else -1)
nums = 0
return total + latestsign * nums | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR LIST STRING STRING ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR STRING NUMBER NUMBER ASSIGN VAR NUMBER RETURN BIN_OP VAR BIN_OP VAR VAR |
Implement a basic calculator to evaluate a simple expression string.
The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces .
Example 1:
Input: "1 + 1"
Output: 2
Example 2:
Input: " 2-1 + 2 "
Output: 3
Example 3:
Input: "(1+(4+5+2)-3)+(6+8)"
Output: 23
Note:
You may assume that the given expression is always valid.
Do not use the eval built-in library function. | class Solution:
def calculate(self, s):
total, tmp, sign = 0, 0, 1
numStack = []
signStack = []
for c in s:
if c == "+":
total, tmp, sign = total + tmp * sign, 0, 1
elif c == "-":
total, tmp, sign = total + tmp * sign, 0, -1
elif c == "(":
numStack.append(total)
signStack.append(sign)
total, tmp, sign = 0, 0, 1
elif c == ")":
total += tmp * sign
total, tmp, sign = total * signStack.pop() + numStack.pop(), 0, 1
elif "0" <= c <= "9":
tmp = tmp * 10 + ord(c) - ord("0")
else:
continue
return total + tmp * sign | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER IF VAR STRING VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER IF STRING VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR STRING RETURN BIN_OP VAR BIN_OP VAR VAR |
Implement a basic calculator to evaluate a simple expression string.
The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces .
Example 1:
Input: "1 + 1"
Output: 2
Example 2:
Input: " 2-1 + 2 "
Output: 3
Example 3:
Input: "(1+(4+5+2)-3)+(6+8)"
Output: 23
Note:
You may assume that the given expression is always valid.
Do not use the eval built-in library function. | class Solution:
def calculate(self, s):
result = number = 0
sign = 1
stack = []
for i in s:
if i == " ":
continue
if i.isdigit():
number = number * 10 + int(i)
elif i == "+":
result += sign * number
number = 0
sign = 1
elif i == "-":
result += sign * number
number = 0
sign = -1
elif i == "(":
stack.append(result)
stack.append(sign)
result = 0
number = 0
sign = 1
elif i == ")":
result += sign * number
number = 0
sign = 1
result = result * stack.pop() + stack.pop()
result += number * sign
return result | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR STRING IF FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR STRING VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR STRING VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR STRING VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR |
Implement a basic calculator to evaluate a simple expression string.
The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces .
Example 1:
Input: "1 + 1"
Output: 2
Example 2:
Input: " 2-1 + 2 "
Output: 3
Example 3:
Input: "(1+(4+5+2)-3)+(6+8)"
Output: 23
Note:
You may assume that the given expression is always valid.
Do not use the eval built-in library function. | class Solution:
def calculate(self, s):
res = 0
num = 0
sign = 1
stk = []
for c in s:
if c.isdigit():
num = 10 * num + (ord(c) - ord("0"))
elif c == "+":
res += sign * num
num = 0
sign = 1
elif c == "-":
res += sign * num
num = 0
sign = -1
elif c == "(":
stk.append(res)
stk.append(sign)
res = 0
sign = 1
elif c == ")":
res += sign * num
res *= stk.pop()
res += stk.pop()
num = 0
sign = 1
if num:
res += sign * num
return res | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING IF VAR STRING VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR STRING VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR STRING VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR BIN_OP VAR VAR RETURN VAR |
Implement a basic calculator to evaluate a simple expression string.
The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces .
Example 1:
Input: "1 + 1"
Output: 2
Example 2:
Input: " 2-1 + 2 "
Output: 3
Example 3:
Input: "(1+(4+5+2)-3)+(6+8)"
Output: 23
Note:
You may assume that the given expression is always valid.
Do not use the eval built-in library function. | class Solution:
def calculate(self, s):
expression = s
s = []
res = 0
curr = 0
sign = 1
for c in expression:
if c.isdigit():
curr *= 10
curr += int(c)
if c == "+":
res += sign * curr
curr = 0
sign = 1
if c == "-":
res += sign * curr
curr = 0
sign = -1
if c == "(":
s.append(res)
s.append(sign)
sign = 1
res = 0
if c == ")":
res += sign * curr
curr = 0
res *= s.pop()
res += s.pop()
if curr != 0:
res += sign * curr
return res | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR STRING VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR STRING VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR STRING VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR IF VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR |
Implement a basic calculator to evaluate a simple expression string.
The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces .
Example 1:
Input: "1 + 1"
Output: 2
Example 2:
Input: " 2-1 + 2 "
Output: 3
Example 3:
Input: "(1+(4+5+2)-3)+(6+8)"
Output: 23
Note:
You may assume that the given expression is always valid.
Do not use the eval built-in library function. | class Solution:
def calculate(self, s):
stack = []
res = 0
sign = 1
number = 0
for i in range(len(s)):
c = s[i]
if c.isdigit():
number = number * 10 + int(c)
elif c == "+":
res += number * sign
sign = 1
number = 0
elif c == "-":
res += number * sign
sign = -1
number = 0
elif c == "(":
stack.append(res)
stack.append(sign)
res = 0
sign = 1
number = 0
elif c == ")":
res += number * sign
res *= stack.pop()
res += stack.pop()
number = 0
sign = 1
res += number * sign
return res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR STRING VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR STRING VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR STRING VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR |
Implement a basic calculator to evaluate a simple expression string.
The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces .
Example 1:
Input: "1 + 1"
Output: 2
Example 2:
Input: " 2-1 + 2 "
Output: 3
Example 3:
Input: "(1+(4+5+2)-3)+(6+8)"
Output: 23
Note:
You may assume that the given expression is always valid.
Do not use the eval built-in library function. | class Solution:
def calculate(self, s):
if not s:
return 0
res = 0
sign = 1
stack = [1]
num = 0
for c in s:
if c <= "9" and c >= "0":
num = num * 10 + ord(c) - ord("0")
elif c in "+-":
res += sign * num
if c == "+":
sign = stack[-1]
else:
sign = stack[-1] * -1
num = 0
elif c == "(":
stack.append(sign)
elif c == ")":
stack.pop()
res += sign * num
return res | CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR STRING IF VAR STRING VAR BIN_OP VAR VAR IF VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR |
Given two integers N and K. The task is to find the string S of minimum length such that it contains all possible strings of size N as a substring. The characters of the string can be from 0 to K-1.
Example 1:
Input:
N = 2, K = 2
Output:
00110
Explanation:
There are 4 string possible of size N=2
which contains characters 0,..K-1
(i.e "00", "01","10","11")
"00110" contains all possible string as a
substring. It also has the minimum length
Example 2:
Input:
N = 2, K = 3
Output:
0010211220
Explanation: There are total 9 strings possible
of size N, given output string has the minimum
length that contains all those strings as substring.
Your Task:
You don't need to read input or print anything. Complete the function findString( ) which takes the integer N and the integer K as input parameters and returns the string.
Note: In case of multiple answers, return any string of minimum length which satisfies above condition. The driver will print the length of the string. In case of wrong answer it will print -1.
Expected Time Complexity: O(K^{N}logK)
Expected Space Complexity: O(K^{N})
Constraints:
1 ≤ N ≤ 4
1 < K < 10
1 < K^{N} < 4096 | import sys
class Solution:
res = ""
def dfs(self, ans, d, tot, N, K):
global res
if len(d) == tot:
res = ans
return True
tmp = ""
if N > 1:
tmp = ans[len(ans) - N + 1 :]
for i in range(K):
tmp += chr(48 + i)
if tmp not in d:
ans += chr(48 + i)
d[tmp] = 1
if self.dfs(ans, d, tot, N, K):
return True
d.pop(tmp)
ans = ans[0 : len(ans) - 1]
tmp = tmp[0 : len(tmp) - 1]
return False
def findString(self, N, K):
if N == 1:
r = ""
for i in range(K):
r += chr(i + 48)
return r
tot = pow(K, N)
ans = "0" * N
d = dict()
d[ans] = 1
self.dfs(ans, d, tot, N, K)
return res
sys.setrecursionlimit(10**6)
if __name__ == "__main__":
t = int(input())
for _ in range(t):
N, K = map(int, input().split())
ob = Solution()
ans = ob.findString(N, K)
ok = 1
for i in ans:
if ord(i) < 48 or ord(i) > K - 1 + 48:
ok = 0
if not ok:
print(-1)
continue
d = dict()
i = 0
while i + N - 1 < len(ans):
d[ans[i : i + N]] = 1
i += 1
tot = pow(K, N)
if len(d) == tot:
print(len(ans))
else:
print(-1) | IMPORT CLASS_DEF ASSIGN VAR STRING FUNC_DEF IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR RETURN NUMBER ASSIGN VAR STRING IF VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
Given two integers N and K. The task is to find the string S of minimum length such that it contains all possible strings of size N as a substring. The characters of the string can be from 0 to K-1.
Example 1:
Input:
N = 2, K = 2
Output:
00110
Explanation:
There are 4 string possible of size N=2
which contains characters 0,..K-1
(i.e "00", "01","10","11")
"00110" contains all possible string as a
substring. It also has the minimum length
Example 2:
Input:
N = 2, K = 3
Output:
0010211220
Explanation: There are total 9 strings possible
of size N, given output string has the minimum
length that contains all those strings as substring.
Your Task:
You don't need to read input or print anything. Complete the function findString( ) which takes the integer N and the integer K as input parameters and returns the string.
Note: In case of multiple answers, return any string of minimum length which satisfies above condition. The driver will print the length of the string. In case of wrong answer it will print -1.
Expected Time Complexity: O(K^{N}logK)
Expected Space Complexity: O(K^{N})
Constraints:
1 ≤ N ≤ 4
1 < K < 10
1 < K^{N} < 4096 | class Solution:
def findString(self, N, K):
str_char = []
for i in range(K):
str_char.append(str(i))
all_str = []
def dfs(cur_str):
if len(cur_str) == N:
all_str.append(cur_str)
return
for i in range(len(str_char)):
dfs(cur_str + str_char[i])
dfs("")
graph = {}
for a_str in all_str:
for ch in str_char:
next_node = a_str[1:] + ch
if next_node in all_str and next_node != a_str:
if a_str in graph:
graph[a_str].append(next_node)
else:
graph[a_str] = [next_node]
ans_string = ""
visited = set()
def find(node, cur_str):
nonlocal ans_string
visited.add(node)
if len(visited) == K**N:
ans_string = cur_str
return True
for next_node in graph[node]:
if next_node not in visited and find(
next_node, cur_str + next_node[-1]
):
return True
visited.remove(node)
return False
for a_str in all_str:
if find(a_str, a_str):
return ans_string
return ans_string | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_DEF IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN NUMBER FOR VAR VAR VAR IF VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN VAR RETURN VAR |
Given two integers N and K. The task is to find the string S of minimum length such that it contains all possible strings of size N as a substring. The characters of the string can be from 0 to K-1.
Example 1:
Input:
N = 2, K = 2
Output:
00110
Explanation:
There are 4 string possible of size N=2
which contains characters 0,..K-1
(i.e "00", "01","10","11")
"00110" contains all possible string as a
substring. It also has the minimum length
Example 2:
Input:
N = 2, K = 3
Output:
0010211220
Explanation: There are total 9 strings possible
of size N, given output string has the minimum
length that contains all those strings as substring.
Your Task:
You don't need to read input or print anything. Complete the function findString( ) which takes the integer N and the integer K as input parameters and returns the string.
Note: In case of multiple answers, return any string of minimum length which satisfies above condition. The driver will print the length of the string. In case of wrong answer it will print -1.
Expected Time Complexity: O(K^{N}logK)
Expected Space Complexity: O(K^{N})
Constraints:
1 ≤ N ≤ 4
1 < K < 10
1 < K^{N} < 4096 | class Solution:
def __init__(self):
self.ans = []
def findString(self, n, k):
if n == 1:
l = [str(i) for i in range(k)]
return "".join(l)
if k == 1:
return "0" * n
dic = {}
def backtrack(s, ans):
if len(ans) == k**n + n - 1:
self.ans[:] = ans[:]
return
if s in dic and dic[s] == k:
return
if s not in dic:
dic[s] = set()
for i in range(k):
if i not in dic[s]:
dic[s].add(i)
backtrack(s[1:] + str(i), ans + [str(i)])
if self.ans:
return
dic[s].remove(i)
def gen(s):
if len(s) == n - 1:
backtrack(s, list(s))
if self.ans:
return True
return False
for i in range(k):
if gen(s + str(i)):
return True
return False
gen("")
return "".join(self.ans) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL STRING VAR IF VAR NUMBER RETURN BIN_OP STRING VAR ASSIGN VAR DICT FUNC_DEF IF FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN IF VAR VAR VAR VAR VAR RETURN IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR LIST FUNC_CALL VAR VAR IF VAR RETURN EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR STRING RETURN FUNC_CALL STRING VAR |
Given two integers N and K. The task is to find the string S of minimum length such that it contains all possible strings of size N as a substring. The characters of the string can be from 0 to K-1.
Example 1:
Input:
N = 2, K = 2
Output:
00110
Explanation:
There are 4 string possible of size N=2
which contains characters 0,..K-1
(i.e "00", "01","10","11")
"00110" contains all possible string as a
substring. It also has the minimum length
Example 2:
Input:
N = 2, K = 3
Output:
0010211220
Explanation: There are total 9 strings possible
of size N, given output string has the minimum
length that contains all those strings as substring.
Your Task:
You don't need to read input or print anything. Complete the function findString( ) which takes the integer N and the integer K as input parameters and returns the string.
Note: In case of multiple answers, return any string of minimum length which satisfies above condition. The driver will print the length of the string. In case of wrong answer it will print -1.
Expected Time Complexity: O(K^{N}logK)
Expected Space Complexity: O(K^{N})
Constraints:
1 ≤ N ≤ 4
1 < K < 10
1 < K^{N} < 4096 | class Solution:
def findString(self, n, k):
out = [0]
combs = set()
def dfs(n, string):
if n == 0:
combs.add(string)
return
for i in range(K):
dfs(n - 1, string + str(i))
vis = set()
def find(comb, string, graph):
vis.add(comb)
if len(vis) == K**N:
out[0] = string
return True
for y in graph[comb]:
if y not in vis and find(y, string + y[-1], graph):
return True
vis.remove(comb)
return False
dfs(N, "")
graph = {}
for i in combs:
for z in range(K):
comb = i[1:] + str(z)
if comb in combs and comb != i:
if i in graph:
graph[i].append(comb)
else:
graph[i] = [comb]
for i in combs:
if find(i, i, graph):
return out[0]
return out[0] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR RETURN NUMBER FOR VAR VAR VAR IF VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR DICT FOR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR RETURN VAR NUMBER RETURN VAR NUMBER |
Given two integers N and K. The task is to find the string S of minimum length such that it contains all possible strings of size N as a substring. The characters of the string can be from 0 to K-1.
Example 1:
Input:
N = 2, K = 2
Output:
00110
Explanation:
There are 4 string possible of size N=2
which contains characters 0,..K-1
(i.e "00", "01","10","11")
"00110" contains all possible string as a
substring. It also has the minimum length
Example 2:
Input:
N = 2, K = 3
Output:
0010211220
Explanation: There are total 9 strings possible
of size N, given output string has the minimum
length that contains all those strings as substring.
Your Task:
You don't need to read input or print anything. Complete the function findString( ) which takes the integer N and the integer K as input parameters and returns the string.
Note: In case of multiple answers, return any string of minimum length which satisfies above condition. The driver will print the length of the string. In case of wrong answer it will print -1.
Expected Time Complexity: O(K^{N}logK)
Expected Space Complexity: O(K^{N})
Constraints:
1 ≤ N ≤ 4
1 < K < 10
1 < K^{N} < 4096 | class Solution:
def findString(self, N, K):
def __init__(self):
self.a = None
def dfs(n, k, string, ans):
if n == 0:
ans.add(string)
return ans
for i in range(k):
ans = dfs(n - 1, k, string + str(i), ans)
return ans
def find(ele, string):
vis.add(ele)
if len(vis) == K**N:
self.a = string
return True
for y in graph[ele]:
if y not in vis and find(y, string + y[-1]):
return True
vis.remove(ele)
return False
ans = set()
ans = dfs(N, K, "", ans)
graph = {}
for i in ans:
for z in range(K):
ele = i[1:] + str(z)
if ele in ans and ele != i:
if i in graph:
graph[i].append(ele)
else:
graph[i] = [ele]
vis = set()
for i in ans:
if find(i, i):
return self.a
return self.a | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NONE FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN NUMBER FOR VAR VAR VAR IF VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR STRING VAR ASSIGN VAR DICT FOR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN VAR RETURN VAR |
Given two integers N and K. The task is to find the string S of minimum length such that it contains all possible strings of size N as a substring. The characters of the string can be from 0 to K-1.
Example 1:
Input:
N = 2, K = 2
Output:
00110
Explanation:
There are 4 string possible of size N=2
which contains characters 0,..K-1
(i.e "00", "01","10","11")
"00110" contains all possible string as a
substring. It also has the minimum length
Example 2:
Input:
N = 2, K = 3
Output:
0010211220
Explanation: There are total 9 strings possible
of size N, given output string has the minimum
length that contains all those strings as substring.
Your Task:
You don't need to read input or print anything. Complete the function findString( ) which takes the integer N and the integer K as input parameters and returns the string.
Note: In case of multiple answers, return any string of minimum length which satisfies above condition. The driver will print the length of the string. In case of wrong answer it will print -1.
Expected Time Complexity: O(K^{N}logK)
Expected Space Complexity: O(K^{N})
Constraints:
1 ≤ N ≤ 4
1 < K < 10
1 < K^{N} < 4096 | class Solution:
res = ""
def dfs(self, ans, d, tot, N, K):
global res
if len(d) == tot:
res = ans
return True
tmp = ""
if N > 1:
tmp = ans[len(ans) - N + 1 :]
for i in range(K):
tmp += chr(48 + i)
if tmp not in d:
ans += chr(48 + i)
d[tmp] = 1
if self.dfs(ans, d, tot, N, K):
return True
d.pop(tmp)
ans = ans[0 : len(ans) - 1]
tmp = tmp[0 : len(tmp) - 1]
return False
def findString(self, N, K):
if N == 1:
r = ""
for i in range(K):
r += chr(i + 48)
return r
tot = pow(K, N)
ans = "0" * N
d = dict()
d[ans] = 1
self.dfs(ans, d, tot, N, K)
return res | CLASS_DEF ASSIGN VAR STRING FUNC_DEF IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR RETURN NUMBER ASSIGN VAR STRING IF VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR |
Given two integers N and K. The task is to find the string S of minimum length such that it contains all possible strings of size N as a substring. The characters of the string can be from 0 to K-1.
Example 1:
Input:
N = 2, K = 2
Output:
00110
Explanation:
There are 4 string possible of size N=2
which contains characters 0,..K-1
(i.e "00", "01","10","11")
"00110" contains all possible string as a
substring. It also has the minimum length
Example 2:
Input:
N = 2, K = 3
Output:
0010211220
Explanation: There are total 9 strings possible
of size N, given output string has the minimum
length that contains all those strings as substring.
Your Task:
You don't need to read input or print anything. Complete the function findString( ) which takes the integer N and the integer K as input parameters and returns the string.
Note: In case of multiple answers, return any string of minimum length which satisfies above condition. The driver will print the length of the string. In case of wrong answer it will print -1.
Expected Time Complexity: O(K^{N}logK)
Expected Space Complexity: O(K^{N})
Constraints:
1 ≤ N ≤ 4
1 < K < 10
1 < K^{N} < 4096 | from itertools import product
class Solution:
def findString(self, n, k):
perms = set(map(lambda x: "".join(x), product(map(str, range(k)), repeat=n)))
ans = []
def dfs(node):
if not perms:
return
for d in map(str, range(k)):
next_ = (node + d)[1:]
if next_ in perms:
perms.remove(next_)
dfs(next_)
ans.append(d)
start = "0" * n
perms.remove(start)
dfs(start)
return "".join(ans) + start | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FUNC_DEF IF VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL STRING VAR VAR |
Given two integers N and K. The task is to find the string S of minimum length such that it contains all possible strings of size N as a substring. The characters of the string can be from 0 to K-1.
Example 1:
Input:
N = 2, K = 2
Output:
00110
Explanation:
There are 4 string possible of size N=2
which contains characters 0,..K-1
(i.e "00", "01","10","11")
"00110" contains all possible string as a
substring. It also has the minimum length
Example 2:
Input:
N = 2, K = 3
Output:
0010211220
Explanation: There are total 9 strings possible
of size N, given output string has the minimum
length that contains all those strings as substring.
Your Task:
You don't need to read input or print anything. Complete the function findString( ) which takes the integer N and the integer K as input parameters and returns the string.
Note: In case of multiple answers, return any string of minimum length which satisfies above condition. The driver will print the length of the string. In case of wrong answer it will print -1.
Expected Time Complexity: O(K^{N}logK)
Expected Space Complexity: O(K^{N})
Constraints:
1 ≤ N ≤ 4
1 < K < 10
1 < K^{N} < 4096 | import sys
class Solution:
seen = []
edge = []
def dfs(node, k, A):
for i in range(k):
str_ = node + A[i]
if str_ not in Solution.seen:
Solution.seen.append(str_)
Solution.dfs(str_[1:], k, A)
Solution.edge.append(i)
def findString(self, N, K):
Solution.seen.clear()
Solution.edge.clear()
A = [str(i) for i in range(K)]
sno = A[0] * (N - 1)
Solution.dfs(sno, K, A)
ans = ""
l = K**N
for i in range(l):
ans += A[Solution.edge[i]]
ans += sno
return ans
sys.setrecursionlimit(10**6)
if __name__ == "__main__":
t = int(input())
for _ in range(t):
N, K = map(int, input().split())
ob = Solution()
ans = ob.findString(N, K)
ok = 1
for i in ans:
if ord(i) < 48 or ord(i) > K - 1 + 48:
ok = 0
if not ok:
print(-1)
continue
d = dict()
i = 0
while i + N - 1 < len(ans):
d[ans[i : i + N]] = 1
i += 1
tot = pow(K, N)
if len(d) == tot:
print(len(ans))
else:
print(-1) | IMPORT CLASS_DEF ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
Given two integers N and K. The task is to find the string S of minimum length such that it contains all possible strings of size N as a substring. The characters of the string can be from 0 to K-1.
Example 1:
Input:
N = 2, K = 2
Output:
00110
Explanation:
There are 4 string possible of size N=2
which contains characters 0,..K-1
(i.e "00", "01","10","11")
"00110" contains all possible string as a
substring. It also has the minimum length
Example 2:
Input:
N = 2, K = 3
Output:
0010211220
Explanation: There are total 9 strings possible
of size N, given output string has the minimum
length that contains all those strings as substring.
Your Task:
You don't need to read input or print anything. Complete the function findString( ) which takes the integer N and the integer K as input parameters and returns the string.
Note: In case of multiple answers, return any string of minimum length which satisfies above condition. The driver will print the length of the string. In case of wrong answer it will print -1.
Expected Time Complexity: O(K^{N}logK)
Expected Space Complexity: O(K^{N})
Constraints:
1 ≤ N ≤ 4
1 < K < 10
1 < K^{N} < 4096 | class Solution:
def findString(self, N, K):
total = K**N
visited = set()
ans = ""
def dfs(subs, counter, res):
if counter == total:
nonlocal ans
ans = res
return True
for digit in range(K):
newSubstring = subs[1:] + str(digit)
if newSubstring in visited:
continue
visited.add(newSubstring)
if dfs(newSubstring, counter + 1, res + str(digit)):
return True
visited.remove(newSubstring)
return
start = "".join(["0"] * N)
visited.add(start)
dfs(start, 1, start)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING FUNC_DEF IF VAR VAR ASSIGN VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL STRING BIN_OP LIST STRING VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR RETURN VAR |
Given two integers N and K. The task is to find the string S of minimum length such that it contains all possible strings of size N as a substring. The characters of the string can be from 0 to K-1.
Example 1:
Input:
N = 2, K = 2
Output:
00110
Explanation:
There are 4 string possible of size N=2
which contains characters 0,..K-1
(i.e "00", "01","10","11")
"00110" contains all possible string as a
substring. It also has the minimum length
Example 2:
Input:
N = 2, K = 3
Output:
0010211220
Explanation: There are total 9 strings possible
of size N, given output string has the minimum
length that contains all those strings as substring.
Your Task:
You don't need to read input or print anything. Complete the function findString( ) which takes the integer N and the integer K as input parameters and returns the string.
Note: In case of multiple answers, return any string of minimum length which satisfies above condition. The driver will print the length of the string. In case of wrong answer it will print -1.
Expected Time Complexity: O(K^{N}logK)
Expected Space Complexity: O(K^{N})
Constraints:
1 ≤ N ≤ 4
1 < K < 10
1 < K^{N} < 4096 | class Solution:
def findString(self, N, K):
ans = "0" * (N - 1)
str_set = set()
for i in range(K**N):
substr = ans[-N + 1 :] if N > 1 else ""
for j in range(K - 1, -1, -1):
if substr + str(j) not in str_set:
str_set.add(substr + str(j))
ans += str(j)
break
return ans[-1::-1] | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP STRING BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR NUMBER NUMBER |
Given two integers N and K. The task is to find the string S of minimum length such that it contains all possible strings of size N as a substring. The characters of the string can be from 0 to K-1.
Example 1:
Input:
N = 2, K = 2
Output:
00110
Explanation:
There are 4 string possible of size N=2
which contains characters 0,..K-1
(i.e "00", "01","10","11")
"00110" contains all possible string as a
substring. It also has the minimum length
Example 2:
Input:
N = 2, K = 3
Output:
0010211220
Explanation: There are total 9 strings possible
of size N, given output string has the minimum
length that contains all those strings as substring.
Your Task:
You don't need to read input or print anything. Complete the function findString( ) which takes the integer N and the integer K as input parameters and returns the string.
Note: In case of multiple answers, return any string of minimum length which satisfies above condition. The driver will print the length of the string. In case of wrong answer it will print -1.
Expected Time Complexity: O(K^{N}logK)
Expected Space Complexity: O(K^{N})
Constraints:
1 ≤ N ≤ 4
1 < K < 10
1 < K^{N} < 4096 | class Solution:
def __init__(self):
self.ans = ""
def findString(self, N, K):
h = {}
def dfs(a):
for i in range(K):
new = a + str(i)
if new not in h:
h[new] = True
dfs(new[1:])
self.ans += str(i)
start = "".join(["0" for _ in range(N - 1)])
h[start] = True
dfs(start)
self.ans += start
return self.ans | CLASS_DEF FUNC_DEF ASSIGN VAR STRING FUNC_DEF ASSIGN VAR DICT FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
ans = [0]
def split(curr, ind):
if ind >= len(s):
return len(curr)
for i in range(ind + 1, len(s) + 1):
tmp = curr.copy()
tmp.add(s[ind:i])
s1 = split(tmp, i)
ans[0] = max(ans[0], s1)
return len(curr)
split(set(), 0)
return ans[0] | CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR RETURN FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER RETURN VAR NUMBER VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
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()) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER FUNC_DEF VAR VAR IF VAR VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str, seen=()) -> int:
return max(
(
1 + self.maxUniqueSplit(s[i:], {candidate, *seen})
for i in range(1, len(s) + 1)
if (candidate := s[:i]) not in seen
),
default=0,
) | CLASS_DEF FUNC_DEF VAR RETURN FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
combin_dict = {s[0]: [set(s[0])]}
for index, char in enumerate(s[1:]):
new_dict = {}
for last_str, sets in combin_dict.items():
for combin_set in sets:
if char not in new_dict:
new_dict[char] = []
single_new_set = combin_set | set(char)
if len("".join(combin_set)) == index + 1:
new_dict[char].append(single_new_set)
new_last = last_str + char
if len("".join(combin_set)) == index + 1:
new_set = combin_set.difference(set([last_str]))
else:
new_set = combin_set
new_set.add(new_last)
if new_last not in new_dict:
new_dict[new_last] = []
new_dict[new_last].append(new_set)
combin_dict = new_dict
res = 0
for key, value in combin_dict.items():
for value_set in value:
if len("".join(value_set)) == len(s):
res = max(res, len(value_set))
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT VAR NUMBER LIST FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR LIST ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL STRING VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR FUNC_CALL STRING VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR LIST VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FOR VAR VAR IF FUNC_CALL VAR FUNC_CALL STRING VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
to_ret = 0
for bin_i in range(2 ** (len(s) - 1)):
to_deal = set()
mark = True
last = s[0]
for i in range(len(s) - 1):
cn = s[i + 1]
if bin_i & 1 << i:
if last in to_deal:
mark = False
break
to_deal.add(last)
last = cn
else:
last += cn
if last in to_deal:
mark = False
else:
to_deal.add(last)
if mark:
to_ret = max(to_ret, len(to_deal))
return to_ret | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
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 | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF BIN_OP BIN_OP VAR VAR VAR VAR RETURN IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
start = 0
end = 0
se = set()
res = 0
def helper(stn, se, count):
nonlocal res
if not stn:
res = max(res, count)
if len(stn) + count <= res:
return
for i in range(1, len(stn) + 1):
if stn[:i] not in se:
helper(stn[i:], se | {stn[:i]}, count + 1)
helper(s, se, 0)
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
dp = [[] for i in range(len(s))]
dp[0].append([s[0]])
for i in range(1, len(s)):
for j in range(len(dp[i - 1])):
comb = dp[i - 1][j]
dp[i].append(comb[:-1] + [comb[-1] + s[i]])
dp[i].append(comb + [s[i]])
_max = 0
for i in range(len(s)):
for j in range(len(dp[i])):
length = len(set(dp[i][j]))
if length > _max:
_max = length
return _max | CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR LIST VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
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 | CLASS_DEF FUNC_DEF VAR RETURN FUNC_CALL VAR VAR NUMBER LIST VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR VAR BIN_OP VAR NUMBER RETURN VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
l = len(s)
for i in range(l - 1, 0, -1):
for j in itertools.combinations(range(1, l), i):
cut = [0] + list(j)
subs = []
for k in range(len(cut)):
if k == len(cut) - 1:
subs.append(s[cut[k] :])
else:
subs.append(s[cut[k] : cut[k + 1]])
if len(subs) == len(set(subs)):
return len(subs)
return 1 | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR RETURN NUMBER VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
def check(s):
if len(s) == 1:
return [[s]]
if len(s) == 0:
return [[]]
ans = []
for i in range(1, len(s) + 1):
for item in check(s[i:]):
ans.append([s[:i]] + item)
return ans
result = check(s)
ans = 0
for item in result:
if len(set(item)) == len(item):
ans = max(len(item), ans)
return ans | CLASS_DEF FUNC_DEF VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN LIST LIST VAR IF FUNC_CALL VAR VAR NUMBER RETURN LIST LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def __init__(self):
self.result = 0
def maxUniqueSplit(self, s: str) -> int:
if not s:
return 0
def backtrack(start, cur):
if start == len(s):
self.result = max(self.result, len(set(cur)))
return
for i in range(start, len(s)):
cur += [s[start : i + 1]]
backtrack(i + 1, cur)
cur.pop()
backtrack(0, [])
return self.result | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF VAR IF VAR RETURN NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR LIST VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER LIST RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
if not s:
return 0
self.answer = 0
substrings = []
def backtrack(start_i):
if start_i == len(s):
self.answer = max(self.answer, len(set(substrings)))
return
for j in range(start_i + 1, len(s) + 1):
substrings.append(s[start_i:j])
backtrack(j)
substrings.pop()
backtrack(0)
return self.answer | CLASS_DEF FUNC_DEF VAR IF VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FUNC_DEF IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def 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 | CLASS_DEF FUNC_DEF VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR LIST VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
words = list(s)
N = len(words)
ans = 0
def backtrack(words, s):
nonlocal ans
n = len(words)
if n == 0:
ans = max(len(s), ans)
for i in range(1, n + 1):
if str(words[:i]) not in s:
s.add(str(words[:i]))
backtrack(words[i:], s)
s.remove(str(words[:i]))
backtrack(words, set())
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
def solve(S, subs):
if not S:
return len(subs)
ans = 0
for i in range(len(S)):
sub = S[: i + 1]
ans = max(ans, solve(S[i + 1 :], subs | {sub}))
return ans
return solve(s, set()) | CLASS_DEF FUNC_DEF VAR FUNC_DEF IF VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
n = len(s)
self.res = 0
def dfs(strs, s):
if not s:
if len(set(strs)) == len(strs):
self.res = max(self.res, len(strs))
return
dfs(strs + [s[0]], s[1:])
if strs:
dfs(strs[:-1] + [strs[-1] + s[:1]], s[1:])
return
dfs([], s)
return self.res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN EXPR FUNC_CALL VAR LIST VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
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())
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
def dfs(s, path):
res = 0
if not s:
return len(path)
for i in range(1, len(s) + 1):
res = max(res, dfs(s[i:], path | {s[:i]}))
return res
return dfs(s, set()) | CLASS_DEF FUNC_DEF VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR RETURN FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | from itertools import combinations
class Solution:
def maxUniqueSplit(self, s: str) -> int:
n = len(s)
if n == 1:
return 1
from itertools import combinations
L = list(range(1, n))
for d in range(n - 1, 0, -1):
for x in combinations(L, d):
hh = [0] + list(x) + [n]
tmep = [s[hh[j] : hh[j + 1]] for j in range(len(hh) - 1)]
if len(set(tmep)) == len(tmep):
return d + 1
return 1 | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR VAR LIST VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR NUMBER RETURN NUMBER VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
visited = set()
def dfs(start):
nonlocal visited
if start == len(s):
return 0
res = 0
for i in range(start, len(s)):
if s[start : i + 1] not in visited:
visited.add(s[start : i + 1])
res = max(res, 1 + dfs(i + 1))
visited.remove(s[start : i + 1])
else:
res = max(res, dfs(i + 1))
return res
return dfs(0) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR NUMBER VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, ss: str) -> int:
@lru_cache(None)
def dfs(s, words):
if not s:
return 0
res = 0
words = set(words)
for i in range(len(s)):
if s[: i + 1] not in words:
res = max(
res, 1 + dfs(s[i + 1 :], tuple(words | set([s[: i + 1]])))
)
return res
return dfs(ss, ()) | CLASS_DEF FUNC_DEF VAR FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER RETURN VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
N = len(s)
res = 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))
return len(set(words))
ways = 2 ** (N - 1)
for i in range(ways):
res = max(res, findSplit(i))
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL BIN_OP FUNC_CALL VAR VAR NUMBER STRING VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR LIST IF VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
exist = collections.defaultdict(int)
self.result = 0
n = len(s)
def dfs(index: int) -> None:
if index == n:
self.result = max(self.result, len(exist))
return
for i in range(index + 1, n + 1):
st = s[index:i]
exist[st] += 1
dfs(i)
exist[st] -= 1
if exist[st] == 0:
del exist[st]
dfs(0)
return self.result | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NONE EXPR FUNC_CALL VAR NUMBER RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
self.x, n = 0, len(s)
def maxUniqueSplit_(i=0, S=set()):
if s[i:] not in S:
self.x = max(self.x, len(S) + 1)
for j in range(i + 1, n):
if s[i:j] not in S and len(S) + 1 + n - j > self.x:
maxUniqueSplit_(j, S.union({s[i:j]}))
maxUniqueSplit_()
return self.x | CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_DEF NUMBER FUNC_CALL VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
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) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF VAR ASSIGN VAR NUMBER IF VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
def helper(i, s, seen):
if i > len(s) - 1:
return len(seen)
max_len = 1
for k in range(i, len(s)):
temp = set(seen)
temp.add(s[i : k + 1])
max_len = max(max_len, helper(k + 1, s, temp))
return max_len
return helper(0, s, set([])) | CLASS_DEF FUNC_DEF VAR FUNC_DEF IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR LIST VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
self._s = s
self.best = 1
self.solve(0, set(), 0)
return self.best
def solve(self, index, seen, count):
s = self._s
if index >= len(s):
self.best = max(self.best, count)
for end in range(index + 1, len(s) + 1):
if s[index:end] not in seen:
seen.add(s[index:end])
self.solve(end, seen, count + 1)
seen.remove(s[index:end]) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def _dfs(self, s, i, visited):
if i == len(s):
yield len(visited)
return
for j in range(i + 1, len(s) + 1):
if s[i:j] not in visited:
visited.add(s[i:j])
yield from self._dfs(s, j, visited)
visited.remove(s[i:j])
def maxUniqueSplit(self, s: str) -> int:
if not s:
return 0
return max(self._dfs(s, 0, set())) | CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF VAR IF VAR RETURN NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
def divide(s):
for i in range(1, len(s)):
low = s[0:i]
high = s[i:]
yield low, high
for div in divide(high):
ret = [low]
ret.extend(div)
yield ret
combinations = list(divide(s))
combinations.append(s)
ret = 1
for comb in combinations:
if len(comb) == len(set(comb)):
ret = max(ret, len(comb))
return ret | CLASS_DEF FUNC_DEF VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR VAR EXPR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR EXPR FUNC_CALL VAR VAR EXPR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
ls = len(s)
sl = ls - 1
ps = 1 << sl
ans = 0
for i in range(ps):
bi = bin(i)[2:].zfill(sl)
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 | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR STRING LIST BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | def walk_divisions(string, covered, cut):
if cut == len(string):
yield len(covered)
else:
for nxt in range(cut + 1, len(string) + 1):
substring = string[cut:nxt]
if substring not in covered:
covered.append(substring)
yield from walk_divisions(string, covered, nxt)
covered.pop()
def max_unique_split(string):
if len(string) == 1:
return 1
assert len(string) >= 2
return max(walk_divisions(string, [], 0))
class Solution:
def maxUniqueSplit(self, s: str) -> int:
return max_unique_split(s) | FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR LIST NUMBER CLASS_DEF FUNC_DEF VAR RETURN FUNC_CALL VAR VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
def solve(last, idx, cur):
if idx == len(s):
if len(cur) == len(set(cur)):
self.ans = max(self.ans, len(cur))
return
cur.append(s[last : idx + 1])
solve(idx + 1, idx + 1, cur)
cur.pop()
solve(last, idx + 1, cur)
self.ans = 0
solve(0, 0, [])
return self.ans | CLASS_DEF FUNC_DEF VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER LIST RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
N = len(s)
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()) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
res = 0
dfs = [[set(), s]]
while dfs:
curSet, curS = dfs.pop()
res = max(res, len(curSet))
if len(curSet) + len(curS) <= res or len(curS) == 0:
continue
for i in range(len(curS)):
if curS[: i + 1] in curSet:
continue
curSet.add(curS[: i + 1])
dfs.append([curSet.copy(), curS[i + 1 :]])
curSet.remove(curS[: i + 1])
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR LIST LIST FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, S: str) -> int:
n = len(S)
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 | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
used = set()
self.res = 1
self.backtrack(s, 0, used)
return self.res
def backtrack(self, s, cur, used):
if not s:
self.res = max(self.res, cur)
return
for i in range(1, len(s) + 1):
if s[:i] not in used:
used.add(s[:i])
self.backtrack(s[i:], cur + 1, used)
used.remove(s[:i]) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR RETURN VAR VAR FUNC_DEF IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
if len(s) == 1:
return 1
n = len(s)
dp = [0] * (n + 1)
dp[0] = [set()]
for end in range(1, n + 1):
ls = []
sub = set()
for start in range(end):
for ele in dp[start]:
last = ele.copy()
node = s[start:end]
last.add(node)
ls.append(last)
dp[end] = ls
res = 0
for i in dp[-1]:
if len(i) > res:
res = len(i)
return res | CLASS_DEF FUNC_DEF VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER LIST FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | 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]
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 | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
def split(s):
if len(s) == 0:
return []
if len(s) == 1:
return [[s]]
res = []
for i in range(1, len(s) + 1):
ans = [s[:i]]
splits = split(s[i:])
if splits:
for sub in splits:
res.append(ans + sub)
else:
res.append(ans)
return res
options = split(s)
m = 0
for o in options:
if len(o) == len(set(o)):
m = max(m, len(o))
return m | CLASS_DEF FUNC_DEF VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN LIST IF FUNC_CALL VAR VAR NUMBER RETURN LIST LIST VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
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])
if len(ss) == K:
res = max(res, K)
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER LIST VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def helper(self, s, occur):
if len(s) == 0:
return 0
curMax = -1
for i in range(1, len(s) + 1):
if s[:i] not in occur:
occur.append(s[:i])
if (s[i:], tuple(occur)) in self.memo:
res = self.memo[s[i:], tuple(occur)]
else:
res = self.helper(s[i:], occur)
self.memo[s[i:], tuple(occur)] = res
if res >= 0:
curMax = max(curMax, 1 + res)
occur.pop()
return curMax
def maxUniqueSplit(self, s: str) -> int:
self.memo = {}
return self.helper(s, []) | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR RETURN VAR FUNC_DEF VAR ASSIGN VAR DICT RETURN FUNC_CALL VAR VAR LIST VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
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 | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
def helper(s, seen=set()):
maximum = 0
for i in range(1, len(s) + 1):
candidate = s[:i]
if candidate not in seen:
maximum = max(maximum, 1 + helper(s[i:], {candidate, *seen}))
return maximum
return helper(s) | CLASS_DEF FUNC_DEF VAR FUNC_DEF FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | from itertools import combinations
class Solution:
def maxUniqueSplit(self, s: str) -> int:
from itertools import combinations
for k in reversed(list(range(len(s) + 1))):
for comb in combinations(list(range(1, len(s))), k):
split_idxs = [0, *comb, len(s)]
splits = []
for prev, next in zip(split_idxs, split_idxs[1:]):
splits.append(s[prev:next])
if len(set(splits)) == len(splits):
return len(splits)
return 0 | CLASS_DEF FUNC_DEF VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR LIST NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR RETURN NUMBER VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
def bitsoncount(x):
return bin(x).count("1")
ans = 0
n = len(s)
seen = defaultdict(int)
for m in range(1 << n):
if bitsoncount(m) <= ans:
continue
prev = 0
count = 0
for i in range(n):
if m & 1 << i:
sub = s[prev : i + 1]
prev = i + 1
if sub not in seen:
seen[sub] += 1
count += 1
ans = max(ans, count)
seen.clear()
return ans | CLASS_DEF FUNC_DEF VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | import itertools
class Solution:
def maxUniqueSplit(self, s: str) -> int:
positions = []
for i in range(1, len(s)):
positions.append(i)
if len(s) < 3:
if s[0] != s[1]:
return 2
maximally_split = 1
for i in range(1, len(s)):
separate_positions = list(itertools.combinations(positions, i))
for separate_position in separate_positions:
curr_position = 0
curr_idx = 0
substrings = []
while curr_idx < len(separate_position):
substrings.append(s[curr_position : separate_position[curr_idx]])
curr_position = separate_position[curr_idx]
curr_idx += 1
substrings.append(s[curr_position:])
length_of_unique_list = len(set(substrings))
if length_of_unique_list == len(substrings):
if length_of_unique_list > maximally_split:
maximally_split = length_of_unique_list
if len(set(list(s))) > maximally_split:
maximally_split = len(set(list(s)))
return maximally_split | IMPORT CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
return self.bt(s, set(), 0, 0)
def bt(self, s, d, l, maxl):
if l >= len(s):
maxl = max(len(d), maxl)
return maxl
else:
for r in range(l + 1, len(s) + 1):
if s[l:r] not in d:
d.add(s[l:r])
maxl = self.bt(s, d, r, maxl)
d.remove(s[l:r])
return maxl | CLASS_DEF FUNC_DEF VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER NUMBER VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
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 | CLASS_DEF FUNC_DEF VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
res = 0
def dfs(cur_pos):
nonlocal res
if cur_pos == len(s):
res = max(res, len(seen))
for next_pos in range(cur_pos, len(s)):
if s[cur_pos : next_pos + 1] not in seen:
seen.add(s[cur_pos : next_pos + 1])
dfs(next_pos + 1)
seen.remove(s[cur_pos : next_pos + 1])
seen = set()
dfs(0)
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
def f(s, seen):
if not s:
return 0
res = 0
for i in range(1, len(s) + 1):
chunk = s[:i]
if chunk not in seen:
res = max(res, f(s[i:], seen | {chunk}) + 1)
return res
return f(s, set()) | CLASS_DEF FUNC_DEF VAR FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
if not s:
return 0
splits = [[] for _ in range(len(s))]
best_len = 0
for i, c in enumerate(s):
splits[i].append(set([s[: i + 1]]))
best_len = max(best_len, 1)
for j in range(1, i + 1):
ss = s[j : i + 1]
for spl in splits[j - 1]:
if ss not in spl:
best_len = max(best_len, len(spl) + 1)
splits[i].append(spl | set([ss]))
return best_len | CLASS_DEF FUNC_DEF VAR IF VAR RETURN NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR LIST VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
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) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR LIST RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
def permutations(s):
result = [[s]]
for i in range(1, len(s)):
start = [s[:i]]
end = s[i:]
for p in permutations(end):
result.append(start + p)
return result
l = permutations(s)
maxt = 0
totalset = set()
for i in l:
if len(set(i)) == len(i):
if len(i) > maxt:
maxt = len(i)
return maxt | CLASS_DEF FUNC_DEF VAR FUNC_DEF ASSIGN VAR LIST LIST VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
n = len(s)
ans = 1
for i in range(2 ** (n - 1)):
b = "1" + bin(i)[2:].zfill(n - 1) + "1"
pr = 0
w = set()
flag = True
for k in range(1, n + 1):
if k == n or b[k] == "1":
chrs = s[pr:k]
if chrs in w:
flag = False
break
w.add(chrs)
pr = k
if flag:
ans = max(ans, len(w))
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING FUNC_CALL FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR STRING ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
r = 0
m = len(s)
for i in range(2 ** (m - 1)):
bb = "{0:0" + str(m - 1) + "b}"
b = bb.format(i)
st = 0
l = []
for k in range(len(b)):
if b[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])
if len(l) == len(set(l)):
r = max(r, len(l))
print(r)
return r | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING FUNC_CALL VAR BIN_OP VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
ans = 0
for i in range(1, 2 ** (len(s) - 1) + 1):
prev = 0
seen = set()
for j in range(0, len(s)):
if i & 1 << j:
seen.add(s[prev : j + 1])
prev = j + 1
final = s[prev : len(s)]
if len(final):
seen.add(final)
ans = max(ans, len(seen))
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
d = {}
for i in s:
if i in d:
d[i] += 1
else:
d[i] = 1
if len(d) == len(s):
return len(d)
def waysToSplit(s):
if not s:
return []
if len(s) == 1:
return [[s]]
else:
res = []
for i in range(len(s)):
first = [s[: i + 1]]
second = waysToSplit(s[i + 1 :])
for seq in second:
res.append(first + seq)
res.append([s])
return res
maxCount = 1
sequences = waysToSplit(s)
for seq in sequences:
s = set()
count = 0
foundDup = False
for element in seq:
if element in s:
foundDup = True
break
else:
count += 1
s.add(element)
if not foundDup:
maxCount = max(maxCount, count)
return maxCount | CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF IF VAR RETURN LIST IF FUNC_CALL VAR VAR NUMBER RETURN LIST LIST VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR LIST VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
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 | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR LIST RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
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 | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
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]) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER RETURN VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
def rec(i, myset):
if i == len(s):
return 0
ans = -float("inf")
for j in range(i, len(s)):
if s[i : j + 1] not in myset:
ans = max(ans, 1 + rec(j + 1, myset | set([s[i : j + 1]])))
return ans
return rec(0, set()) | CLASS_DEF FUNC_DEF VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
res = [[s[0]]]
for i in s[1:]:
for _ in range(len(res)):
j = res.pop(0)
res.append(j + [i])
res.append(j[:-1] + [j[-1] + i])
return max(list(map(len, list(map(set, res))))) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST LIST VAR NUMBER FOR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
n = len(s)
maxi = 0
def sub(ind, ar):
if ind == n:
if len(ar) == len(set(ar)):
return len(ar)
return maxi
a = ar + [s[ind]]
b = ar[:]
b[-1] += s[ind]
return max(sub(ind + 1, a), sub(ind + 1, b))
return sub(1, [s[0]]) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR LIST VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR NUMBER LIST VAR NUMBER VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
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) | CLASS_DEF FUNC_DEF VAR RETURN FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
def solve(start, visited, memo):
state = start, tuple(visited)
if start >= len(s):
return 0
if state in memo:
return memo[state]
result = 0
for idx in range(start, len(s)):
cut_str = s[start : idx + 1]
pos = get_position(cut_str)
if visited[pos]:
continue
visited[pos] = True
result = max(result, 1 + solve(idx + 1, visited, memo))
visited[pos] = False
memo[state] = result
return result
def get_position(string):
return self.sub_str_pos[string]
self.sub_str_pos = {}
for i in range(len(s)):
for j in range(i, len(s)):
sub_str = s[i : j + 1]
if sub_str not in self.sub_str_pos:
self.sub_str_pos[sub_str] = len(self.sub_str_pos)
return solve(0, [(False) for _ in range(len(self.sub_str_pos))], {}) | CLASS_DEF FUNC_DEF VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF RETURN VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR DICT VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
c = collections.Counter(s)
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)
cand = [s[0]]
for i, x in enumerate(bit):
if x == "0":
cand[-1] += s[i + 1]
else:
cand.append(s[i + 1])
nodup = set(cand)
if len(nodup) == len(cand):
res = max(res, len(cand))
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
1 <= s.length <= 16
s contains only lower case English letters. | class Solution:
def maxUniqueSplit(self, s: str) -> int:
max_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 | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.