description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array.
Example 1:
Input:
N=5
arr[] = {2, 1, 3, 2, 3}
Output: 5
Explanation:
Total distinct elements in array is 3
Total sub-arrays that satisfy the condition
are:
Subarray from index 0 to 2
Subarray from index 0 to 3
Subarray from index 0 to 4
Subarray from index 1 to 3
Subarray from index 1 to 4
Example 2:
Input:
N=5
arr[] = {2, 4, 4, 2, 4}
Output: 9
Your Task:
Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10^{4}
|
class Solution:
def countDistinctSubarray(self, a, n):
w = len(set(a))
count = 0
i = 0
d = {}
s = 0
for i in range(n):
d[a[i]] = d.get(a[i], 0) + 1
while len(d) >= w:
count += n - i
d[a[s]] -= 1
if d[a[s]] == 0:
d.pop(a[s])
s += 1
return count
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR
|
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array.
Example 1:
Input:
N=5
arr[] = {2, 1, 3, 2, 3}
Output: 5
Explanation:
Total distinct elements in array is 3
Total sub-arrays that satisfy the condition
are:
Subarray from index 0 to 2
Subarray from index 0 to 3
Subarray from index 0 to 4
Subarray from index 1 to 3
Subarray from index 1 to 4
Example 2:
Input:
N=5
arr[] = {2, 4, 4, 2, 4}
Output: 9
Your Task:
Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10^{4}
|
class Solution:
def countDistinctSubarray(self, arr, n):
s = set(arr)
cnt_distinct = len(s)
ans = self.atmost(arr, cnt_distinct) - self.atmost(arr, cnt_distinct - 1)
return ans
def atmost(self, arr, B):
i = 0
j = 0
d = {}
cnt = 0
while j < len(arr):
if arr[j] in d:
d[arr[j]] += 1
else:
d[arr[j]] = 1
while len(d) > B:
d[arr[i]] -= 1
if d[arr[i]] == 0:
d.pop(arr[i])
i += 1
cnt += j - i + 1
j += 1
return cnt
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array.
Example 1:
Input:
N=5
arr[] = {2, 1, 3, 2, 3}
Output: 5
Explanation:
Total distinct elements in array is 3
Total sub-arrays that satisfy the condition
are:
Subarray from index 0 to 2
Subarray from index 0 to 3
Subarray from index 0 to 4
Subarray from index 1 to 3
Subarray from index 1 to 4
Example 2:
Input:
N=5
arr[] = {2, 4, 4, 2, 4}
Output: 9
Your Task:
Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10^{4}
|
class Solution:
def countDistinctSubarray(self, arr, n):
ans = 0
target_length = len(set(arr))
d = {}
length = 0
left = 0
for i in range(n):
if arr[i] not in d or d[arr[i]] == 0:
length += 1
d[arr[i]] = 1
else:
d[arr[i]] += 1
while length == target_length:
ans += n - i
d[arr[left]] -= 1
if d[arr[left]] == 0:
length -= 1
left += 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
|
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array.
Example 1:
Input:
N=5
arr[] = {2, 1, 3, 2, 3}
Output: 5
Explanation:
Total distinct elements in array is 3
Total sub-arrays that satisfy the condition
are:
Subarray from index 0 to 2
Subarray from index 0 to 3
Subarray from index 0 to 4
Subarray from index 1 to 3
Subarray from index 1 to 4
Example 2:
Input:
N=5
arr[] = {2, 4, 4, 2, 4}
Output: 9
Your Task:
Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10^{4}
|
class Solution:
def countDistinctSubarray(self, arr, n):
sp_set = set()
for i in arr:
sp_set.add(i)
k = len(sp_set)
dp = {}
right, window, ans = 0, 0, 0
for left in range(n):
while right < n and window < k:
dp[arr[right]] = dp.get(arr[right], 0) + 1
if dp[arr[right]] == 1:
window += 1
right += 1
if window == k:
ans += n - right + 1
dp[arr[left]] -= 1
if dp[arr[left]] == 0:
window -= 1
return ans
return res
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER RETURN VAR RETURN VAR
|
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array.
Example 1:
Input:
N=5
arr[] = {2, 1, 3, 2, 3}
Output: 5
Explanation:
Total distinct elements in array is 3
Total sub-arrays that satisfy the condition
are:
Subarray from index 0 to 2
Subarray from index 0 to 3
Subarray from index 0 to 4
Subarray from index 1 to 3
Subarray from index 1 to 4
Example 2:
Input:
N=5
arr[] = {2, 4, 4, 2, 4}
Output: 9
Your Task:
Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10^{4}
|
class Solution:
def countDistinctSubarray(self, arr, n):
l = len(set(arr))
dp = {}
for xi in arr:
dp[xi] = 0
solve = 0
c = 0
i = 0
j = 0
while j < n:
dp[arr[j]] += 1
if dp[arr[j]] == 1:
c += 1
while c == l:
solve += n - j
dp[arr[i]] -= 1
if dp[arr[i]] == 0:
c -= 1
i += 1
j += 1
return solve
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
|
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array.
Example 1:
Input:
N=5
arr[] = {2, 1, 3, 2, 3}
Output: 5
Explanation:
Total distinct elements in array is 3
Total sub-arrays that satisfy the condition
are:
Subarray from index 0 to 2
Subarray from index 0 to 3
Subarray from index 0 to 4
Subarray from index 1 to 3
Subarray from index 1 to 4
Example 2:
Input:
N=5
arr[] = {2, 4, 4, 2, 4}
Output: 9
Your Task:
Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10^{4}
|
class Solution:
def countDistinctSubarray(self, nums, n):
k = len(set(nums))
return self.subarraysWithatmostkDistinct(
nums, k
) - self.subarraysWithatmostkDistinct(nums, k - 1)
def subarraysWithatmostkDistinct(self, nums, k):
memo = {}
Count = release = 0
for acquire in range(len(nums)):
if nums[acquire] in memo:
memo[nums[acquire]] += 1
else:
memo[nums[acquire]] = 1
while release <= acquire and len(memo) > k:
memo[nums[release]] -= 1
if memo[nums[release]] == 0:
del memo[nums[release]]
release += 1
Count += acquire - release + 1
return Count
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR DICT ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
|
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array.
Example 1:
Input:
N=5
arr[] = {2, 1, 3, 2, 3}
Output: 5
Explanation:
Total distinct elements in array is 3
Total sub-arrays that satisfy the condition
are:
Subarray from index 0 to 2
Subarray from index 0 to 3
Subarray from index 0 to 4
Subarray from index 1 to 3
Subarray from index 1 to 4
Example 2:
Input:
N=5
arr[] = {2, 4, 4, 2, 4}
Output: 9
Your Task:
Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10^{4}
|
class Solution:
def countDistinctSubarray(self, arr, n):
tot = 0
mp = {}
for i in arr:
if i not in mp:
mp[i] = 0
tot += 1
mp = {}
l = 0
r = 0
tem = 0
while True:
if tem == tot:
break
if arr[r] not in mp:
mp[arr[r]] = 0
tem += 1
mp[arr[r]] += 1
if tem == tot:
break
r += 1
ans = 0
ans += n - r
tog = 0
while l < n and r < n:
if tog == 0:
mp[arr[l]] -= 1
if mp[arr[l]] != 0:
ans += n - r
l += 1
continue
else:
tog = 1
find = arr[l]
l += 1
else:
r += 1
while r < n and arr[r] != find:
mp[arr[r]] += 1
r += 1
if r == n:
break
mp[arr[r]] += 1
tog = 0
ans += n - r
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR
|
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array.
Example 1:
Input:
N=5
arr[] = {2, 1, 3, 2, 3}
Output: 5
Explanation:
Total distinct elements in array is 3
Total sub-arrays that satisfy the condition
are:
Subarray from index 0 to 2
Subarray from index 0 to 3
Subarray from index 0 to 4
Subarray from index 1 to 3
Subarray from index 1 to 4
Example 2:
Input:
N=5
arr[] = {2, 4, 4, 2, 4}
Output: 9
Your Task:
Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10^{4}
|
class Solution:
def countDistinctSubarray(self, arr, n):
d = {}
k = 0
for i in arr:
if i not in d:
d[i] = 1
k += 1
else:
d[i] += 1
l = 0
r = 0
x = 0
dl = {}
s = 0
for i in range(n):
if arr[i] not in dl:
dl[arr[i]] = 1
s += 1
else:
dl[arr[i]] += 1
if s == k:
x += n - i
while s == k:
dl[arr[l]] -= 1
if dl[arr[l]] == 0:
del dl[arr[l]]
s -= 1
else:
x += n - i
l += 1
return x
t = int(input())
for _ in range(0, t):
n = int(input())
a = list(map(int, input().split()))
ob = Solution()
print(ob.countDistinctSubarray(a, n))
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array.
Example 1:
Input:
N=5
arr[] = {2, 1, 3, 2, 3}
Output: 5
Explanation:
Total distinct elements in array is 3
Total sub-arrays that satisfy the condition
are:
Subarray from index 0 to 2
Subarray from index 0 to 3
Subarray from index 0 to 4
Subarray from index 1 to 3
Subarray from index 1 to 4
Example 2:
Input:
N=5
arr[] = {2, 4, 4, 2, 4}
Output: 9
Your Task:
Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10^{4}
|
class Solution:
def countDistinctSubarray(self, arr, n):
s = set(arr)
d = {e: (0) for e in s}
z, k = 0, 0
ns = set()
c = 0
while k < len(arr):
ns.add(arr[k])
d[arr[k]] += 1
k += 1
while len(ns) == len(s):
c += len(arr) - k + 1
d[arr[z]] -= 1
if not d[arr[z]]:
ns.remove(arr[z])
z += 1
return c
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR
|
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array.
Example 1:
Input:
N=5
arr[] = {2, 1, 3, 2, 3}
Output: 5
Explanation:
Total distinct elements in array is 3
Total sub-arrays that satisfy the condition
are:
Subarray from index 0 to 2
Subarray from index 0 to 3
Subarray from index 0 to 4
Subarray from index 1 to 3
Subarray from index 1 to 4
Example 2:
Input:
N=5
arr[] = {2, 4, 4, 2, 4}
Output: 9
Your Task:
Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10^{4}
|
class Solution:
def countDistinctSubarray(self, arr, n):
d = {}
for i in arr:
d[i] = d.get(i, 0) + 1
r = 0
k = len(d)
d = {}
ans = 0
countdist = 0
for l in range(0, n):
while r < n and countdist < k:
d[arr[r]] = d.get(arr[r], 0) + 1
if d[arr[r]] == 1:
countdist += 1
r += 1
if countdist == k:
ans += n - r + 1
d[arr[l]] -= 1
if d.get(arr[l], 0) == 0:
countdist -= 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR NUMBER RETURN VAR
|
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array.
Example 1:
Input:
N=5
arr[] = {2, 1, 3, 2, 3}
Output: 5
Explanation:
Total distinct elements in array is 3
Total sub-arrays that satisfy the condition
are:
Subarray from index 0 to 2
Subarray from index 0 to 3
Subarray from index 0 to 4
Subarray from index 1 to 3
Subarray from index 1 to 4
Example 2:
Input:
N=5
arr[] = {2, 4, 4, 2, 4}
Output: 9
Your Task:
Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10^{4}
|
class Solution:
def countDistinctSubarray(self, arr, n):
d = {}
for i in range(len(arr)):
if arr[i] in d:
d[arr[i]] = d[arr[i]] + 1
else:
d[arr[i]] = 1
i = 0
j = 0
re = 0
d1 = {}
while j < len(arr):
if arr[j] in d1:
d1[arr[j]] = d1[arr[j]] + 1
else:
d1[arr[j]] = 1
if len(d1) < len(d):
j = j + 1
elif len(d1) == len(d):
while len(d1) == len(d):
result = len(arr) - j
re = re + result
d1[arr[i]] = d1[arr[i]] - 1
if d1[arr[i]] == 0:
del d1[arr[i]]
i = i + 1
j = j + 1
return re
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array.
Example 1:
Input:
N=5
arr[] = {2, 1, 3, 2, 3}
Output: 5
Explanation:
Total distinct elements in array is 3
Total sub-arrays that satisfy the condition
are:
Subarray from index 0 to 2
Subarray from index 0 to 3
Subarray from index 0 to 4
Subarray from index 1 to 3
Subarray from index 1 to 4
Example 2:
Input:
N=5
arr[] = {2, 4, 4, 2, 4}
Output: 9
Your Task:
Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10^{4}
|
class Solution:
def countDistinctSubarray(self, arr, n):
counter = dict.fromkeys(arr, 0)
zeroset = set(arr)
count = 0
i, j, count = 0, -1, 0
while i < n and j < n:
while len(zeroset) > 0:
j = j + 1
if j >= n:
break
counter[arr[j]] += 1
if arr[j] in zeroset:
zeroset.remove(arr[j])
else:
inc = n - j
count += inc
counter[arr[i]] = max(0, counter[arr[i]] - 1)
if counter[arr[i]] == 0:
zeroset.add(arr[i])
i = i + 1
if i >= n:
break
return count
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR RETURN VAR
|
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array.
Example 1:
Input:
N=5
arr[] = {2, 1, 3, 2, 3}
Output: 5
Explanation:
Total distinct elements in array is 3
Total sub-arrays that satisfy the condition
are:
Subarray from index 0 to 2
Subarray from index 0 to 3
Subarray from index 0 to 4
Subarray from index 1 to 3
Subarray from index 1 to 4
Example 2:
Input:
N=5
arr[] = {2, 4, 4, 2, 4}
Output: 9
Your Task:
Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10^{4}
|
class Solution:
def mapAdd(self, dic, val):
if val in dic:
dic[val] += 1
else:
dic[val] = 1
def mapSub(self, dic, val):
dic[val] -= 1
if dic[val] == 0:
del dic[val]
def getCount(self, arr):
dSet = set()
for val in arr:
if val not in dSet:
dSet.add(val)
return len(dSet)
def countDistinctSubarray(self, arr, n):
count = self.getCount(arr)
resCnt = 0
currMap = {}
lb = -1
ub = -1
while lb < len(arr) and ub < len(arr):
if len(currMap) == count:
resCnt += n - ub
lb += 1
self.mapSub(currMap, arr[lb])
else:
ub += 1
if ub == n:
return resCnt
self.mapAdd(currMap, arr[ub])
return resCnt
|
CLASS_DEF FUNC_DEF IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_DEF VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR
|
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array.
Example 1:
Input:
N=5
arr[] = {2, 1, 3, 2, 3}
Output: 5
Explanation:
Total distinct elements in array is 3
Total sub-arrays that satisfy the condition
are:
Subarray from index 0 to 2
Subarray from index 0 to 3
Subarray from index 0 to 4
Subarray from index 1 to 3
Subarray from index 1 to 4
Example 2:
Input:
N=5
arr[] = {2, 4, 4, 2, 4}
Output: 9
Your Task:
Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10^{4}
|
class Solution:
def countDistinctSubarray(self, arr, n):
i, j = 0, 0
m = {}
count = 0
ori = len(set(arr))
ans = 0
while j < n:
if arr[j] not in m:
m[arr[j]] = 1
count += 1
else:
m[arr[j]] += 1
while count == ori:
ans += len(arr) - j
m[arr[i]] -= 1
if m[arr[i]] == 0:
count -= 1
m.pop(arr[i])
i += 1
j += 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER WHILE VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array.
Example 1:
Input:
N=5
arr[] = {2, 1, 3, 2, 3}
Output: 5
Explanation:
Total distinct elements in array is 3
Total sub-arrays that satisfy the condition
are:
Subarray from index 0 to 2
Subarray from index 0 to 3
Subarray from index 0 to 4
Subarray from index 1 to 3
Subarray from index 1 to 4
Example 2:
Input:
N=5
arr[] = {2, 4, 4, 2, 4}
Output: 9
Your Task:
Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10^{4}
|
class Solution:
def countDistinctSubarray(self, arr, n):
d = {}
for i in arr:
d[i] = 0
k = len(d)
count, j, w = 0, 0, 0
for i in range(n):
while k != w and j < n:
d[arr[j]] += 1
if d[arr[j]] == 1:
w += 1
j += 1
if w == k:
count += n - j + 1
d[arr[i]] -= 1
if d[arr[i]] == 0:
w -= 1
return count
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, S):
start = 0
end = 0
maxLen = 0
charDict = {}
while end < len(S):
if S[end] in charDict and charDict[S[end]] >= start:
start = charDict[S[end]] + 1
charDict[S[end]] = end
end += 1
maxLen = max(maxLen, end - start)
return maxLen
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, S):
d = {}
i, j, ans = 0, 0, 0
n = len(S)
while j < n:
if S[j] not in d:
d[S[j]] = j
else:
ans = max(ans, j - i)
a = d[S[j]]
while i <= a:
del d[S[i]]
i += 1
d[S[j]] = j
j += 1
ans = max(len(d), ans)
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, s):
s_set = set()
j = 0
ans = -1
for i in range(len(s)):
if s[i] not in s_set:
s_set.add(s[i])
else:
while j < i and s[i] in s_set:
s_set.remove(s[j])
j += 1
s_set.add(s[i])
ans = max(ans, i - j + 1)
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, s):
n = len(s)
ans = 0
mem = {}
l = 0
for i in range(n):
if s[i] not in mem.keys():
mem[s[i]] = i
else:
ans = max(ans, i - l)
while l < mem[s[i]]:
del mem[s[l]]
l += 1
l += 1
mem[s[i]] = i
if n - l > ans:
ans = n - l
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, S):
d = {}
l = r = 0
maxlen = 0
while r < len(S):
if S[r] in d:
l = max(d[S[r]] + 1, l)
d[S[r]] = r
maxlen = max(maxlen, r - l + 1)
r += 1
return maxlen
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, S):
n = len(S)
seen = set()
left = 0
right = 0
max_len = 0
while left < n and right < n:
if S[right] not in seen:
seen.add(s[right])
right += 1
max_len = max(max_len, right - left)
else:
seen.remove(s[left])
left += 1
return max_len
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, s):
n = len(s)
freq = {}
l, r = 0, 0
res = 0
while r < n:
while s[r] in freq:
del freq[s[l]]
l += 1
freq[s[r]] = 1 + freq.get(s[r], 0)
res = max(res, r - l + 1)
r += 1
return res
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, S):
hashSet = set()
l = 0
r = 0
n = len(S)
maxLen = -1
while r < n:
while S[r] in hashSet:
hashSet.remove(S[l])
l += 1
hashSet.add(S[r])
maxLen = max(maxLen, r - l + 1)
r += 1
return maxLen
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, S):
d = {}
j = 0
ans = 0
for i in range(len(s)):
item = s[i]
if item in d:
j = max(j, d[item] + 1)
d[item] = i
ans = max(ans, i - j + 1)
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, S):
dict1 = {}
i = j = 0
max_len = 0
while j < len(s):
if S[j] not in dict1:
dict1[S[j]] = 1
max_len = max(max_len, j - i + 1)
j += 1
else:
while S[j] in dict1:
del dict1[S[i]]
i += 1
return max_len
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, S):
i, j = 0, 0
fMap = {}
lenMax = 0
while j < len(S):
fMap[S[j]] = fMap.get(S[j], 0) + 1
while fMap[S[j]] > 1:
fMap[S[i]] -= 1
i += 1
lenMax = max(lenMax, j - i + 1)
j += 1
return lenMax
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, S):
a = []
maxm = 0
if len(S) == 1:
return 1
for i in S:
if i not in a:
a.append(i)
else:
if maxm < len(a):
maxm = len(a)
c = a.index(i)
a = a[c + 1 :]
a.append(i)
if maxm < len(a):
return len(a)
return maxm
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, S):
max_len = 0
current_len = 0
char_set = set()
left = 0
right = 0
while right < len(S):
if S[right] not in char_set:
char_set.add(S[right])
current_len += 1
max_len = max(max_len, current_len)
right += 1
else:
char_set.remove(S[left])
left += 1
current_len -= 1
return max_len
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, S):
has = [(-1) for _ in range(256)]
left = 0
right = 0
n = len(S)
res = 0
while right < n:
if has[ord(S[right])] != -1:
left = max(has[ord(S[right])] + 1, left)
has[ord(S[right])] = right
res = max(res, right - left + 1)
right += 1
return res
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, S):
i, j = 0, 0
maxi = -9999
n = len(S)
d = {}
while j < n:
if S[j] in d:
while S[i] != S[j]:
d[S[i]] -= 1
if d[S[i]] == 0:
del d[S[i]]
i += 1
i += 1
elif S[j] not in d:
d[S[j]] = 1
if j - i + 1 > maxi:
maxi = j - i + 1
j += 1
return maxi
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT WHILE VAR VAR IF VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, s):
if len(s) == 0:
return 0
start = 0
maxl = 0
d = {}
for i in range(len(s)):
if s[i] in d and start <= d[s[i]]:
start = d[s[i]] + 1
else:
maxl = max(maxl, i - start + 1)
d[s[i]] = i
return maxl
|
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, str):
l = 0
res = 0
a = {}
arr = []
for r in range(len(str)):
while str[r] in a:
ele = arr.pop(0)
del a[ele]
l += 1
arr.append(str[r])
a[str[r]] = 1
res = max(res, r - l + 1)
return res
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, S):
n = len(S)
if n == 0:
return 0
res = float("-inf")
has = set()
left = 0
for right in range(n):
if S[right] in has:
while left < right and S[right] in has:
has.remove(S[left])
left += 1
has.add(S[right])
res = max(res, right - left + 1)
return res
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR WHILE VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, S):
hashmap = {}
left = 0
right = 0
i = -1
j = -1
max_len = -int(1000000000.0)
while right < len(S):
if S[right] not in hashmap:
hashmap[S[right]] = 1
else:
while left <= right and S[right] in hashmap:
del hashmap[S[left]]
left += 1
hashmap[S[right]] = 1
if i == -1 and j == -1:
i = left
j = right
elif j - i < right - left:
i = left
j = right
max_len = max(max_len, j - i + 1)
right += 1
return max_len
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, S):
j = 0
i = 0
x = {}
max_len = 0
while j < len(S):
if S[j] not in x:
x[S[j]] = 1
else:
max_len = max(max_len, j - i)
while i < j and S[i] != S[j]:
del x[S[i]]
i += 1
del x[S[i]]
i += 1
x[S[j]] = 1
j += 1
max_len = max(max_len, j - i)
return max_len
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, S):
s = {}
left = output = 0
for right, num in enumerate(S):
if num in s:
left = max(left, s[num] + 1)
output = max(output, right - left + 1)
s[num] = right
return output
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, S):
l = []
s1 = ""
for i in S:
if i not in s1:
s1 += i
else:
l.append(s1)
k = s1.index(i)
s1 = s1[k + 1 :] + i
l.append(s1)
l.sort(key=len)
return len(l[-1])
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, S):
n = len(S)
cur_len = 1
max_len = 1
prev_index = -1
visited = {}
for i in S:
visited[i] = -1
visited[S[0]] = 0
for i in range(1, n):
prev_index = visited[S[i]]
if prev_index == -1 or i - cur_len > prev_index:
cur_len += 1
else:
if cur_len > max_len:
max_len = cur_len
cur_len = i - prev_index
visited[S[i]] = i
if cur_len > max_len:
max_len = cur_len
return max_len
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, S):
if len(S) == 1 or S == S[0] * len(S):
return 1
start = 0
l = 0
i = 1
while i < len(S):
if S[i] in S[start:i]:
while start < i:
if S[start] == S[i]:
start += 1
break
start += 1
l = max(l, len(S[start : i + 1]))
i += 1
return l
|
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, S):
dic = {}
cm = count = i = 0
while i < len(S):
if S[i] in dic:
if cm < count:
cm = count
count = 0
i = dic[S[i]] + 1
dic = {}
else:
dic[S[i]] = i
count += 1
i += 1
if cm > count:
return cm
else:
return count
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN VAR RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, S):
mp = {}
ans = 0
i = 0
for j in range(len(S)):
if S[j] in mp:
i = max(mp[S[j]], i)
ans = max(ans, j - i + 1)
mp[S[j]] = j + 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, str1):
r = 0
l = 0
map = {}
MxLen, n, lent = 0, len(str1), 0
while r < n:
if map.get(str1[r], -1) != -1:
l = max(map[str1[r]] + 1, l)
map[str1[r]] = r
MxLen = max(MxLen, r - l + 1)
r += 1
return MxLen
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, s):
temp = ""
ans = 0
for i in range(len(s)):
ch = s[i]
if ch not in temp:
temp += ch
ans = max(ans, len(temp))
else:
temp += ch
while temp[0] != ch:
temp = temp[1:]
temp = temp[1:]
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR WHILE VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, s):
n = len(s)
res = 0
prev = [-1] * 256
i = 0
for j in range(n):
i = max(i, prev[ord(s[j])] + 1)
maxEnd = j - i + 1
res = max(res, maxEnd)
prev[ord(s[j])] = j
return res
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, S):
i = -1
j = -1
mlen = 0
hashmap = {}
le = len(S) - 1
while True:
f1 = False
f2 = False
while i < le:
f1 = True
i += 1
ch = S[i]
if ch in hashmap:
hashmap[ch] += 1
else:
hashmap[ch] = 1
if hashmap[ch] == 2:
break
else:
clen = i - j
if clen > mlen:
mlen = clen
while j < i:
f2 = True
j += 1
ch = S[j]
hashmap[ch] -= 1
if hashmap[ch] == 1:
break
if f1 == False and f2 == False:
break
return mlen
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, s):
d = {}
l = 0
ml = 0
for r in range(len(s)):
if s[r] not in d:
ml = max(ml, r - l + 1)
elif d[s[r]] < l:
ml = max(ml, r - l + 1)
else:
l = d[s[r]] + 1
d[s[r]] = r
return ml
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, arr):
r, l = 0, 0
max_len = 0
count_fr = {}
while r < len(arr):
if count_fr.get(arr[r], -1) != -1:
l = max(count_fr[arr[r]] + 1, l)
max_len = max(max_len, r - l + 1)
count_fr[arr[r]] = r
r += 1
return max_len
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, s):
n = len(s)
hm = {}
l = 0
r = 0
ans = 0
while r < n:
if s[r] in hm:
l = max(l, hm[s[r]] + 1)
hm[s[r]] = r
ans = max(ans, r - l + 1)
r += 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, s: str) -> int:
max_len = 0
left, right = 0, 0
unique_chars = set()
while right < len(s):
if s[right] not in unique_chars:
unique_chars.add(s[right])
right += 1
max_len = max(max_len, right - left)
else:
unique_chars.remove(s[left])
left += 1
return max_len
|
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, S):
l = 0
r = 0
max = 0
a = ""
for i in s:
if i in a[l:r]:
while a[l] != i:
l += 1
l += 1
a += i
if r - l + 1 > max:
max = r - l + 1
r += 1
return max
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR VAR VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, S):
maxi = -(10**9)
dic = {}
start = 0
for i in range(len(S)):
if S[i] not in dic:
dic[S[i]] = i
else:
if start < dic[S[i]] + 1:
start = dic[S[i]] + 1
dic[S[i]] = i
maxi = max(maxi, i - start + 1)
if maxi == -(10**9):
return -1
return maxi
|
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP NUMBER NUMBER RETURN NUMBER RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, S):
ans = 0
l = 0
A = set()
for i in range(len(S)):
while S[i] in A:
A.remove(S[l])
l = l + 1
A.add(S[i])
ans = max(ans, len(A))
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, S):
n = len(S)
d = {}
for i in range(n):
d[S[i]] = -1
d[S[0]] = 0
l = 1
maxx = 1
for i in range(1, n):
x = d[S[i]]
if x == -1 or i - l > x:
l += 1
else:
maxx = max(maxx, l)
l = i - x
d[S[i]] = i
maxx = max(maxx, l)
return maxx
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, S):
l = []
r = []
if len(set(S)) == len(S):
return len(S)
for i in S:
if i not in r:
r += [i]
else:
l += [len(r)]
p = i
while True:
if p in r:
r.pop(0)
else:
r += [p]
break
l += [len(r)]
return max(l)
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR LIST VAR VAR LIST FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER VAR LIST VAR VAR LIST FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, S):
seen = {}
mL = float("-inf")
start, end = 0, 0
while end < len(S):
if S[end] in seen:
start = max(start, seen[S[end]] + 1)
seen[S[end]] = end
mL = max(mL, end - start + 1)
end += 1
return mL if mL != float("-inf") else 0
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_CALL VAR STRING VAR NUMBER
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, S):
char = [(0) for i in range(250)]
n = len(S)
j = 0
ans = 0
i = 0
while j < n:
char[ord(S[j])] += 1
if char[ord(S[j])] > 1:
while char[ord(S[j])] > 1:
char[ord(S[i])] -= 1
i = i + 1
ans = max(ans, j - i + 1)
j = j + 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, S):
length = len(S)
first = 0
second = 0
max = 1
count_list = []
for i in range(26):
count_list.append(0)
while second < length:
while count_list[ord(S[second]) - ord("a")]:
count_list[ord(S[first]) - ord("a")] = 0
first += 1
count_list[ord(S[second]) - ord("a")] = 1
count = second - first + 1
if max < count:
max = count
second += 1
return max
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR VAR WHILE VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR
|
Given a string S, find the length of the longest substring without repeating characters.
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'
|
class Solution:
def longestUniqueSubsttr(self, S):
start = end = m = 0
dic = {}
for i, c in enumerate(S):
if c in dic and dic[c] >= start:
m = max(m, end - start)
start = dic[c] + 1
dic[c] = i
end += 1
return max(m, end - start)
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR
|
There are several cards arranged in a row, and each has an associated number of points. The points are given in the integer array cardPoints of size N.
In one step, you can take one card from beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints, and its size N and the integer k, return the maximum score you can obtain.
Example 1:
Input:
N = 7
k = 3
cardPoints[ ] = {1, 2, 3, 4, 5, 6, 1}
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input:
N = 5
k = 5
arr[ ] = {8, 6, 2, 4, 5}
Output: 25
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxScore() which takes the array of integers cardPoints , an integer N and k as parameters and returns a maximum score.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ k ≤ N
^{1 }≤ cardPoints_{i }≤ 10^{5}
|
class Solution:
def maxScore(self, a, n, k):
sumf = sum(a[:k])
sumb = sum(a[n - k : n])
bf = 0
ef = k - 1
bb = n - 1
eb = n - k
ans = 0
x = 1
while x <= k:
if sumf >= sumb:
ans += a[bf]
sumf -= a[bf]
sumb -= a[eb]
bf += 1
eb += 1
elif sumf < sumb:
ans += a[bb]
sumf -= a[ef]
sumb -= a[bb]
bb -= 1
ef -= 1
x += 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
|
There are several cards arranged in a row, and each has an associated number of points. The points are given in the integer array cardPoints of size N.
In one step, you can take one card from beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints, and its size N and the integer k, return the maximum score you can obtain.
Example 1:
Input:
N = 7
k = 3
cardPoints[ ] = {1, 2, 3, 4, 5, 6, 1}
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input:
N = 5
k = 5
arr[ ] = {8, 6, 2, 4, 5}
Output: 25
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxScore() which takes the array of integers cardPoints , an integer N and k as parameters and returns a maximum score.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ k ≤ N
^{1 }≤ cardPoints_{i }≤ 10^{5}
|
class Solution:
def maxScore(self, cardPoints, n, k):
n = len(cardPoints)
res = sumi = sum(cardPoints[:k])
left = k - 1
right = n - 1
while left >= 0:
sumi -= cardPoints[left]
sumi += cardPoints[right]
res = max(res, sumi)
left -= 1
right -= 1
return res
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
|
There are several cards arranged in a row, and each has an associated number of points. The points are given in the integer array cardPoints of size N.
In one step, you can take one card from beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints, and its size N and the integer k, return the maximum score you can obtain.
Example 1:
Input:
N = 7
k = 3
cardPoints[ ] = {1, 2, 3, 4, 5, 6, 1}
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input:
N = 5
k = 5
arr[ ] = {8, 6, 2, 4, 5}
Output: 25
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxScore() which takes the array of integers cardPoints , an integer N and k as parameters and returns a maximum score.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ k ≤ N
^{1 }≤ cardPoints_{i }≤ 10^{5}
|
class Solution:
def maxScore(self, a, N, k):
s = sum(a[:k])
e = 0
m = max(s, e)
j = len(a) - 1
for i in range(k - 1, -1, -1):
e += a[j]
s -= a[i]
m = max(m, s + e)
j -= 1
k -= 1
return m
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
|
There are several cards arranged in a row, and each has an associated number of points. The points are given in the integer array cardPoints of size N.
In one step, you can take one card from beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints, and its size N and the integer k, return the maximum score you can obtain.
Example 1:
Input:
N = 7
k = 3
cardPoints[ ] = {1, 2, 3, 4, 5, 6, 1}
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input:
N = 5
k = 5
arr[ ] = {8, 6, 2, 4, 5}
Output: 25
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxScore() which takes the array of integers cardPoints , an integer N and k as parameters and returns a maximum score.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ k ≤ N
^{1 }≤ cardPoints_{i }≤ 10^{5}
|
class Solution:
def maxScore(self, cardPoints, N, k):
s = 0
for i in range(N - k, N):
s += cardPoints[i]
p = s
for i in range(k):
h = cardPoints[N - (k - i)]
p = p - h + cardPoints[i]
s = max(s, p)
return s
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
|
There are several cards arranged in a row, and each has an associated number of points. The points are given in the integer array cardPoints of size N.
In one step, you can take one card from beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints, and its size N and the integer k, return the maximum score you can obtain.
Example 1:
Input:
N = 7
k = 3
cardPoints[ ] = {1, 2, 3, 4, 5, 6, 1}
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input:
N = 5
k = 5
arr[ ] = {8, 6, 2, 4, 5}
Output: 25
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxScore() which takes the array of integers cardPoints , an integer N and k as parameters and returns a maximum score.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ k ≤ N
^{1 }≤ cardPoints_{i }≤ 10^{5}
|
class Solution:
def maxScore(self, arr, n, k):
l, r = 0, n - k
summ = max_sum = sum(arr[r:])
for r in range(n - k, n):
summ = summ - arr[r] + arr[l]
l += 1
if summ > max_sum:
max_sum = summ
return max_sum
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR
|
There are several cards arranged in a row, and each has an associated number of points. The points are given in the integer array cardPoints of size N.
In one step, you can take one card from beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints, and its size N and the integer k, return the maximum score you can obtain.
Example 1:
Input:
N = 7
k = 3
cardPoints[ ] = {1, 2, 3, 4, 5, 6, 1}
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input:
N = 5
k = 5
arr[ ] = {8, 6, 2, 4, 5}
Output: 25
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxScore() which takes the array of integers cardPoints , an integer N and k as parameters and returns a maximum score.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ k ≤ N
^{1 }≤ cardPoints_{i }≤ 10^{5}
|
class Solution:
def maxScore(self, cardPoints, N, k):
maxScore = 0
sumTotal = sum(cardPoints[:k])
maxScore = max(maxScore, sumTotal)
for i in range(k):
sumTotal = sumTotal - cardPoints[k - 1 - i] + cardPoints[N - 1 - i]
maxScore = max(maxScore, sumTotal)
return maxScore
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
|
There are several cards arranged in a row, and each has an associated number of points. The points are given in the integer array cardPoints of size N.
In one step, you can take one card from beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints, and its size N and the integer k, return the maximum score you can obtain.
Example 1:
Input:
N = 7
k = 3
cardPoints[ ] = {1, 2, 3, 4, 5, 6, 1}
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input:
N = 5
k = 5
arr[ ] = {8, 6, 2, 4, 5}
Output: 25
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxScore() which takes the array of integers cardPoints , an integer N and k as parameters and returns a maximum score.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ k ≤ N
^{1 }≤ cardPoints_{i }≤ 10^{5}
|
class Solution:
def maxScore(self, a, n, k):
ans = 0
for i in range(k):
ans += a[i]
res = ans
j = n - 1
while i >= 0:
ans = ans - a[i] + a[j]
res = max(res, ans)
i -= 1
j -= 1
return res
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
|
There are several cards arranged in a row, and each has an associated number of points. The points are given in the integer array cardPoints of size N.
In one step, you can take one card from beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints, and its size N and the integer k, return the maximum score you can obtain.
Example 1:
Input:
N = 7
k = 3
cardPoints[ ] = {1, 2, 3, 4, 5, 6, 1}
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input:
N = 5
k = 5
arr[ ] = {8, 6, 2, 4, 5}
Output: 25
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxScore() which takes the array of integers cardPoints , an integer N and k as parameters and returns a maximum score.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ k ≤ N
^{1 }≤ cardPoints_{i }≤ 10^{5}
|
class Solution:
def maxScore(self, cardPoints, N, k):
tot = sum(cardPoints)
ans = 0
res = 0
for i in range(N - k):
res += cardPoints[i]
ans = tot - res
for i in range(N - k, N):
res += cardPoints[i] - cardPoints[i - (N - k)]
ans = max(ans, tot - res)
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR
|
There are several cards arranged in a row, and each has an associated number of points. The points are given in the integer array cardPoints of size N.
In one step, you can take one card from beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints, and its size N and the integer k, return the maximum score you can obtain.
Example 1:
Input:
N = 7
k = 3
cardPoints[ ] = {1, 2, 3, 4, 5, 6, 1}
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input:
N = 5
k = 5
arr[ ] = {8, 6, 2, 4, 5}
Output: 25
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxScore() which takes the array of integers cardPoints , an integer N and k as parameters and returns a maximum score.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ k ≤ N
^{1 }≤ cardPoints_{i }≤ 10^{5}
|
class Solution:
def maxScore(self, cardPoints, N, k):
left, right = 0, len(cardPoints) - k
total = sum(cardPoints[right:])
res = total
while right < len(cardPoints):
total += cardPoints[left] - cardPoints[right]
res = max(total, res)
left += 1
right += 1
return res
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
|
There are several cards arranged in a row, and each has an associated number of points. The points are given in the integer array cardPoints of size N.
In one step, you can take one card from beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints, and its size N and the integer k, return the maximum score you can obtain.
Example 1:
Input:
N = 7
k = 3
cardPoints[ ] = {1, 2, 3, 4, 5, 6, 1}
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input:
N = 5
k = 5
arr[ ] = {8, 6, 2, 4, 5}
Output: 25
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxScore() which takes the array of integers cardPoints , an integer N and k as parameters and returns a maximum score.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ k ≤ N
^{1 }≤ cardPoints_{i }≤ 10^{5}
|
class Solution:
def maxScore(self, A, N, k):
req = len(A) - k
summ = sum(A)
partSum = sum(A[0:req])
mini = partSum
for i in range(req, N):
old = i - req
partSum -= A[old]
partSum += A[i]
mini = min(mini, partSum)
return summ - mini
|
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR
|
There are several cards arranged in a row, and each has an associated number of points. The points are given in the integer array cardPoints of size N.
In one step, you can take one card from beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints, and its size N and the integer k, return the maximum score you can obtain.
Example 1:
Input:
N = 7
k = 3
cardPoints[ ] = {1, 2, 3, 4, 5, 6, 1}
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input:
N = 5
k = 5
arr[ ] = {8, 6, 2, 4, 5}
Output: 25
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxScore() which takes the array of integers cardPoints , an integer N and k as parameters and returns a maximum score.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ k ≤ N
^{1 }≤ cardPoints_{i }≤ 10^{5}
|
class Solution:
def maxScore(self, cardPoints, N, k):
cur = sum(cardPoints[:k])
maxs = cur
x = len(cardPoints)
for i in range(k):
cur = cur + cardPoints[x - 1 - i]
cur = cur - cardPoints[k - 1 - i]
maxs = max(maxs, cur)
return maxs
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
|
There are several cards arranged in a row, and each has an associated number of points. The points are given in the integer array cardPoints of size N.
In one step, you can take one card from beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints, and its size N and the integer k, return the maximum score you can obtain.
Example 1:
Input:
N = 7
k = 3
cardPoints[ ] = {1, 2, 3, 4, 5, 6, 1}
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input:
N = 5
k = 5
arr[ ] = {8, 6, 2, 4, 5}
Output: 25
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxScore() which takes the array of integers cardPoints , an integer N and k as parameters and returns a maximum score.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ k ≤ N
^{1 }≤ cardPoints_{i }≤ 10^{5}
|
class Solution:
def maxScore(self, cardPoints, N, k):
cardPoints = cardPoints[N - k :] + cardPoints[:k]
i, j = 0, 0
maxi = 0
sum_ = 0
count = 0
N = len(cardPoints)
while j < N:
sum_ += cardPoints[j]
count += 1
if count < k:
j += 1
elif count == k:
maxi = max(maxi, sum_)
sum_ -= cardPoints[i]
i += 1
j += 1
count -= 1
return maxi
|
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
|
There are several cards arranged in a row, and each has an associated number of points. The points are given in the integer array cardPoints of size N.
In one step, you can take one card from beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints, and its size N and the integer k, return the maximum score you can obtain.
Example 1:
Input:
N = 7
k = 3
cardPoints[ ] = {1, 2, 3, 4, 5, 6, 1}
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input:
N = 5
k = 5
arr[ ] = {8, 6, 2, 4, 5}
Output: 25
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxScore() which takes the array of integers cardPoints , an integer N and k as parameters and returns a maximum score.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ k ≤ N
^{1 }≤ cardPoints_{i }≤ 10^{5}
|
class Solution:
def maxScore(self, cardPoints, N, k):
n = len(cardPoints)
left, right = 0, n - k
total_sum = sum(cardPoints[right:])
ans_sum = total_sum
for i in range(k):
total_sum += cardPoints[left] - cardPoints[right]
ans_sum = max(ans_sum, total_sum)
left += 1
right += 1
return ans_sum
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
|
There are several cards arranged in a row, and each has an associated number of points. The points are given in the integer array cardPoints of size N.
In one step, you can take one card from beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints, and its size N and the integer k, return the maximum score you can obtain.
Example 1:
Input:
N = 7
k = 3
cardPoints[ ] = {1, 2, 3, 4, 5, 6, 1}
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input:
N = 5
k = 5
arr[ ] = {8, 6, 2, 4, 5}
Output: 25
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxScore() which takes the array of integers cardPoints , an integer N and k as parameters and returns a maximum score.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ k ≤ N
^{1 }≤ cardPoints_{i }≤ 10^{5}
|
class Solution:
def maxScore(self, cardPoints, N, k):
ans = 0
for i in range(k):
ans += cardPoints[i]
cur = ans
for i in range(k - 1, -1, -1):
cur -= cardPoints[i]
cur += cardPoints[N - k + i]
ans = max(cur, ans)
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
|
There are several cards arranged in a row, and each has an associated number of points. The points are given in the integer array cardPoints of size N.
In one step, you can take one card from beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints, and its size N and the integer k, return the maximum score you can obtain.
Example 1:
Input:
N = 7
k = 3
cardPoints[ ] = {1, 2, 3, 4, 5, 6, 1}
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input:
N = 5
k = 5
arr[ ] = {8, 6, 2, 4, 5}
Output: 25
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxScore() which takes the array of integers cardPoints , an integer N and k as parameters and returns a maximum score.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ k ≤ N
^{1 }≤ cardPoints_{i }≤ 10^{5}
|
class Solution:
def maxScore(self, cardPoints, N, k):
summa = 0
for i in range(N - k):
summa += cardPoints[i]
mini = summa
for i in range(k):
summa -= cardPoints[i]
summa += cardPoints[i + N - k]
mini = min(mini, summa)
return sum(cardPoints) - mini
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR
|
There are several cards arranged in a row, and each has an associated number of points. The points are given in the integer array cardPoints of size N.
In one step, you can take one card from beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints, and its size N and the integer k, return the maximum score you can obtain.
Example 1:
Input:
N = 7
k = 3
cardPoints[ ] = {1, 2, 3, 4, 5, 6, 1}
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input:
N = 5
k = 5
arr[ ] = {8, 6, 2, 4, 5}
Output: 25
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxScore() which takes the array of integers cardPoints , an integer N and k as parameters and returns a maximum score.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ k ≤ N
^{1 }≤ cardPoints_{i }≤ 10^{5}
|
class Solution:
def maxScore(self, cardPoints, N, k):
total_points = sum(cardPoints)
if k == len(cardPoints):
return total_points
remain_size = N - k
curr_sum = sum(cardPoints[:remain_size])
mini_sum = curr_sum
for i in range(remain_size, len(cardPoints)):
curr_sum -= cardPoints[i - remain_size]
curr_sum += cardPoints[i]
mini_sum = min(curr_sum, mini_sum)
return total_points - mini_sum
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR
|
There are several cards arranged in a row, and each has an associated number of points. The points are given in the integer array cardPoints of size N.
In one step, you can take one card from beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints, and its size N and the integer k, return the maximum score you can obtain.
Example 1:
Input:
N = 7
k = 3
cardPoints[ ] = {1, 2, 3, 4, 5, 6, 1}
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input:
N = 5
k = 5
arr[ ] = {8, 6, 2, 4, 5}
Output: 25
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxScore() which takes the array of integers cardPoints , an integer N and k as parameters and returns a maximum score.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ k ≤ N
^{1 }≤ cardPoints_{i }≤ 10^{5}
|
class Solution:
def maxScore(self, cardPoints, N, k):
arr = cardPoints
n = len(cardPoints)
t = sum(cardPoints)
r = n - k
sub = sum(arr[:r])
min_s = sub
for i in range(r, n):
sub += arr[i]
sub -= arr[i - r]
min_s = min(min_s, sub)
return t - min_s
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR
|
There are several cards arranged in a row, and each has an associated number of points. The points are given in the integer array cardPoints of size N.
In one step, you can take one card from beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints, and its size N and the integer k, return the maximum score you can obtain.
Example 1:
Input:
N = 7
k = 3
cardPoints[ ] = {1, 2, 3, 4, 5, 6, 1}
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input:
N = 5
k = 5
arr[ ] = {8, 6, 2, 4, 5}
Output: 25
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxScore() which takes the array of integers cardPoints , an integer N and k as parameters and returns a maximum score.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ k ≤ N
^{1 }≤ cardPoints_{i }≤ 10^{5}
|
class Solution:
def maxScore(self, arr, N, k):
i = 0
j = N - k
total = sum(arr[j:])
ans = total
while j < N:
total = total + arr[i] - arr[j]
ans = max(ans, total)
i = i + 1
j = j + 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
There are several cards arranged in a row, and each has an associated number of points. The points are given in the integer array cardPoints of size N.
In one step, you can take one card from beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints, and its size N and the integer k, return the maximum score you can obtain.
Example 1:
Input:
N = 7
k = 3
cardPoints[ ] = {1, 2, 3, 4, 5, 6, 1}
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input:
N = 5
k = 5
arr[ ] = {8, 6, 2, 4, 5}
Output: 25
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxScore() which takes the array of integers cardPoints , an integer N and k as parameters and returns a maximum score.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ k ≤ N
^{1 }≤ cardPoints_{i }≤ 10^{5}
|
class Solution:
def maxScore(self, cardPoints, N, k):
sum1 = 0
ans = 0
i = 0
j = N - 1
sum1 = 0
while i < k:
sum1 = sum1 + cardPoints[i]
ans = max(ans, sum1)
i = i + 1
i = i - 1
while i >= 0 and j >= 0:
sum1 = sum1 + cardPoints[j] - cardPoints[i]
ans = max(sum1, ans)
i = i - 1
j = j - 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
There are several cards arranged in a row, and each has an associated number of points. The points are given in the integer array cardPoints of size N.
In one step, you can take one card from beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints, and its size N and the integer k, return the maximum score you can obtain.
Example 1:
Input:
N = 7
k = 3
cardPoints[ ] = {1, 2, 3, 4, 5, 6, 1}
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input:
N = 5
k = 5
arr[ ] = {8, 6, 2, 4, 5}
Output: 25
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxScore() which takes the array of integers cardPoints , an integer N and k as parameters and returns a maximum score.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ k ≤ N
^{1 }≤ cardPoints_{i }≤ 10^{5}
|
class Solution:
def maxScore(self, cardPoints, N, k):
suff = [0]
for el in cardPoints[::-1]:
suff.append(suff[-1] + el)
ans = 0
temp = 0
for i in range(k + 1):
ans = max(ans, temp + suff[k - i])
if i < len(cardPoints):
temp += cardPoints[i]
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR
|
There are several cards arranged in a row, and each has an associated number of points. The points are given in the integer array cardPoints of size N.
In one step, you can take one card from beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints, and its size N and the integer k, return the maximum score you can obtain.
Example 1:
Input:
N = 7
k = 3
cardPoints[ ] = {1, 2, 3, 4, 5, 6, 1}
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input:
N = 5
k = 5
arr[ ] = {8, 6, 2, 4, 5}
Output: 25
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxScore() which takes the array of integers cardPoints , an integer N and k as parameters and returns a maximum score.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ k ≤ N
^{1 }≤ cardPoints_{i }≤ 10^{5}
|
class Solution:
def maxScore(self, cardPoints, N, k):
o_s = sum(cardPoints[N - k :])
m = o_s
i = 0
j = N - k - 1
while j < N - 1:
j += 1
o_s = o_s - cardPoints[j] + cardPoints[i]
i += 1
m = max(m, o_s)
return m
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
|
There are several cards arranged in a row, and each has an associated number of points. The points are given in the integer array cardPoints of size N.
In one step, you can take one card from beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints, and its size N and the integer k, return the maximum score you can obtain.
Example 1:
Input:
N = 7
k = 3
cardPoints[ ] = {1, 2, 3, 4, 5, 6, 1}
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input:
N = 5
k = 5
arr[ ] = {8, 6, 2, 4, 5}
Output: 25
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxScore() which takes the array of integers cardPoints , an integer N and k as parameters and returns a maximum score.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ k ≤ N
^{1 }≤ cardPoints_{i }≤ 10^{5}
|
class Solution:
def maxScore(self, cardPoints, N, k):
start_sum = 0
end_sum = sum(cardPoints[-k:])
max_sum = end_sum
for i in range(k):
start_sum = start_sum + cardPoints[i]
end_sum = end_sum - cardPoints[N - k + i]
current_sum = start_sum + end_sum
max_sum = max(max_sum, current_sum)
return max_sum
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
|
There are several cards arranged in a row, and each has an associated number of points. The points are given in the integer array cardPoints of size N.
In one step, you can take one card from beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints, and its size N and the integer k, return the maximum score you can obtain.
Example 1:
Input:
N = 7
k = 3
cardPoints[ ] = {1, 2, 3, 4, 5, 6, 1}
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input:
N = 5
k = 5
arr[ ] = {8, 6, 2, 4, 5}
Output: 25
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxScore() which takes the array of integers cardPoints , an integer N and k as parameters and returns a maximum score.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ k ≤ N
^{1 }≤ cardPoints_{i }≤ 10^{5}
|
class Solution:
def maxScore(self, card, N, k):
n = len(card)
r = n - k
total = sum(card[n - k :])
res = total
l = 0
while r < n:
total = total + card[l] - card[r]
res = max(res, total)
r += 1
l += 1
return res
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
|
There are several cards arranged in a row, and each has an associated number of points. The points are given in the integer array cardPoints of size N.
In one step, you can take one card from beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints, and its size N and the integer k, return the maximum score you can obtain.
Example 1:
Input:
N = 7
k = 3
cardPoints[ ] = {1, 2, 3, 4, 5, 6, 1}
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input:
N = 5
k = 5
arr[ ] = {8, 6, 2, 4, 5}
Output: 25
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxScore() which takes the array of integers cardPoints , an integer N and k as parameters and returns a maximum score.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ k ≤ N
^{1 }≤ cardPoints_{i }≤ 10^{5}
|
class Solution:
def maxScore(self, cardPoints, N, k):
window = N - k
if window == 0:
return sum(cardPoints)
window_sum = sum(cardPoints[: window - 1])
total_sum = sum(cardPoints)
max_sum = 0
for i in range(window - 1, N):
window_sum += cardPoints[i]
max_sum = max(max_sum, total_sum - window_sum)
window_sum -= cardPoints[i - window + 1]
return max_sum
|
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
|
There are several cards arranged in a row, and each has an associated number of points. The points are given in the integer array cardPoints of size N.
In one step, you can take one card from beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints, and its size N and the integer k, return the maximum score you can obtain.
Example 1:
Input:
N = 7
k = 3
cardPoints[ ] = {1, 2, 3, 4, 5, 6, 1}
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input:
N = 5
k = 5
arr[ ] = {8, 6, 2, 4, 5}
Output: 25
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxScore() which takes the array of integers cardPoints , an integer N and k as parameters and returns a maximum score.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ k ≤ N
^{1 }≤ cardPoints_{i }≤ 10^{5}
|
class Solution:
def maxScore(self, cardPoints, N, k):
startpoint = N - 1
endpoint = k - 1
rollingsum = sum(cardPoints[0 : endpoint + 1])
maxsum = rollingsum
for i in range(k):
rollingsum = rollingsum - cardPoints[endpoint] + cardPoints[startpoint]
startpoint -= 1
endpoint -= 1
maxsum = max(maxsum, rollingsum)
return maxsum
|
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
|
There are several cards arranged in a row, and each has an associated number of points. The points are given in the integer array cardPoints of size N.
In one step, you can take one card from beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints, and its size N and the integer k, return the maximum score you can obtain.
Example 1:
Input:
N = 7
k = 3
cardPoints[ ] = {1, 2, 3, 4, 5, 6, 1}
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input:
N = 5
k = 5
arr[ ] = {8, 6, 2, 4, 5}
Output: 25
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxScore() which takes the array of integers cardPoints , an integer N and k as parameters and returns a maximum score.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ k ≤ N
^{1 }≤ cardPoints_{i }≤ 10^{5}
|
class Solution:
def maxScore(self, arr, n, k):
if k == n:
return sum(arr)
left_sum = 0
right_sum = 0
for i in range(k):
left_sum += arr[i]
ll = 0
lr = k - 1
rr = n - 1
rl = n - k
count = left_sum
suma = left_sum
for i in range(k):
left_sum -= arr[lr]
right_sum += arr[rr]
suma = suma + arr[rr] - arr[lr]
rr -= 1
lr -= 1
count = max(count, suma)
return count
|
CLASS_DEF FUNC_DEF IF VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
|
There are several cards arranged in a row, and each has an associated number of points. The points are given in the integer array cardPoints of size N.
In one step, you can take one card from beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints, and its size N and the integer k, return the maximum score you can obtain.
Example 1:
Input:
N = 7
k = 3
cardPoints[ ] = {1, 2, 3, 4, 5, 6, 1}
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input:
N = 5
k = 5
arr[ ] = {8, 6, 2, 4, 5}
Output: 25
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxScore() which takes the array of integers cardPoints , an integer N and k as parameters and returns a maximum score.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ k ≤ N
^{1 }≤ cardPoints_{i }≤ 10^{5}
|
class Solution:
def maxScore(self, cardPoints, N, k):
i, j, ans = 0, 0, float("inf")
state = 0
window = N - k
count = 0
for j in range(N):
state += cardPoints[j]
count += 1
if count > window:
state -= cardPoints[i]
i += 1
count -= 1
if count == window:
ans = min(ans, state)
return sum(cardPoints) - ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR
|
There are several cards arranged in a row, and each has an associated number of points. The points are given in the integer array cardPoints of size N.
In one step, you can take one card from beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints, and its size N and the integer k, return the maximum score you can obtain.
Example 1:
Input:
N = 7
k = 3
cardPoints[ ] = {1, 2, 3, 4, 5, 6, 1}
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input:
N = 5
k = 5
arr[ ] = {8, 6, 2, 4, 5}
Output: 25
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxScore() which takes the array of integers cardPoints , an integer N and k as parameters and returns a maximum score.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ k ≤ N
^{1 }≤ cardPoints_{i }≤ 10^{5}
|
class Solution:
def maxScore(self, cardPoints, N, k):
sum = 0
res = 0
Sum = []
Score = 0
n = len(cardPoints)
for i in range(n):
sum += cardPoints[i]
Sum.append(sum)
for i in range(k + 1):
j = i + n - k - 1
if i == 0:
res = Sum[j]
else:
res = Sum[j] - Sum[i - 1]
Score = max(Score, sum - res)
return Score
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR
|
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
|
class Solution:
def lengthOfLongestSubstring(self, s):
L, res, last = -1, 0, {}
for R, char in enumerate(s):
if char in last and last[char] > L:
L = last[char]
elif R - L > res:
res = R - L
last[char] = R
return res
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER DICT FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR RETURN VAR
|
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
|
class Solution:
def lengthOfLongestSubstring(self, s):
result = 0
if len(s) == 0:
return result
hashmap = {}
start = 0
for i in range(len(s)):
if s[i] in hashmap:
start = max(hashmap[s[i]] + 1, start)
hashmap[s[i]] = i
result = max(result, i - start + 1)
return result
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
|
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
|
class Solution:
def lengthOfLongestSubstring(self, s):
head = 0
dic = {}
res = 0
for i in range(len(s)):
if s[i] in dic and dic[s[i]] >= head:
res = max(res, i - head)
head = dic[s[i]] + 1
dic[s[i]] = i
return max(res, len(s) - head)
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR
|
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
|
class Solution:
def lengthOfLongestSubstring(self, s):
record = {}
result = 0
start = -1
for i, x in enumerate(s):
if x in record:
start = max(start, record[x])
record[x] = i
result = max(result, i - start)
return result
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR
|
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
|
class Solution:
def lengthOfLongestSubstring(self, s):
idx = i = 0
record = set()
count = 0
maxcount = 0
while i < len(s):
if s[i] in record:
record.remove(s[idx])
count -= 1
idx += 1
else:
record.add(s[i])
count += 1
i += 1
if count > maxcount:
maxcount = count
return maxcount
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR
|
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
|
class Solution:
def lengthOfLongestSubstring(self, s):
dic = {}
start = max_len = 0
for i, c in enumerate(s):
if c in dic and start <= dic[c]:
start = dic[c] + 1
else:
max_len = max(max_len, i - start + 1)
dic[c] = i
return max_len
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR
|
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
|
class Solution:
def lengthOfLongestSubstring(self, s):
char_dict = {}
longest_run = 0
current_run = 0
distance = 2
for i, char in enumerate(s):
if char in char_dict:
distance = i - char_dict[char]
if distance <= current_run:
current_run = distance
else:
current_run += 1
else:
current_run += 1
char_dict[char] = i
if current_run > longest_run:
longest_run = current_run
return longest_run
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR
|
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
|
class Solution:
def lengthOfLongestSubstring(self, s):
helper_dict = {}
longest_start = 0
longest_end = -1
current_start = 0
for char_index, char in enumerate(s):
if char not in helper_dict or helper_dict[char] < current_start:
helper_dict[char] = char_index
else:
current_start = helper_dict[char] + 1
helper_dict[char] = char_index
if char_index - current_start > longest_end - longest_start:
longest_start = current_start
longest_end = char_index
return longest_end - longest_start + 1
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER
|
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
|
class Solution:
def lengthOfLongestSubstring(self, s):
start = 0
end = 0
seen_set = set()
longest_substring = ""
while end < len(s):
end += 1
currentelem = s[end - 1]
if currentelem not in seen_set:
seen_set.add(currentelem)
if end - start > len(longest_substring):
longest_substring = s[start:end]
else:
while start < end - 1:
if s[start] != currentelem:
seen_set.remove(s[start])
start += 1
else:
start += 1
break
return len(longest_substring)
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING WHILE VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR
|
Utkarsh has recently started taking English-language classes to improve his reading and writing skills. However, he is still struggling to learn English. His teacher gave him the following problem to improve his vowel-identification skills:
There is a string S of length N consisting of lowercase English letters only. Utkarsh has to start from the first letter of the string.
Each time he encounters a vowel (i.e. a character from the set \{a, e, i, o, u\}) he has to reverse the entire substring that came before the vowel.
Utkarsh needs help verifying his answer. Can you print the final string after performing all the operations for him?
------ Input Format ------
- First line will contain T, number of test cases. Then T test cases follow.
- The first line of each test case contains N, the length of the string.
- The second line contains S, the string itself.
------ Output Format ------
For each test case, output in a single line the final string after traversing S from left to right and performing the necessary reversals.
------ Constraints ------
$1 ≤ T ≤ 10^{4}$
$1 ≤ N ≤ 10^{6}$
- Sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
10
abcdefghij
7
bcadage
----- Sample Output 1 ------
hgfeabcdij
gacbade
----- explanation 1 ------
Test case $1$: The first letter is a vowel, but there is no substring before it to reverse, so we leave it as it is. Next, we reverse abcd and the string becomes dcbaefghij. Next we reach the vowel i and reverse dcbaefgh to get the string hgfeabcdij.
Test case $2$: Initially, we reverse bc and the string becomes cbadage. Next we reach the vowel a and reverse cbad to get the string dabcage. Finally we reach the vowel e and reverse dabcag to get the string gacbade.
|
T = int(input())
for vinay in range(T):
N = int(input())
S = input()
count = 0
ans1 = ""
ans2 = ""
for c in range(N - 1, -1, -1):
if count % 2 == 0:
ans2 = S[c] + ans2
else:
ans1 += S[c]
if S[c] == "a" or S[c] == "e" or S[c] == "i" or S[c] == "o" or S[c] == "u":
count += 1
print(ans1 + ans2)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
Utkarsh has recently started taking English-language classes to improve his reading and writing skills. However, he is still struggling to learn English. His teacher gave him the following problem to improve his vowel-identification skills:
There is a string S of length N consisting of lowercase English letters only. Utkarsh has to start from the first letter of the string.
Each time he encounters a vowel (i.e. a character from the set \{a, e, i, o, u\}) he has to reverse the entire substring that came before the vowel.
Utkarsh needs help verifying his answer. Can you print the final string after performing all the operations for him?
------ Input Format ------
- First line will contain T, number of test cases. Then T test cases follow.
- The first line of each test case contains N, the length of the string.
- The second line contains S, the string itself.
------ Output Format ------
For each test case, output in a single line the final string after traversing S from left to right and performing the necessary reversals.
------ Constraints ------
$1 ≤ T ≤ 10^{4}$
$1 ≤ N ≤ 10^{6}$
- Sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
10
abcdefghij
7
bcadage
----- Sample Output 1 ------
hgfeabcdij
gacbade
----- explanation 1 ------
Test case $1$: The first letter is a vowel, but there is no substring before it to reverse, so we leave it as it is. Next, we reverse abcd and the string becomes dcbaefghij. Next we reach the vowel i and reverse dcbaefgh to get the string hgfeabcdij.
Test case $2$: Initially, we reverse bc and the string becomes cbadage. Next we reach the vowel a and reverse cbad to get the string dabcage. Finally we reach the vowel e and reverse dabcag to get the string gacbade.
|
kt = int(input())
while kt:
p = int(input())
s = input()
ans = []
for i in s:
ans.append(i)
h = p - 1
while h >= 0 and s[h] not in ["a", "e", "i", "o", "u"]:
h -= 1
h -= 1
st = 0
end = h
dirl = 0
ind = h
while ind >= 0:
temp = []
while ind >= 0:
if s[ind] in ["a", "e", "i", "o", "u"]:
temp.append(s[ind])
ind -= 1
break
else:
temp.append(s[ind])
ind -= 1
if dirl % 2 == 1:
for it in temp:
ans[end] = it
end -= 1
else:
for it in temp:
ans[st] = it
st += 1
dirl += 1
print("".join(i for i in ans))
kt -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR LIST STRING STRING STRING STRING STRING VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER IF VAR VAR LIST STRING STRING STRING STRING STRING EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR VAR NUMBER
|
Utkarsh has recently started taking English-language classes to improve his reading and writing skills. However, he is still struggling to learn English. His teacher gave him the following problem to improve his vowel-identification skills:
There is a string S of length N consisting of lowercase English letters only. Utkarsh has to start from the first letter of the string.
Each time he encounters a vowel (i.e. a character from the set \{a, e, i, o, u\}) he has to reverse the entire substring that came before the vowel.
Utkarsh needs help verifying his answer. Can you print the final string after performing all the operations for him?
------ Input Format ------
- First line will contain T, number of test cases. Then T test cases follow.
- The first line of each test case contains N, the length of the string.
- The second line contains S, the string itself.
------ Output Format ------
For each test case, output in a single line the final string after traversing S from left to right and performing the necessary reversals.
------ Constraints ------
$1 ≤ T ≤ 10^{4}$
$1 ≤ N ≤ 10^{6}$
- Sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
10
abcdefghij
7
bcadage
----- Sample Output 1 ------
hgfeabcdij
gacbade
----- explanation 1 ------
Test case $1$: The first letter is a vowel, but there is no substring before it to reverse, so we leave it as it is. Next, we reverse abcd and the string becomes dcbaefghij. Next we reach the vowel i and reverse dcbaefgh to get the string hgfeabcdij.
Test case $2$: Initially, we reverse bc and the string becomes cbadage. Next we reach the vowel a and reverse cbad to get the string dabcage. Finally we reach the vowel e and reverse dabcag to get the string gacbade.
|
t = int(input())
for i in range(t):
n = int(input())
s = input()
l = ["a", "e", "i", "o", "u"]
a = []
temp = ""
cnt = 0
for i in range(len(s)):
if i != 0 and s[i] in l:
a.append(temp)
temp = ""
cnt += 1
temp += s[i]
ans = []
if cnt % 2 == 0:
i = 0
while i < cnt:
if i % 2 == 0:
ans.append(a[i])
else:
ans.insert(0, a[i])
i += 1
else:
i = 0
while i < cnt:
if i % 2:
ans.append(a[i])
else:
ans.insert(0, a[i])
i += 1
ans.append(temp)
for k in range(cnt // 2 + cnt % 2):
l = ans[k]
ans[k] = l[::-1]
for h in ans:
print(h, end="")
print()
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING VAR NUMBER VAR VAR VAR ASSIGN VAR LIST IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
|
Utkarsh has recently started taking English-language classes to improve his reading and writing skills. However, he is still struggling to learn English. His teacher gave him the following problem to improve his vowel-identification skills:
There is a string S of length N consisting of lowercase English letters only. Utkarsh has to start from the first letter of the string.
Each time he encounters a vowel (i.e. a character from the set \{a, e, i, o, u\}) he has to reverse the entire substring that came before the vowel.
Utkarsh needs help verifying his answer. Can you print the final string after performing all the operations for him?
------ Input Format ------
- First line will contain T, number of test cases. Then T test cases follow.
- The first line of each test case contains N, the length of the string.
- The second line contains S, the string itself.
------ Output Format ------
For each test case, output in a single line the final string after traversing S from left to right and performing the necessary reversals.
------ Constraints ------
$1 ≤ T ≤ 10^{4}$
$1 ≤ N ≤ 10^{6}$
- Sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
10
abcdefghij
7
bcadage
----- Sample Output 1 ------
hgfeabcdij
gacbade
----- explanation 1 ------
Test case $1$: The first letter is a vowel, but there is no substring before it to reverse, so we leave it as it is. Next, we reverse abcd and the string becomes dcbaefghij. Next we reach the vowel i and reverse dcbaefgh to get the string hgfeabcdij.
Test case $2$: Initially, we reverse bc and the string becomes cbadage. Next we reach the vowel a and reverse cbad to get the string dabcage. Finally we reach the vowel e and reverse dabcag to get the string gacbade.
|
t = int(input())
for _ in range(t):
n = int(input())
s = input()[::-1]
left = []
right = []
back = True
for c in s:
if back:
right.append(c)
else:
left.append(c)
if c in "aeuio":
back = not back
right.reverse()
print("".join(left + right))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR VAR
|
Utkarsh has recently started taking English-language classes to improve his reading and writing skills. However, he is still struggling to learn English. His teacher gave him the following problem to improve his vowel-identification skills:
There is a string S of length N consisting of lowercase English letters only. Utkarsh has to start from the first letter of the string.
Each time he encounters a vowel (i.e. a character from the set \{a, e, i, o, u\}) he has to reverse the entire substring that came before the vowel.
Utkarsh needs help verifying his answer. Can you print the final string after performing all the operations for him?
------ Input Format ------
- First line will contain T, number of test cases. Then T test cases follow.
- The first line of each test case contains N, the length of the string.
- The second line contains S, the string itself.
------ Output Format ------
For each test case, output in a single line the final string after traversing S from left to right and performing the necessary reversals.
------ Constraints ------
$1 ≤ T ≤ 10^{4}$
$1 ≤ N ≤ 10^{6}$
- Sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
10
abcdefghij
7
bcadage
----- Sample Output 1 ------
hgfeabcdij
gacbade
----- explanation 1 ------
Test case $1$: The first letter is a vowel, but there is no substring before it to reverse, so we leave it as it is. Next, we reverse abcd and the string becomes dcbaefghij. Next we reach the vowel i and reverse dcbaefgh to get the string hgfeabcdij.
Test case $2$: Initially, we reverse bc and the string becomes cbadage. Next we reach the vowel a and reverse cbad to get the string dabcage. Finally we reach the vowel e and reverse dabcag to get the string gacbade.
|
for _ in range(0, int(input())):
n = int(input())
s = input()
ans = [" "] * n
vowel = 0
left, right = 0, n - 1
for i in range(n - 1, -1, -1):
if vowel & 1 == 1:
ans[left] = s[i]
left += 1
else:
ans[right] = s[i]
right -= 1
vowel += 1 if s[i] in ["a", "e", "i", "o", "u"] else 0
print("".join(x for x in ans))
|
FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR VAR LIST STRING STRING STRING STRING STRING NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.