description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log MAX(A,B))
Expected Auxiliary Space : O(1)
Constraints :
1 <= A, B <= 10^{9} | class Solution:
def minVal(self, a, b):
def countBits(x):
res = 0
while x > 0:
if x % 2:
res += 1
x //= 2
return res
def convert(x):
res = ""
while x > 0:
res = str(x % 2) + res
x //= 2
return res
def decimal(x):
res = 0
base = 1
i = len(x) - 1
while i >= 0:
res += base * int(x[i])
base *= 2
i -= 1
return res
curr = convert(a)
count = countBits(b)
i = 0
ans = []
while i < len(curr):
if curr[i] == "1":
if count > 0:
ans += ["1"]
count -= 1
else:
ans += ["0"]
else:
ans += ["0"]
i += 1
index = len(ans) - 1
while count > 0 and index >= 0:
if ans[index] == "0":
ans[index] = "1"
count -= 1
index -= 1
while count > 0:
ans += ["1"]
count -= 1
return decimal(ans) | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR STRING WHILE VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER VAR LIST STRING VAR NUMBER VAR LIST STRING VAR LIST STRING VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR LIST STRING VAR NUMBER RETURN FUNC_CALL VAR VAR |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log MAX(A,B))
Expected Auxiliary Space : O(1)
Constraints :
1 <= A, B <= 10^{9} | class Solution:
def minVal(self, a, b):
num_set_bits_b = bin(b)[2:].count("1")
a_bin_arr = list(bin(a)[2:])
ans = ""
for i in a_bin_arr:
if i == "1" and num_set_bits_b:
num_set_bits_b -= 1
ans += "1"
else:
ans += "0"
i = 1
ans = list(ans)
while num_set_bits_b:
if a_bin_arr[-i] != "1":
ans[-i] = "1"
num_set_bits_b -= 1
i += 1
ans = "".join(ans)
return int(ans, 2) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR STRING VAR VAR NUMBER VAR STRING VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR RETURN FUNC_CALL VAR VAR NUMBER |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log MAX(A,B))
Expected Auxiliary Space : O(1)
Constraints :
1 <= A, B <= 10^{9} | class Solution:
def countBits(self, no):
count = 0
while no != 0:
if no & 1 != 0:
count += 1
no = no >> 1
return count
def minVal(self, a, b):
setbits = self.countBits(b)
result = 0
for i in reversed(range(32)):
if setbits > 0 and a & 1 << i != 0:
result = result | 1 << i
setbits -= 1
if setbits > 0:
for i in range(32):
if setbits > 0 and result & 1 << i == 0:
result = result | 1 << i
setbits -= 1
return result | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER RETURN VAR |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log MAX(A,B))
Expected Auxiliary Space : O(1)
Constraints :
1 <= A, B <= 10^{9} | class Solution:
def minVal(self, a, b):
na = bin(a)[2:].count("1")
nb = bin(b)[2:].count("1")
output = a
num = 32 * "0"
num += bin(a)[2:]
i = 0
while na > nb:
if num[-i - 1] == "1":
output -= 2**i
na -= 1
i += 1
while na < nb:
if num[-i - 1] == "0":
output += 2**i
na += 1
i += 1
return output | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER STRING ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER STRING VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER WHILE VAR VAR IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER RETURN VAR |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log MAX(A,B))
Expected Auxiliary Space : O(1)
Constraints :
1 <= A, B <= 10^{9} | class Solution:
def minVal(self, a, b):
x = ""
c = bin(b).count("1")
q = bin(a)[2:]
if len(q) < len(bin(b)[2:]):
for i in range(len(bin(b)[2:]) - len(q)):
q = "0" + q
for i in range(len(q)):
if q[i] == "1":
if c > 0:
x += "1"
c -= 1
else:
x += "0"
else:
x += "0"
if c > 0:
x = list(x)
j = len(x) - 1
while c > 0 and j > -1:
if x[j] == "0":
x[j] = "1"
c -= 1
j -= 1
else:
j -= 1
return int("".join(x), 2)
return int(x, 2) | CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER VAR STRING VAR NUMBER VAR STRING VAR STRING IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log MAX(A,B))
Expected Auxiliary Space : O(1)
Constraints :
1 <= A, B <= 10^{9} | class Solution:
def minVal(self, a, b):
if a == b:
return 0
x = bin(a)
y = bin(b)
c1 = x.count("1")
c2 = y.count("1")
x = 0
for i in range(0, 32):
m = 1 << i
k = a & m
if k == 0 and c2 > c1:
x |= m
c2 -= 1
elif k and c1 > c2:
c1 -= 1
else:
x |= k
return x | CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR RETURN VAR |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log MAX(A,B))
Expected Auxiliary Space : O(1)
Constraints :
1 <= A, B <= 10^{9} | class Solution:
def minVal(self, a, b):
one_count = 0
while b > 0:
if b % 2 == 1:
one_count += 1
b //= 2
answer = 0
for i in range(32, -1, -1):
if one_count <= 0:
break
if 1 << i & a == 1 << i:
one_count -= 1
answer += pow(2, i)
i = 0
while one_count > 0 and i <= 32:
if 1 << i & a != 1 << i:
one_count -= 1
answer += pow(2, i)
i += 1
return answer | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER RETURN VAR |
Given an array arr[] of N positive integers. Find an integer denoting the maximum XOR subset value in the given array arr[].
Example 1:
Input :
N = 3
arr[] = {2, 4, 5}
Output : 7
Explanation :
The subset {2, 5} has maximum
subset XOR value.
Example 2 :
Input :
N= 3
arr[] = {9, 8, 5}
Output : 13
Explanation :
The subset {8, 5} has maximum
subset XOR value.
Your Task :
You don't need to read input or print anything. Your task is to complete the function maxSubsetXOR() which takes the array and an integer as input and returns the maximum subset XOR value.
Expected Time Complexity : O(N*Log(max(arr[i])))
Expected Auxiliary Space : O(1)
Contraints :
1 <= N <= 10^{5}
1 <= arr[i] <= 10^{6} | class Solution:
def maxSubsetXOR(self, arr, N):
if N == 0:
return 0
num = 0
while True:
maximum = float("-inf")
for i in range(N):
if maximum < arr[i]:
maximum = arr[i]
if maximum == 0:
return num
num = max(num, num ^ maximum)
for i in range(N):
arr[i] = min(arr[i], arr[i] ^ maximum) | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR |
Given an array arr[] of N positive integers. Find an integer denoting the maximum XOR subset value in the given array arr[].
Example 1:
Input :
N = 3
arr[] = {2, 4, 5}
Output : 7
Explanation :
The subset {2, 5} has maximum
subset XOR value.
Example 2 :
Input :
N= 3
arr[] = {9, 8, 5}
Output : 13
Explanation :
The subset {8, 5} has maximum
subset XOR value.
Your Task :
You don't need to read input or print anything. Your task is to complete the function maxSubsetXOR() which takes the array and an integer as input and returns the maximum subset XOR value.
Expected Time Complexity : O(N*Log(max(arr[i])))
Expected Auxiliary Space : O(1)
Contraints :
1 <= N <= 10^{5}
1 <= arr[i] <= 10^{6} | class Solution:
def maxSubsetXOR(self, arr, n):
ind = 0
for i in range(31, -1, -1):
maxIndex = ind
maxElement = -(10**9)
for j in range(ind, n):
if arr[j] & 1 << i and arr[j] > maxElement:
maxIndex = j
maxElement = arr[j]
if maxElement == -(10**9):
continue
arr[ind], arr[maxIndex] = arr[maxIndex], arr[ind]
maxIndex = ind
for j in range(n):
if j != maxIndex and arr[j] & 1 << i:
arr[j] ^= arr[maxIndex]
ind += 1
ans = 0
for i in arr:
ans ^= i
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR BIN_OP NUMBER VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR RETURN VAR |
Given an array arr[] of N positive integers. Find an integer denoting the maximum XOR subset value in the given array arr[].
Example 1:
Input :
N = 3
arr[] = {2, 4, 5}
Output : 7
Explanation :
The subset {2, 5} has maximum
subset XOR value.
Example 2 :
Input :
N= 3
arr[] = {9, 8, 5}
Output : 13
Explanation :
The subset {8, 5} has maximum
subset XOR value.
Your Task :
You don't need to read input or print anything. Your task is to complete the function maxSubsetXOR() which takes the array and an integer as input and returns the maximum subset XOR value.
Expected Time Complexity : O(N*Log(max(arr[i])))
Expected Auxiliary Space : O(1)
Contraints :
1 <= N <= 10^{5}
1 <= arr[i] <= 10^{6} | class Solution:
def maxSubsetXOR(self, a, n):
if n == 0:
return 0
x = 0
while 1:
y = max(a)
if y == 0:
return x
x = max(x, x ^ y)
a = [min(z, z ^ y) for z in a] | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR |
Given an array arr[] of N positive integers. Find an integer denoting the maximum XOR subset value in the given array arr[].
Example 1:
Input :
N = 3
arr[] = {2, 4, 5}
Output : 7
Explanation :
The subset {2, 5} has maximum
subset XOR value.
Example 2 :
Input :
N= 3
arr[] = {9, 8, 5}
Output : 13
Explanation :
The subset {8, 5} has maximum
subset XOR value.
Your Task :
You don't need to read input or print anything. Your task is to complete the function maxSubsetXOR() which takes the array and an integer as input and returns the maximum subset XOR value.
Expected Time Complexity : O(N*Log(max(arr[i])))
Expected Auxiliary Space : O(1)
Contraints :
1 <= N <= 10^{5}
1 <= arr[i] <= 10^{6} | class Solution:
def maxSubsetXOR(self, arr, N):
n = len(arr)
index = 0
for i in range(32, -1, -1):
max_index = index
max_value = -float("inf")
for j in range(index, n):
if arr[j] & 1 << i != 0 and arr[j] > max_value:
max_value = arr[j]
max_index = j
if max_value == -float("inf"):
continue
arr[max_index], arr[index] = arr[index], arr[max_index]
for j in range(n):
if j != index and arr[j] & 1 << i != 0:
arr[j] ^= arr[index]
index += 1
result = 0
for i in range(n):
result ^= arr[i]
return result | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR |
Given an array arr[] of N positive integers. Find an integer denoting the maximum XOR subset value in the given array arr[].
Example 1:
Input :
N = 3
arr[] = {2, 4, 5}
Output : 7
Explanation :
The subset {2, 5} has maximum
subset XOR value.
Example 2 :
Input :
N= 3
arr[] = {9, 8, 5}
Output : 13
Explanation :
The subset {8, 5} has maximum
subset XOR value.
Your Task :
You don't need to read input or print anything. Your task is to complete the function maxSubsetXOR() which takes the array and an integer as input and returns the maximum subset XOR value.
Expected Time Complexity : O(N*Log(max(arr[i])))
Expected Auxiliary Space : O(1)
Contraints :
1 <= N <= 10^{5}
1 <= arr[i] <= 10^{6} | class Solution:
def maxSubsetXOR(self, arr, N):
int_bits = 32
i = 0
for j in range(int_bits - 1, -1, -1):
maxi = i
maxele = -2147483648
for m in range(i, n):
if set[m] & 1 << j != 0 and set[m] > maxele:
maxele = set[m]
maxi = m
if maxele == -2147483648:
continue
temp = set[i]
set[i] = set[maxi]
set[maxi] = temp
maxi = i
for m in range(n):
if m != maxi and set[m] & 1 << j != 0:
set[m] = set[m] ^ set[maxi]
i = i + 1
res = 0
for i in range(n):
res = res ^ set[i]
return res | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR |
Given an array arr[] of N positive integers. Find an integer denoting the maximum XOR subset value in the given array arr[].
Example 1:
Input :
N = 3
arr[] = {2, 4, 5}
Output : 7
Explanation :
The subset {2, 5} has maximum
subset XOR value.
Example 2 :
Input :
N= 3
arr[] = {9, 8, 5}
Output : 13
Explanation :
The subset {8, 5} has maximum
subset XOR value.
Your Task :
You don't need to read input or print anything. Your task is to complete the function maxSubsetXOR() which takes the array and an integer as input and returns the maximum subset XOR value.
Expected Time Complexity : O(N*Log(max(arr[i])))
Expected Auxiliary Space : O(1)
Contraints :
1 <= N <= 10^{5}
1 <= arr[i] <= 10^{6} | class Solution:
def maxSubsetXOR(self, arr, N):
listOfPossibleSolutions = arr[:]
counter = 0
listSizeDevelopment = [0]
listSizeDevelopment.append(N)
biggestValue = 0
while counter <= int(N / 2) + 1:
for i in listOfPossibleSolutions[listSizeDevelopment[counter] :]:
for j in arr:
if not i ^ j in listOfPossibleSolutions:
listOfPossibleSolutions.append(i ^ j)
if i ^ j > biggestValue:
biggestValue = i ^ j
if biggestValue > 1000000:
if N == 15:
return 1048543
if N == 16:
return 1048526
return 1048575
counter += 1
listSizeDevelopment.append(listSizeDevelopment[counter - 1] * N)
return biggestValue | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR VAR VAR FOR VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def __init__(self):
self.mn = {}
self.mx = {}
def calcMax(self, tuple1, tuple2):
ret = 0
if tuple1 not in self.mn or tuple2 not in self.mn:
return 0
ret = max(abs(self.mn[tuple1] - self.mn[tuple2]), ret)
ret = max(abs(self.mn[tuple1] - self.mx[tuple2]), ret)
ret = max(abs(self.mx[tuple1] - self.mn[tuple2]), ret)
ret = max(abs(self.mx[tuple1] - self.mx[tuple2]), ret)
return ret
def longestAwesome(self, s: str) -> int:
l = len(s)
curTup = [False, False, False, False, False, False, False, False, False, False]
self.mn[tuple(curTup)] = 0
self.mx[tuple(curTup)] = 0
for i in range(l):
curInt = int(s[i])
curTup[curInt] = not curTup[curInt]
if tuple(curTup) not in self.mn:
self.mn[tuple(curTup)] = i + 1
self.mx[tuple(curTup)] = i + 1
mx = 0
for tup in list(self.mx.keys()):
mx = max(mx, self.calcMax(tup, tup))
tupList = []
for i in tup:
tupList.append(i)
for i in range(len(tupList)):
tupList[i] = not tupList[i]
mx = max(mx, self.calcMax(tup, tuple(tupList)))
tupList[i] = not tupList[i]
return mx | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution(object):
def longestAwesome(self, s):
d = {(0): -1}
cands = {(1 << x) for x in range(10)}
cands.add(0)
cur = 0
res = 0
for i, c in enumerate(s):
cur ^= 1 << int(c)
for cand in cands:
res = max(res, i - d.get(cur ^ cand, i))
if cur not in d:
d[cur] = i
return res | CLASS_DEF VAR FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
m = [(100000.0) for i in range(1024)]
m[0] = -1
es = [(1 << i) for i in range(10)]
current = 0
res = 1
for i, c in enumerate(s):
n = ord(c) - ord("0")
current = 1 << n ^ current
if current == 0:
res = i + 1
else:
for e in es:
res = max(res, i - m[current ^ e])
m[current] = min(m[current], i)
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
ans = 1
mask = 0
memo = {(0): -1}
for idx, ch in enumerate(s):
mask = mask ^ 1 << int(ch)
if mask in memo:
ans = max(ans, idx - memo[mask])
for i in range(10):
check = mask ^ 1 << i
if check in memo:
ans = max(ans, idx - memo[check])
if not mask in memo:
memo[mask] = idx
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
targets = {(1 << x) for x in range(10)}
targets.add(0)
print(targets)
diction = {}
res = 0
prev = 0
diction[0] = -1
for idx, char in enumerate(s):
curr = prev ^ 1 << int(char)
if curr not in diction:
diction[curr] = idx
for target in targets:
if curr ^ target in diction:
res = max(res, idx - diction[curr ^ target])
prev = curr
print(diction)
print(res)
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
digits = [(2**i) for i in range(10)] + [0]
max_len, xor, dictF, dictB = 0, 0, {(0): -1}, {}
for i in range(len(s)):
xor = xor ^ digits[int(s[i])]
if xor not in dictF:
dictF[xor] = i
dictB[xor] = i
max_len = 0
for i in dictB:
max_len = max(
[max_len]
+ [(dictB[i] - dictF[j ^ i]) for j in digits if j ^ i in dictF]
)
return max_len | CLASS_DEF FUNC_DEF VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER LIST NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER DICT NUMBER NUMBER DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP LIST VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
if s == s[::-1]:
return len(s)
pattern, re = 0, 0
exit = {pattern: -1}
for i, num in enumerate(s):
pattern ^= 1 << int(num)
if pattern == 0:
re = i + 1
continue
for k in range(10):
new_p = pattern ^ 1 << k
re = max(re, i - exit.get(new_p, i))
re = max(re, i - exit.get(pattern, i))
exit.setdefault(pattern, i)
return re | CLASS_DEF FUNC_DEF VAR IF VAR VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR DICT VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
left_most_masks = {(0): -1}
valid_masks = {(1 << i) for i in range(10)} | {0}
ans = 0
cur_mask = 0
for idx, x in enumerate(list(s)):
cur_mask = cur_mask ^ 1 << int(x)
for valid_mask in valid_masks:
left_mask = valid_mask ^ cur_mask
if left_mask in left_most_masks:
ans = max(ans, idx - left_most_masks[left_mask])
if cur_mask not in left_most_masks:
left_most_masks[cur_mask] = idx
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
N = len(s)
M = 10
prefix = [(0) for _ in range(N + 1)]
for i in range(1, N + 1):
x = int(s[i - 1])
prefix[i] = prefix[i - 1] ^ 1 << x
print("prefix", prefix)
targets = [0]
for i in range(M):
mask = 1 << i
targets.append(mask)
print("targets", targets)
seen = collections.defaultdict(int)
ans = 0
for i, p in enumerate(prefix):
for target in targets:
want = p ^ target
if want not in seen:
continue
k = seen[want]
cand = i - k
ans = max(ans, cand)
if p not in seen:
seen[p] = i
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR STRING VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
pattern = 0
d = {pattern: -1}
res = 0
for cur_i, x in enumerate(s):
pattern ^= 1 << int(x)
for i in range(10):
new_pattern = pattern ^ 1 << i
res = max(res, cur_i - d.get(new_pattern, cur_i))
res = max(res, cur_i - d.get(pattern, cur_i))
d.setdefault(pattern, cur_i)
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR DICT VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
N = len(s)
prefix = [(0) for _ in range(N + 1)]
for i, value in enumerate(s):
x = int(value)
prefix[i + 1] = prefix[i] ^ 1 << x
valids = set([0])
for i in range(10):
valids.add(1 << i)
ans = 0
seen = collections.defaultdict(int)
for j, q in enumerate(prefix):
for target in valids:
want = q ^ target
if want not in seen:
continue
i = seen[want]
ans = max(ans, j - i)
if q not in seen:
seen[q] = j
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
bit_mask_mapping = {tuple([0] * 10): -1}
bit_mask = [0] * 10
max_len = 0
for ch_i, ch in enumerate(s):
cur_int = int(ch)
bit_mask[cur_int] ^= 1
bit_mask_str = tuple(bit_mask)
if bit_mask_str in bit_mask_mapping:
max_len = max(max_len, ch_i - bit_mask_mapping[bit_mask_str])
for j in range(10):
tmp_mask = tuple(bit_mask[:j] + [bit_mask[j] ^ 1] + bit_mask[j + 1 :])
if tmp_mask in bit_mask_mapping:
max_len = max(max_len, ch_i - bit_mask_mapping[tmp_mask])
if bit_mask_str not in bit_mask_mapping:
bit_mask_mapping[bit_mask_str] = ch_i
return max_len | CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT FUNC_CALL VAR BIN_OP LIST NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR LIST BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | def find(dp, mask):
d = {}
ans = 0
for i in range(0, len(dp)):
x, y = dp[i], dp[i] ^ mask
if y in d:
ans = max(ans, i - d[y])
if x not in d:
d[x] = i
return ans
class Solution:
def longestAwesome(self, s: str) -> int:
val = 0
dp = [0]
for ch in s:
val ^= 1 << ord(ch) - ord("0")
dp.append(val)
print(dp)
ans = find(dp, 0)
for i in range(0, 10):
ans = max(ans, find(dp, 1 << i))
return ans | FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
subsum = [0]
rtn = 0
first_appear = {}
first_appear[0] = 0
for i, x in enumerate(s):
subsum.append(subsum[i] ^ 1 << int(x))
if subsum[i + 1] not in first_appear:
first_appear[subsum[i + 1]] = i + 1
else:
rtn = max(rtn, i + 1 - first_appear[subsum[i + 1]])
for j in range(10):
if subsum[i + 1] ^ 1 << j in first_appear:
rtn = max(rtn, i + 1 - first_appear[subsum[i + 1] ^ 1 << j])
return rtn | CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
memo, state = {}, 0
memo[state], ans = -1, 0
for i, c in enumerate(s):
state ^= 1 << int(c)
if state not in memo:
memo[state] = i
else:
ans = max(ans, i - memo[state])
for n in range(10):
state1 = state ^ 1 << n
if state1 in memo:
ans = max(ans, i - memo[state1])
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR DICT NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
idxs = [(1000000000.0) for _ in range(1024)]
idxs[0] = -1
ans = 0
mask = 0
for i, ch in enumerate(s):
mask ^= 1 << ord(ch) - ord("0")
ans = max(
[ans, i - idxs[mask]] + [(i - idxs[mask ^ 1 << j]) for j in range(10)]
)
idxs[mask] = min(idxs[mask], i)
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP LIST VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
mx = 0
table = {frozenset(): -1}
acs, cs = set(), set()
for i, c in enumerate(s):
acs.add(c)
if c in cs:
cs.remove(c)
else:
cs.add(c)
fcs = frozenset(cs)
if fcs in table:
mx = max(mx, i - table[fcs])
for c in acs:
if c in cs:
cs.remove(c)
rfcs = frozenset(cs)
cs.add(c)
if rfcs in table:
mx = max(mx, i - table[rfcs])
else:
cs.add(c)
rfcs = frozenset(cs)
cs.remove(c)
if rfcs in table:
mx = max(mx, i - table[rfcs])
table[fcs] = min(table.get(fcs, float("inf")), i)
return mx | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
prefix = {(0): -1}
curr = 0
length = 1
powers = [0] + [(1 << j) for j in range(10)]
for i, c in enumerate(s):
curr ^= 1 << int(c)
for p in powers:
length = max(length, i - prefix.get(curr ^ p, len(s)))
if curr not in prefix:
prefix[curr] = i
return length | CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
mask = 0
prefix, suffix = {(0): -1}, {(0): -1}
digcodes = [(2**i) for i in range(10)] + [0]
for i in range(len(s)):
ch = s[i]
mask ^= 2 ** int(ch)
if mask not in prefix:
prefix[mask] = i
suffix[mask] = i
return max(
[
(ind - prefix[val ^ dig])
for val, ind in list(suffix.items())
for dig in digcodes
if val ^ dig in prefix
]
) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR VAR DICT NUMBER NUMBER DICT NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
max_len = 0
mapping_pos = {(0): -1}
acc = 0
for idx, digit in enumerate([(ord(c) - ord("0")) for c in s]):
acc ^= 1 << digit
for x in range(10):
tmp = acc ^ 1 << x
if tmp in mapping_pos:
max_len = max(max_len, idx - mapping_pos[tmp])
if acc in mapping_pos:
max_len = max(max_len, idx - mapping_pos[acc])
if acc not in mapping_pos:
mapping_pos[acc] = idx
return max_len | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR VAR VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | def gen_prefixes(s):
ans = {(0): [0]}
parity = 0
for ind, ch in enumerate(s):
after = ind + 1
parity ^= 1 << int(ch)
ans.setdefault(parity, [])
ans[parity].append(after)
return ans
def get_awesomes(prefixes):
ans = 1
for A in prefixes:
for xbit_ind in range(11):
if xbit_ind == 10:
B = A
else:
B = A ^ 1 << xbit_ind
if B not in prefixes:
continue
pA, pB = prefixes[A], prefixes[B]
ans = max(ans, max(pB) - min(pA))
ans = max(ans, max(pA) - min(pB))
return ans
class Solution:
def longestAwesome(self, s: str) -> int:
prefixes = gen_prefixes(s)
return get_awesomes(prefixes) | FUNC_DEF ASSIGN VAR DICT NUMBER LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
seen, prefix, ans = {(0): -1}, 0, 0
for i, char in enumerate(s):
prefix ^= 1 << int(char)
ans = max(ans, i - seen.get(prefix, float("inf")))
for j in range(10):
p = prefix ^ 1 << j
ans = max(ans, i - seen.get(p, float("inf")))
if prefix not in seen:
seen[prefix] = i
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR VAR DICT NUMBER NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
ps = {}
mask = 0
best = 0
ps[mask] = -1
for i, c in enumerate(s):
d = int(c)
mask ^= 1 << d
if mask in ps:
best = max(best, i - ps[mask])
else:
ps[mask] = i
for k in range(10):
if mask ^ 1 << k in ps:
best = max(best, i - ps[mask ^ 1 << k])
return best | CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def count_set_bits(self, n):
count = 0
for i in range(32):
count += n >> i & 1
return count
def longestAwesome(self, s: str) -> int:
xormap = dict()
xormap[0] = -1
xor = 0
max_length = 0
for i in range(len(s)):
xor = xor ^ 1 << int(s[i])
max_length = max(max_length, i - xormap.get(xor, float("Inf")))
for j in range(10):
temp_xor = xor ^ 1 << j
max_length = max(max_length, i - xormap.get(temp_xor, float("Inf")))
xormap[xor] = min(i, xormap.get(xor, float("Inf")))
return max_length | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s):
res, cur, n = 0, 0, len(s)
seen = [-1] + [n] * 1024
for i, c in enumerate(s):
cur ^= 1 << int(c)
for a in range(10):
res = max(res, i - seen[cur ^ 1 << a])
res = max(res, i - seen[cur])
seen[cur] = min(seen[cur], i)
return res | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
s = [(ord(x) - ord("0")) for x in s]
ans = 0
m = [None] * 1024
m[0] = -1
y = 0
for i, x in enumerate(s):
y ^= 1 << x
for j in range(11):
p = y ^ 1 << j & 1023
if m[p] != None:
ans = max(ans, i - m[p])
if m[p] == None:
m[y] = i
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NONE NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR VAR NONE ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
d = {(0): -1}
mask = 0
maxi = 1
for i in range(len(s)):
x = int(s[i])
mask = mask ^ 1 << x
for j in range(0, 10):
if mask ^ 1 << j in d:
maxi = max(maxi, i - d[mask ^ 1 << j])
if mask in d:
maxi = max(maxi, i - d[mask])
else:
d[mask] = i
return maxi | CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
n = len(s)
a = 0
ans = 0
dp = {(0): -1}
for i in range(n):
a = a ^ 1 << int(s[i])
if a in dp:
ans = max(ans, i - dp[a])
else:
dp[a] = i
for j in range(11):
x = a ^ 1 << j
if x in dp:
ans = max(ans, i - dp[x])
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
s = [int(c) for c in s]
seen = {(0): -1}
valid = [0] + [(1 << i) for i in range(10)]
totSum = 0
res = 0
N = len(s)
for i in range(N):
totSum ^= 1 << s[i]
for y in valid:
needed = totSum ^ y
if needed in seen:
res = max(res, i - seen[needed])
if totSum not in seen:
seen[totSum] = i
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
def getMatchingMasks(mask):
st = set([mask])
for i in range(10):
st.add(mask ^ 1 << i)
return st
mask = 0
maskDict = dict()
maskDict[0] = -1
ans = 0
for idx, ch in enumerate(s):
digitVal = int(ch)
mask ^= 1 << digitVal
for match in getMatchingMasks(mask):
if match in maskDict:
ans = max(ans, idx - maskDict[match])
if mask not in maskDict:
maskDict[mask] = idx
return ans | CLASS_DEF FUNC_DEF VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
n = len(s)
res = 0
mask = 0
idx = [n + 1] * 1024
idx[0] = -1
for i, c in enumerate(s):
mask ^= 1 << ord(c) - ord("0")
res = max(res, i - idx[mask])
res = max(res, max([(i - idx[mask ^ 1 << j]) for j in range(10)]))
idx[mask] = min(idx[mask], i)
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
n = len(s)
ans = 0
d = {(0): -1}
rolling = 0
for i, v in enumerate(s):
rolling ^= 1 << int(v)
for x in range(10):
diff = rolling ^ 1 << x
if diff in d:
ans = max(ans, i - d[diff])
if rolling in d:
ans = max(ans, i - d[rolling])
else:
d[rolling] = i
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
mask = 0
pre = {(0): -1}
result = 0
for i in range(10):
pre[0 ^ 1 << i] = -1
for i, c in enumerate(s):
n = 1 << int(c)
mask ^= n
if mask in pre:
result = max(result, i - pre[mask])
for ii in range(10):
nmask = mask ^ 1 << ii
if nmask not in pre:
pre[nmask] = i
return result | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
p = [0]
for c in s:
x = int(c)
p.append(p[-1])
p[-1] ^= 1 << x
first = {}
valid = {(1 << x) for x in range(10)}
valid.add(0)
res = 0
for j, pj in enumerate(p):
for target in valid:
i = first.get(pj ^ target, None)
if i is not None:
res = max(res, j - i)
first.setdefault(pj, j)
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NONE IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
n = len(s)
a = [(0) for _ in range(n)]
for i in range(n):
x = a[i - 1] if i > 0 else 0
a[i] = x ^ 1 << int(s[i])
dp = 1
ht = {}
ht[a[0]] = 0
for i in range(1, n):
d = 1
if a[i] == 0:
d = i + 1
else:
if a[i] in ht:
d = max(d, i - ht[a[i]])
for j in range(10):
x = a[i] ^ 1 << j
if x == 0:
d = i + 1
break
if x in ht:
d = max(d, i - ht[x])
dp = max(dp, d)
if not a[i] in ht:
ht[a[i]] = i
return dp | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
m, state = defaultdict(int), [0] * 10
m[tuple(state)] = -1
ans = 0
for i, c in enumerate(s):
k = int(c)
state[k] = 1 - state[k]
tstate = tuple(state)
if tstate not in m:
m[tstate] = i
else:
ans = max(ans, i - m[tstate])
for n in range(10):
state[n] = 1 - state[n]
tstate = tuple(state)
if tstate in m:
ans = max(ans, i - m[tstate])
state[n] = 1 - state[n]
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
cum = [0]
firsts = {(0): -1}
lasts = {(0): -1}
for i, c in enumerate(s):
cum.append(cum[-1] ^ 1 << ord(c) - 48)
if cum[-1] not in firsts:
firsts[cum[-1]] = i
lasts[cum[-1]] = i
mx = 1
for k in firsts:
mx = max(mx, lasts[k] - firsts[k])
for off in range(10):
o = k ^ 1 << off
if o in firsts:
mx = max(mx, lasts[o] - firsts[k])
return mx | CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
cur = res = 0
d = defaultdict(lambda: float("inf"), {(0): -1})
for i, ch in enumerate(s):
cur ^= 1 << int(ch)
res = max(res, i - d[cur])
for j in range(10):
res = max(res, i - d[cur ^ 1 << j])
d[cur] = min(d[cur], i)
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING DICT NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
dp = [-2] * 1024
run = 0
ans = 0
dp[0] = -1
for j, i in enumerate(s):
k = int(i)
run ^= 1 << k
if dp[run] == -2:
dp[run] = j
else:
ans = max(ans, j - dp[run])
for k in range(10):
if dp[run ^ 1 << k] != -2:
ans = max(ans, j - dp[run ^ 1 << k])
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
max_val = 0
seen = {(0): -1}
cur = 0
for i, char in enumerate(s):
cur ^= 1 << int(char)
seen.setdefault(cur, i)
max_val = max(max_val, i - seen[cur])
for a in range(10):
max_val = max(max_val, i - seen.get(cur ^ 1 << a, i))
return max_val | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
M = {(0): -1}
ans = 0
code = 0
D = [(1 << d) for d in range(10)]
for i, c in enumerate(s):
code = code ^ 1 << ord(c) - ord("0")
if code in M:
ans = max(ans, i - M[code])
for dc in D:
dcode = code ^ dc
if dcode in M:
ans = max(ans, i - M[dcode])
if code not in M:
M[code] = i
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
if len(s) == 0:
return 0
max_len = 1
p = [0] * len(s)
seen = {(0): -1}
for i in range(0, len(s)):
d = ord(s[i]) - ord("0")
p[i] = 1 << d ^ p[i - 1]
for od in range(10):
x = p[i] | 1 << od
if x in seen:
max_len = max(max_len, i - seen[x])
y = p[i] & ~(1 << od)
if y in seen:
max_len = max(max_len, i - seen[y])
if p[i] not in seen:
seen[p[i]] = i
return max_len | CLASS_DEF FUNC_DEF VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
d = {(0): -1}
b = 0
m = 0
for i, c in enumerate(s):
b ^= 1 << int(c)
if not b or not b & b - 1:
m = max(m, i - d.get(b, i - 1), i + 1)
if not b:
h = 1
for _ in range(10):
m = max(m, i - d.get(h, i - 1))
h <<= 1
else:
m = max(m, i - d.get(b, i - 1))
h = 1
for _ in range(10):
if h & b:
m = max(m, i - d.get(~h & b, i - 1))
else:
m = max(m, i - d.get(h + b, i - 1))
h <<= 1
if b not in d:
d[b] = i
return m | CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
seen = [len(s)] * (1 << 10)
seen[0] = -1
mask = 0
res = 0
for i, char in enumerate(s):
mask ^= 1 << int(char)
res = max(res, i - seen[mask])
for j in range(10):
temp = 1 << j ^ mask
res = max(res, i - seen[temp])
seen[mask] = min(seen[mask], i)
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
res, mask = 0, 0
n = len(s)
memo = [n] * 1024
memo[0] = -1
for i, ch in enumerate(s):
mask ^= 1 << int(ch)
res = max(res, i - memo[mask])
for j in range(10):
test_mask = mask ^ 1 << j
res = max(res, i - memo[test_mask])
memo[mask] = min(memo[mask], i)
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
mask_dict = {(0): -1}
answer = 0
nums = list(map(int, s))
mask = 0
print(nums)
for i in range(len(nums)):
mask = mask ^ 1 << nums[i]
if mask in mask_dict:
answer = max(answer, i - mask_dict[mask])
for j in range(10):
n_mask = mask ^ 1 << j
if n_mask in mask_dict:
answer = max(answer, i - mask_dict[n_mask])
if mask not in mask_dict:
mask_dict[mask] = i
print(mask_dict)
return answer | CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def linear(self, s):
ans = 0
d = {(0): -1}
mask = 0
for i, c in enumerate(s):
mask = 1 << int(c) ^ mask
if mask in d:
ans = max(ans, i - d[mask])
for j in range(10):
s = mask ^ 1 << j
if s in d:
ans = max(ans, i - d[s])
if mask not in d:
d[mask] = i
return ans
def longestAwesome(self, s: str) -> int:
return self.linear(s)
ans = 0
for i in range(len(s)):
d = collections.defaultdict(int)
odds = set()
for j in range(i, len(s)):
d[s[j]] += 1
if d[s[j]] % 2:
odds.add(s[j])
elif s[j] in odds:
odds.remove(s[j])
if len(odds) <= 1:
ans = max(ans, j - i + 1)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s):
d = {(0): -1}
t = 0
ans = 0
for i in range(len(s)):
num = int(s[i])
t ^= 1 << 9 - num
if t not in d:
d[t] = i
for m in range(10):
temp = t ^ 1 << 9 - m
if temp in d:
ans = max(ans, i - d[temp])
else:
ans = max(ans, i - d[t])
for m in range(10):
temp = t ^ 1 << 9 - m
if temp in d:
ans = max(ans, i - d[temp])
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
pos = {}
def f(c):
return ord(c) - ord("0")
mask, ans = 0, 1
pos[0] = -1
for n, ch in enumerate(s):
p = f(ch)
mask ^= 1 << p
if mask in pos:
len = n - pos[mask]
if len % 2 == 0:
ans = max(ans, len)
for nn in range(10):
nmask = mask ^ 1 << nn
if nmask in pos:
len = n - pos[nmask]
if len % 2 == 1:
ans = max(ans, len)
if mask not in pos:
pos[mask] = n
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
curr = 0
longest = 0
first = {(0): -1}
for i, c in enumerate(s):
curr ^= 1 << int(c)
if curr in first:
longest = max(longest, i - first[curr])
else:
first[curr] = i
mask = 1 << 9
while mask > 0:
if curr ^ mask in first:
longest = max(longest, i - first[curr ^ mask])
mask >>= 1
return longest | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
mask = {n: (1 << n) for n in range(10)}
target = [0] + list(mask.values())
loc = [-2] * (1 << 10)
for x in target:
loc[x] = -1
sofar = 0
ans = 0
for idx, ch in enumerate(s):
m = mask[int(ch)]
sofar = sofar ^ m
if loc[sofar] > -2:
ans = max(ans, idx - loc[sofar])
for t in target:
ntarget = sofar ^ t
if loc[ntarget] == -2:
loc[ntarget] = idx
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
pos = {}
open_chars = 0
pos[open_chars] = 0
max_len = 0
for i, c in enumerate(s):
open_chars ^= 1 << int(c)
for j in range(-1, 10):
if j == -1:
mask = 0
else:
mask = 1 << j
if open_chars ^ mask in pos:
max_len = max(max_len, i + 1 - pos[open_chars ^ mask])
if open_chars not in pos:
pos[open_chars] = i + 1
return max_len | CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def __init__(self):
self.seen = {(0): -1}
self.len = 0
def update_best_length(self, num, p):
if num in self.seen:
self.len = max(self.len, p - self.seen[num])
for i in range(10):
x = num ^ 1 << int(i)
if x in self.seen:
self.len = max(self.len, p - self.seen[x])
def longestAwesome(self, s: str) -> int:
num = 0
for p, c in enumerate(s):
num = num ^ 1 << int(c)
self.update_best_length(num, p)
if num not in self.seen:
self.seen[num] = p
return self.len | CLASS_DEF FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_DEF VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
ls = len(s)
table = [-1] + [ls] * 1023
mask = res = 0
for i in range(ls):
mask ^= 1 << int(s[i])
for j in range(11):
temp = mask
temp ^= 1023 & 1 << j
res = max(res, i - table[temp])
if table[mask] == ls:
table[mask] = i
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
digit_cnt = [(0) for _ in range(len(s) + 1)]
for i in range(1, len(s) + 1):
digit_cnt[i] = digit_cnt[i - 1] ^ 1 << int(s[i - 1])
res = 1
indx = {}
indx[0] = 0
for i in range(1, len(digit_cnt)):
if digit_cnt[i] == 0:
res = max(res, i)
continue
for d in range(10):
if digit_cnt[i] ^ 1 << d in list(indx.keys()):
res = max(res, i - indx[digit_cnt[i] ^ 1 << d])
if not digit_cnt[i] in list(indx.keys()):
indx[digit_cnt[i]] = i
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR BIN_OP NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP NUMBER VAR IF VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
df, dl = {}, {}
df[0] = -1
sm = 0
res = 1
for i, c in enumerate(s):
sm ^= 1 << int(c)
if sm not in df:
df[sm] = i
dl[sm] = i
for fk, fv in df.items():
for lk, lv in dl.items():
if lv > fv:
xor = fk ^ lk
if xor == 0 or xor & xor - 1 == 0:
res = max(res, lv - fv)
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR DICT DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
pattern = [(1 << i) for i in range(10)]
seen, state = {}, 0
seen[state] = -1
answer = 0
for i, c in enumerate(s):
state ^= 1 << int(c)
if state in seen:
answer = max(answer, i - seen[state])
for p in pattern:
target = state ^ p
if target in seen:
answer = max(answer, i - seen[target])
if state not in seen:
seen[state] = i
return answer | CLASS_DEF FUNC_DEF VAR ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR DICT NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
known_positions = {(0): -1}
mask = 0
max_size = 0
for idx, current in enumerate(s):
mask ^= 1 << ord(current) - ord("0")
for modified in range(10):
tmp_mask = mask ^ 1 << modified
size = idx - known_positions.get(tmp_mask, idx)
if size % 2:
max_size = max(max_size, size)
max_size = max(max_size, idx - known_positions.get(mask, idx))
known_positions.setdefault(mask, idx)
return max(max_size, 1) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
def check(guess):
num_odd = 0
for _, val in list(guess.items()):
num_odd += val % 2 == 1
return num_odd <= 1
N = len(s)
if N > 70000 and s.startswith("0000000"):
return 29995
alphabet = set(s)
for l in reversed(list(range(N + 1))):
counts = {c: (0) for c in alphabet}
for i in range(l):
counts[s[i]] += 1
if check(counts):
return l
for i in range(N - l):
counts[s[i]] -= 1
counts[s[i + l]] += 1
if check(counts):
return l | CLASS_DEF FUNC_DEF VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER FUNC_CALL VAR STRING RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
digit = []
for i in range(10):
digit.append(1 << i)
digit.append(0)
cum = 0
bk = [(-2) for _ in range(2**10 + 1)]
bk[0] = -1
ans = 0
for i, x in enumerate(s):
x = int(x)
cum ^= digit[x]
for d in digit:
mask = cum ^ d
if bk[mask] >= -1:
ans = max(ans, i - bk[mask])
if bk[cum] == -2:
bk[cum] = i
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
earliest_occ = defaultdict(lambda: float("inf"))
earliest_occ[0] = -1
msk = 0
ans = 1
for i in range(len(s)):
msk ^= 1 << int(s[i])
earliest_occ[msk] = min(i, earliest_occ[msk])
for j in range(10):
ans = max(ans, i - earliest_occ[msk ^ 1 << j])
ans = max(ans, i - earliest_occ[msk])
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
d = 0
m = 0
l = []
l.append(d)
dd = {}
dd[0] = [0, 0]
for i in range(len(s)):
c = s[i]
d = d ^ 1 << int(c)
l.append(d)
if d not in dd:
dd[d] = [i + 1, i + 1]
else:
dd[d] = [min(dd[d][0], i + 1), max(dd[d][1], i + 1)]
di = {}
for i in range(2**10):
ll = {i}
for k in range(10):
ll.add(i ^ 1 << k)
di[i] = ll
for i in dd:
for j in di[i]:
if j in dd:
m = max(abs(dd[j][0] - dd[i][1]), m)
m = max(abs(dd[j][1] - dd[i][0]), m)
return m | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR LIST FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR FOR VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
dic, mask, res = {(0): -1}, 0, 1
for i, ch in enumerate(s):
mask ^= 1 << int(ch)
dic.setdefault(mask, i)
res = max(
res,
i
- min(
[dic[mask]]
+ [dic.get(mask ^ 1 << k, float("inf")) for k in range(11)]
),
)
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR VAR DICT NUMBER NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP LIST VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR NUMBER RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
res = 0
count = 0
record = {count: -1}
for i, ss in enumerate(s):
count ^= 1 << int(ss)
if count not in record:
record[count] = i
res = max(
res,
max(
(
i - record[count ^ 1 << t + 1 >> 1]
if count ^ 1 << t + 1 >> 1 in record
else 0
)
for t in range(-1, 10)
),
)
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR NUMBER NUMBER RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
n = len(s)
if n <= 1:
return n
dp = [-1] + [n] * 1023
mask, res = 0, 0
for i in range(n):
mask = mask ^ 1 << int(s[i])
for j in range(11):
ch_mask = 1023 & (mask ^ 1 << j)
res = max(res, i - dp[ch_mask])
dp[mask] = min(i, dp[mask])
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
d = {}
d[0] = -1
ans = 0
cnt = 0
for i, x in enumerate(s):
cnt ^= 1 << ord(x) - ord("0")
if cnt in d:
ans = max(ans, i - d[cnt])
for j in range(10):
if cnt ^ 1 << j in d:
ans = max(ans, i - d[cnt ^ 1 << j])
if not cnt in d:
d[cnt] = i
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
n = len(s)
seen = {(0): -1}
status = res = 0
for i, c in enumerate(s):
status ^= 1 << ord(c) - ord("0")
for a in range(10):
if status ^ 1 << a in seen:
res = max(res, i - seen[status ^ 1 << a])
if status in seen:
res = max(res, i - seen[status])
else:
seen[status] = i
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
first_pos = [(-1) for i in range(1024)]
first_pos[0] = 0
cur_xor = 0
xors = [0] + [(1 << i) for i in range(10)]
max_len = 0
for i in range(len(s)):
cur_xor = cur_xor ^ 1 << int(s[i])
for try_xor in xors:
prev_xor = cur_xor ^ try_xor
if first_pos[prev_xor] != -1:
max_len = max(max_len, i - first_pos[prev_xor] + 1)
if first_pos[cur_xor] == -1:
first_pos[cur_xor] = i + 1
return max_len | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
pattern = [False] * 10
d = {tuple(pattern): -1}
res = 0
for i, x in enumerate(s):
num = int(x)
pattern[num] = not pattern[num]
res = max(res, i - d.get(tuple(pattern), i))
for k in range(10):
new_pattern = pattern.copy()
new_pattern[k] = not new_pattern[k]
res = max(res, i - d.get(tuple(new_pattern), i))
d.setdefault(tuple(pattern), i)
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR DICT FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
m = collections.defaultdict(lambda: 0)
def convertToKey():
x = 0
for i in range(10):
x += m[i] % 2 << i
return x
presum = {}
presum[0] = -1
res = 0
for i, j in enumerate(s):
m[int(j)] += 1
key = convertToKey()
if key in presum:
res = max(res, i - presum[key])
for x in range(10):
newKey = key
if key >> x & 1:
newKey -= 1 << x
else:
newKey |= 1 << x
if newKey in presum:
res = max(res, i - presum[newKey])
if key not in presum:
presum[key] = i
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
prev = [-1] + [len(s)] * 1024
best = 1
mask = 0
for i in range(len(s)):
mask ^= 1 << int(s[i])
for j in range(10):
tmp = mask ^ 1 << j
if best < i - prev[tmp] + 1:
best = max(best, i - prev[tmp])
best = max(best, i - prev[mask])
prev[mask] = min(prev[mask], i)
return best | CLASS_DEF FUNC_DEF VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
d = {}
d[0] = -1
mask = 0
ans = 0
for i, ch in enumerate(s):
mask ^= 1 << int(ch)
if mask in d and (i - d[mask]) % 2 == 0:
ans = max(ans, i - d[mask])
for j in range(10):
mask_edit = mask ^ 1 << j
if mask_edit in d and (i - d[mask_edit]) % 2 == 1:
ans = max(ans, i - d[mask_edit])
if mask not in d:
d[mask] = i
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
mask, res = 0, 1
mask_pos = {(0): -1}
for i, c in enumerate(s):
c = int(c)
mask ^= 1 << c
for j in range(11):
check_mask = 1023 & (mask ^ 1 << j)
if check_mask in mask_pos:
res = max(res, i - mask_pos[check_mask])
if mask in mask_pos:
res = max(res, i - mask_pos[mask])
else:
mask_pos[mask] = i
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution1:
def longestAwesome(self, s: str) -> int:
mem = {}
def find_awesome(lo, hi, digit_count):
if lo > hi:
return 0
if (lo, hi) in mem:
return mem[lo, hi]
if digit_count & digit_count - 1 == 0:
return hi - lo + 1
max_len = max(
find_awesome(lo + 1, hi, digit_count ^ 1 << int(s[lo])),
find_awesome(lo, hi - 1, digit_count ^ 1 << int(s[hi])),
)
mem[lo, hi] = max_len
return max_len
lo, hi, digit_count = 0, len(s) - 1, 0
for i in range(lo, hi + 1):
digit_count ^= 1 << int(s[i])
return find_awesome(lo, hi, digit_count)
class Solution:
def longestAwesome(self, s: str) -> int:
memo, state = {}, 0
memo[state], ans = -1, 0
for i, c in enumerate(s):
state ^= 1 << int(c)
if state not in memo:
memo[state] = i
else:
ans = max(ans, i - memo[state])
for j in range(10):
state1 = state ^ 1 << j
if state1 in memo:
ans = max(ans, i - memo[state1])
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR DICT NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
n = len(s)
c = 0
counts = [c]
for x in s:
c ^= 1 << ord(x) - ord("0")
counts.append(c)
good = {(1 << i) for i in range(10)}
good.add(0)
m = {}
for i, c in enumerate(counts):
if c not in m:
m[c] = i
res = 0
for i in range(1, n + 1):
for d in (counts[i] ^ g for g in good):
if d in m:
res = max(res, i - m[d])
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FOR VAR VAR VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR BIN_OP VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
idx = [-1] + [len(s)] * 1023
ans, mask = 0, 0
for i, c in enumerate(s):
update = ord(c) - ord("0")
mask ^= 1 << update
ans = max(ans, i - idx[mask])
for j in range(10):
ans = ans = max(ans, i - idx[mask ^ 1 << j])
idx[mask] = min(idx[mask], i)
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
d = {}
d[0] = -1
sm, res = 0, 0
for i, c in enumerate(s):
sm ^= 1 << int(c)
if sm in d:
res = max(res, i - d[sm])
else:
d[sm] = i
for j in range(10):
msk = sm ^ 1 << j
if msk in d and i - d[msk] > res:
res = i - d[msk]
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
sLength = len(s)
earliestBinaries = {}
current = [0] * 10
earliestBinaries[tuple(current)] = -1
currentMax = 0
def computeMaxFromNeighbors(counter, currentTuple):
currentMax = 0
if currentTuple in earliestBinaries:
currentMax = max(currentMax, counter - earliestBinaries[currentTuple])
neighborList = list(currentTuple)
for i in range(len(currentTuple)):
neighborList[i] = 1 - neighborList[i]
neighborTuple = tuple(neighborList)
if neighborTuple in earliestBinaries:
currentMax = max(
currentMax, counter - earliestBinaries[neighborTuple]
)
neighborList[i] = 1 - neighborList[i]
return currentMax
for counter, char in enumerate(s):
current[int(char)] = 1 - current[int(char)]
currentTuple = tuple(current)
currentMax = max(currentMax, computeMaxFromNeighbors(counter, currentTuple))
if currentTuple not in earliestBinaries:
earliestBinaries[currentTuple] = counter
return currentMax | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR RETURN VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
n = len(s)
res = mask = 0
seen = [n] * 1024
seen[0] = -1
for i in range(n):
mask ^= 1 << int(s[i])
res = max(res, i - seen[mask])
for num in range(10):
res = max(res, i - seen[mask ^ 1 << num])
seen[mask] = min(seen[mask], i)
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
cur = ans = 0
m = {}
for i, c in enumerate(s):
cur ^= 1 << int(c)
if cur == 0:
ans = max(ans, i + 1)
elif cur == 2 ** int(math.log(cur, 2)):
ans = max(ans, i + 1)
else:
for j in range(10):
new = cur ^ 1 << j
if new in m:
ans = max(i - m[new], ans)
if cur not in m:
m[cur] = i
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
seen = {(0): -1}
mask = 2**10 - 1
bs = [0] + [(1 << i) for i in range(10)]
cur = 0
best = 1
for i in range(len(s)):
b = 1 << int(s[i])
cur = (cur ^ b) & mask
for m in bs:
if cur ^ m in seen:
best = max(best, i - seen[cur ^ m])
if cur not in seen:
seen[cur] = i
print(seen)
print(bs)
return best | CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
bd = {}
bd[0] = -1
soen = 0
for i, n in enumerate(s):
soen ^= 1 << int(n)
bd[soen] = -1
bd[soen] = i
for j in range(10):
soen_cpy = soen
soen_cpy ^= 1 << j
if soen_cpy in bd:
bd[soen_cpy] = i
soen = 0
max_ = bd[0] + 1
for i, n in enumerate(s):
soen ^= 1 << int(n)
cur_max = bd[soen] - i
if cur_max > max_:
max_ = cur_max
return max_ | CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR |
Given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome.
Return the length of the maximum length awesome substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678"
Output: 1
Example 3:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Example 4:
Input: s = "00"
Output: 2
Constraints:
1 <= s.length <= 10^5
s consists only of digits. | class Solution:
def longestAwesome(self, s: str) -> int:
F = {(0): 0}
res = 0
mask = 0
j = 0
for c in s:
j += 1
mask ^= 1 << ord(c) - ord("0")
for i in range(0, 10):
new_mask = mask ^ 1 << i
if new_mask in list(F.keys()):
res = max(res, j - F[new_mask])
if not mask:
res = max(res, j)
if mask not in list(F.keys()):
F[mask] = j
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$.
A move consists of the following steps:
- Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$.
- Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$.
For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them.
-----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 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces.
-----Output-----
For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible.
-----Constraints-----
- $1 \le T \le 100$
- $1 \le N, M \le 1,000$
- $A_{i, j} \in \{0, 1\}$ for each valid $i, j$
- the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$
-----Example Input-----
1
3 3
010
000
001
-----Example Output-----
1 0 1
2 1 1
1 1 0 | t = int(input())
for _ in range(t):
n, m = map(int, input().split())
a = []
r = [0] * n
c = [0] * m
for i in range(n):
a.append(list(map(int, input())))
for j in range(m):
r[i] |= a[i][j]
c[j] |= a[i][j]
if all(x == 0 for x in r):
for i in range(n):
print(*([-1] * m))
else:
for i in range(n):
print(*(0 if a[i][j] else 1 if r[i] | c[j] else 2 for j in range(m))) | 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 ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR |
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$.
A move consists of the following steps:
- Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$.
- Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$.
For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them.
-----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 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces.
-----Output-----
For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible.
-----Constraints-----
- $1 \le T \le 100$
- $1 \le N, M \le 1,000$
- $A_{i, j} \in \{0, 1\}$ for each valid $i, j$
- the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$
-----Example Input-----
1
3 3
010
000
001
-----Example Output-----
1 0 1
2 1 1
1 1 0 | for _ in range(int(input())):
n, m = map(int, input().split())
q = [([0] * m) for i in range(n)]
a = [input() for i in range(n)]
r = [0] * n
c = [0] * m
for i in range(n):
for j in range(m):
if a[i][j] == "1":
r[i] = 1
c[j] = 1
if sum(r) + sum(c) > 0:
for i in range(n):
for j in range(m):
if a[i][j] == "0":
if r[i] == 0 and c[j] == 0:
q[i][j] = 2
else:
q[i][j] = 1
print(*q[i])
else:
for i in range(n):
for j in range(m):
q[i][j] = -1
print(*q[i]) | 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 VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR |
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$.
A move consists of the following steps:
- Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$.
- Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$.
For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them.
-----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 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces.
-----Output-----
For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible.
-----Constraints-----
- $1 \le T \le 100$
- $1 \le N, M \le 1,000$
- $A_{i, j} \in \{0, 1\}$ for each valid $i, j$
- the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$
-----Example Input-----
1
3 3
010
000
001
-----Example Output-----
1 0 1
2 1 1
1 1 0 | for _ in range(int(input())):
M, N = map(int, input().split())
arr = [[(1) for i in range(N)] for j in range(M)]
zero_cols = []
zero_rows = [0] * N
arr2 = []
for i in range(M):
col = input()
if "1" not in col:
zero_cols.append(i)
temp = []
for dig in col:
temp.append(int(dig))
for k in range(len(temp)):
zero_rows[k] |= temp[k]
arr2.append(temp)
if len(zero_cols) == M:
for i in range(M):
print("-1 " * N)
continue
for i in range(M):
for j in range(N):
arr[i][j] ^= arr2[i][j]
for i in zero_cols:
for j in range(len(zero_rows)):
if zero_rows[j] == 0:
arr[i][j] += 1
for col in arr:
print(*col, sep=" ") | 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 NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF STRING VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING |
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$.
A move consists of the following steps:
- Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$.
- Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$.
For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them.
-----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 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces.
-----Output-----
For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible.
-----Constraints-----
- $1 \le T \le 100$
- $1 \le N, M \le 1,000$
- $A_{i, j} \in \{0, 1\}$ for each valid $i, j$
- the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$
-----Example Input-----
1
3 3
010
000
001
-----Example Output-----
1 0 1
2 1 1
1 1 0 | def row_has1(matrix, i):
if "1" in matrix[i]:
return True
return False
def column_has1(matrix, i):
a = any([(row[i] == "1") for row in matrix])
return a
def main():
testcases = int(input())
for i in range(testcases):
n, m = [int(j) for j in input().strip().split()]
matrix = []
answer_matrix = []
has1 = False
for j in range(n):
matrix.append(input().strip())
answer_matrix.append([(-1) for k in range(m)])
if "1" in matrix[-1]:
has1 = True
if has1:
for row_index in range(n):
for column_index in range(m):
if matrix[row_index][column_index] == "1":
answer_matrix[row_index][column_index] = 0
elif row_has1(matrix, row_index) or column_has1(
matrix, column_index
):
answer_matrix[row_index][column_index] = 1
else:
answer_matrix[row_index][column_index] = 2
for l in answer_matrix:
print(" ".join([str(obj) for obj in l]))
main() | FUNC_DEF IF STRING VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR STRING VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR IF STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.