description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
substrs = []
substr = ""
chars = {}
for c in s:
substr += c
if c in chars.keys():
chars[c] += 1
else:
chars[c] = 1
if len(chars) == k:
substrs.append(substr)
continue
while len(chars) > k:
if chars[substr[0]] == 1:
chars.pop(substr[0])
else:
chars[substr[0]] -= 1
substr = substr[1:]
if len(chars) == k:
substrs.append(substr)
res = max([len(ss) for ss in substrs]) if substrs else -1
return res
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR DICT FOR VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
n = len(s)
freq = [0] * 26
start = 0
distinct_count = 0
max_len = -1
for end in range(n):
char_index = ord(s[end]) - ord("a")
freq[char_index] += 1
if freq[char_index] == 1:
distinct_count += 1
while distinct_count > k:
freq[ord(s[start]) - ord("a")] -= 1
if freq[ord(s[start]) - ord("a")] == 0:
distinct_count -= 1
start += 1
if distinct_count == k:
max_len = max(max_len, end - start + 1)
return max_len
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
return self.helper(s, k)
def helper(self, s, k):
i, j, ans = 0, 0, -1
state = {}
N = len(s)
for j in range(N):
state[s[j]] = 1 + state.get(s[j], 0)
while len(state) > k:
state[s[i]] -= 1
if state[s[i]] == 0:
state.pop(s[i])
i += 1
if len(state) == k:
ans = max(ans, j - i + 1)
return ans
|
CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL 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 IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
d = {}
for i in range(len(s)):
if s[i] in d:
d[s[i]] += 1
else:
d[s[i]] = 1
if len(d) < k:
return -1
m = {}
j = 0
ans = 0
for i in range(len(s)):
if s[i] in m:
m[s[i]] += 1
else:
m[s[i]] = 1
while len(m) > k:
m[s[j]] = m[s[j]] - 1
if m[s[j]] == 0:
del m[s[j]]
j = j + 1
ans = max(ans, i - j + 1)
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN 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 FUNC_CALL VAR 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 FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
window_start, hash_map, max_len = 0, {}, 0
for window_end in range(len(s)):
hash_map[s[window_end]] = 1 + hash_map.get(s[window_end], 0)
while len(hash_map) > k:
hash_map[s[window_start]] -= 1
if hash_map[s[window_start]] == 0:
del hash_map[s[window_start]]
window_start += 1
max_len = max(max_len, window_end - window_start + 1)
return -1 if len(hash_map) < k else max_len
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER DICT NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER VAR
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
i = 0
result = -1
mapp = dict()
for j, v in enumerate(s):
mapp[v] = mapp.get(v, 0) + 1
if len(mapp) == k:
total = 0
for val in mapp.values():
total += val
result = max(result, total)
while len(mapp) > k:
mapp[s[i]] -= 1
if mapp[s[i]] == 0:
del mapp[s[i]]
i += 1
return result
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
ans = float("-inf")
curr = 0
startWindow = 0
mp = {}
for endWindow in range(len(s)):
if s[endWindow] not in mp:
mp[s[endWindow]] = 1
else:
mp[s[endWindow]] += 1
while len(mp) > k:
mp[s[startWindow]] -= 1
if mp[s[startWindow]] == 0:
mp.pop(s[startWindow])
startWindow += 1
ans = max(ans, endWindow + 1 - startWindow)
if len(mp) != k:
return -1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER 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 ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN VAR
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, a, k):
i, j = 0, 0
s = {}
maxi = 0
n = len(a)
while j < n:
if a[j] in s:
s[a[j]] += 1
else:
s[a[j]] = 1
j += 1
while len(s) > k:
s[a[i]] -= 1
if s[a[i]] == 0:
del s[a[i]]
i += 1
maxi = max(maxi, j - i)
if len(s) < k:
return -1
else:
return maxi
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN VAR
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
n = len(s)
hashmap = {}
l, r = 0, 0
res = 0
while r < n:
while len(hashmap) > k:
hashmap[s[l]] -= 1
if hashmap[s[l]] == 0:
del hashmap[s[l]]
l += 1
hashmap[s[r]] = 1 + hashmap.get(s[r], 0)
if len(hashmap) == k:
res = max(res, r - l + 1)
r += 1
return res if res != 0 else -1
|
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 FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR NUMBER VAR NUMBER
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
n = len(s)
distinct = 0
length = 0
letters = {}
i, j = 0, 0
while j < n:
if s[j] in letters:
letters[s[j]] += 1
else:
letters[s[j]] = 1
distinct = len(letters)
if distinct == k:
length = max(length, j - i + 1)
while distinct > k:
letters[s[i]] -= 1
if letters[s[i]] == 0:
del letters[s[i]]
distinct = len(letters)
i += 1
j += 1
return length if length != 0 else -1
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER RETURN VAR NUMBER VAR NUMBER
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
count = 0
l = 0
r = 0
arr = {}
maxx = 0
for i in range(len(s)):
if s[i] not in arr:
count += 1
arr[s[i]] = 1
else:
arr[s[i]] += 1
if count > k:
temp = l
while l < r:
arr[s[l]] -= 1
l += 1
if arr[s[l - 1]] == 0:
break
if l != temp:
arr.pop(s[l - 1])
count -= 1
else:
arr.pop(s[l])
count -= 1
l += 1
if len(s[l : r + 1]) > maxx and count == k:
maxx = len(s[l : r + 1])
r += 1
if maxx == 0:
return -1
return maxx
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN VAR
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
d = {}
i = 0
answer = 0
for j in range(len(s)):
d[s[j]] = d.get(s[j], 0) + 1
if len(d) < k:
pass
elif len(d) == k:
answer = max(answer, j - i + 1)
elif len(d) > k:
while len(d) > k and i <= j:
d[s[i]] -= 1
if d[s[i]] == 0:
del d[s[i]]
i += 1
return answer if answer > 0 else -1
|
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 BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR NUMBER VAR NUMBER
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
hm = {}
i = 0
result = -1
for j, val in enumerate(s):
hm[val] = hm.get(val, 0) + 1
while len(hm) > k:
hm[s[i]] -= 1
if hm[s[i]] == 0:
del hm[s[i]]
i += 1
if len(hm) == k:
result = max(result, j - i + 1)
return result
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
i = 0
j = 0
dic = {}
ans = -1
while j < len(s):
if s[j] not in dic:
dic[s[j]] = 1
else:
dic[s[j]] += 1
if len(dic) < k:
j += 1
elif len(dic) == k:
ans = max(j - i + 1, ans)
j += 1
elif len(dic) > k:
while len(dic) > k:
if s[i] in dic:
dic[s[i]] -= 1
if dic[s[i]] == 0:
dic.pop(s[i])
i += 1
j += 1
return ans
|
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 VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
d = {}
l = len(s)
j = 0
mx = -1
for i in range(l):
if s[i] not in d:
d[s[i]] = 1
else:
d[s[i]] = d[s[i]] + 1
while len(d) > k:
d[s[j]] = d[s[j]] - 1
if d[s[j]] <= 0:
del d[s[j]]
j += 1
if len(d) == k:
mx = max(mx, i - j + 1)
return mx
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def uniqChars(self, s):
map = {}
for i in s:
map[i] = True
return len(map.keys())
def longestKSubstr(self, s, k):
freq = {}
i, j = 0, 1
if self.uniqChars(s) < k:
return -1
freq[s[0]] = 1
mxLen = 1
uniq = 1
while i < len(s) and j < len(s):
if freq.get(s[j], 0) == 0:
uniq += 1
freq[s[j]] = freq.get(s[j], 0) + 1
if uniq == k:
mxLen = max(mxLen, j - i + 1)
elif uniq > k:
while uniq > k:
freq[s[i]] -= 1
if freq[s[i]] == 0:
uniq -= 1
i += 1
j += 1
return mxLen
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR DICT ASSIGN VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
map = {}
left = 0
maxLength = -1
for right in range(len(s)):
ch = s[right]
map[ch] = map.get(ch, 0) + 1
if len(map) == k:
maxLength = max(maxLength, right - left + 1)
while len(map) > k:
left_char = s[left]
map[left_char] -= 1
if map[left_char] == 0:
del map[left_char]
left += 1
return maxLength
|
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 ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER RETURN VAR
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
left = 0
freq = {}
res = 0
for right, c in enumerate(s):
freq[c] = freq.get(c, 0) + 1
if len(freq) == k:
res = max(res, right - left + 1)
while len(freq) > k:
freq[s[left]] -= 1
if not freq[s[left]]:
del freq[s[left]]
left += 1
return res if res else -1
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR NUMBER
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
hashmap = {}
for i in s:
hashmap[i] = 1 + hashmap.get(i, 0)
if len(hashmap) < k:
return -1
windowstart = 0
hashtable = {}
length = 0
for windowend in range(len(s)):
if s[windowend] not in hashtable:
hashtable[s[windowend]] = 0
hashtable[s[windowend]] += 1
while len(hashtable) > k:
hashtable[s[windowstart]] -= 1
if hashtable[s[windowstart]] == 0:
del hashtable[s[windowstart]]
windowstart += 1
length = max(length, windowend - windowstart + 1)
return length
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
i = -1
j = -1
left = 0
right = 0
hm = {}
while right < len(s):
if s[right] not in hm:
hm[s[right]] = 1
else:
hm[s[right]] += 1
while left <= right and len(hm) > k:
hm[s[left]] -= 1
if hm[s[left]] == 0:
del hm[s[left]]
left += 1
if len(hm) == k:
if i == -1 and j == -1:
i = left
j = right
elif j - i < right - left:
i = left
j = right
right += 1
if i == -1 and j == -1:
return -1
return len(s[i : j + 1])
|
CLASS_DEF FUNC_DEF ASSIGN 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 NUMBER 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 IF FUNC_CALL VAR VAR VAR 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 VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
i, j = 0, 0
freq = {}
ans = -1
while j < len(s):
if s[j] in freq:
freq[s[j]] += 1
else:
freq[s[j]] = 1
if len(freq) < k:
j += 1
elif len(freq) == k:
ans = max(ans, j - i + 1)
j += 1
else:
while len(freq) > k:
freq[s[i]] -= 1
if freq[s[i]] == 0:
del freq[s[i]]
i += 1
j += 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER 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 IF FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
occurences = {}
count = k
l = 0
r = 0
n = len(s)
maxLen = -1
while r < n:
occurences[s[r]] = occurences.get(s[r], 0) + 1
if occurences[s[r]] == 1:
count -= 1
if count == 0:
maxLen = max(maxLen, r - l + 1)
elif count < 0:
while count < 0:
occurences[s[l]] -= 1
if occurences[s[l]] == 0:
count += 1
l += 1
r += 1
return maxLen
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
di = {}
i = 0
j = 0
n = len(s)
maxLen = 0
while j < n:
if s[j] not in di:
di[s[j]] = 1
else:
di[s[j]] += 1
if len(di) < k:
j += 1
elif len(di) == k:
maxLen = max(maxLen, j - i + 1)
j += 1
elif len(di) > k:
while len(di) > k:
di[s[i]] -= 1
if di[s[i]] == 0:
del di[s[i]]
i += 1
if len(di) == k:
maxLen = max(maxLen, j - i + 1)
j += 1
if maxLen == 0:
return -1
return maxLen
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN VAR
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
freq = {}
i, j = 0, 0
ans = -1
longest = ""
while j < len(s):
freq[s[j]] = 1 + freq.get(s[j], 0)
while len(freq) > k:
freq[s[i]] -= 1
if freq[s[i]] == 0:
del freq[s[i]]
i += 1
if len(freq) == k and j - i + 1 > len(longest):
longest = s[i : j + 1]
ans = max(ans, j - i + 1)
j += 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
pt1, pt2, ct, max_len = 0, 0, 0, 0
hash_map = {}
while pt2 < len(s):
if s[pt2] in hash_map:
hash_map[s[pt2]] += 1
else:
hash_map[s[pt2]] = 1
ct += 1
while ct > k and pt1 <= pt2:
hash_map[s[pt1]] -= 1
if hash_map[s[pt1]] == 0:
del hash_map[s[pt1]]
ct -= 1
pt1 += 1
if ct == k:
max_len = max(max_len, pt2 - pt1 + 1)
pt2 += 1
if max_len == 0:
return -1
return max_len
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR DICT WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN VAR
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
d = {}
ans = -1
p1, p2, n = 0, 0, len(s)
b = False
while p2 < n:
if s[p2] in d:
d[s[p2]] += 1
else:
d[s[p2]] = 1
while len(d) > k and p1 <= p2:
d[s[p1]] -= 1
if d[s[p1]] == 0:
del d[s[p1]]
p1 += 1
b = True
ans = max(ans, p2 - p1 + 1)
p2 += 1
if b or len(d) == k:
return ans
return -1
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR RETURN VAR RETURN NUMBER
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
if len(s) < k:
return -1
l = 0
D = {s[l]: 1}
ans = -1
for r in range(1, len(s)):
D[s[r]] = 1 + D.get(s[r], 0)
if len(D) > k:
while len(D) > k and l <= r:
D[s[l]] = D[s[l]] - 1
if D[s[l]] == 0:
D.pop(s[l])
l += 1
if len(D) == k:
ans = max(ans, r - l + 1)
return ans
|
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
n = len(s)
a = s
i = 0
f = {}
l, o = 0, 0
i = 0
j = 0
ans = -1
while i < n and j < n:
if a[j] not in f or f[a[j]] == 0:
f[a[j]] = 1
o += 1
else:
f[a[j]] += 1
l += 1
if o == k:
ans = max(l, ans)
elif o > k:
while i < j:
f[a[i]] -= 1
l -= 1
if f[a[i]] == 0:
i += 1
break
i += 1
o -= 1
j += 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
if len(s) < k or len(set(s)) < k:
return -1
map1 = {}
start = 0
maxlength, curr = 0, -1
for end in range(len(s)):
if s[end] not in map1:
map1[s[end]] = 0
map1[s[end]] += 1
while len(map1) > k:
map1[s[start]] -= 1
if map1[s[start]] <= 0:
del map1[s[start]]
start += 1
curr = max(curr, end - start + 1)
return curr
|
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
n = len(s)
if len(set(s)) < k:
return -1
count = [0] * 26
distinct = 0
ans = 0
i = j = 0
while j < n:
if count[ord(s[j]) - ord("a")] == 0:
distinct += 1
count[ord(s[j]) - ord("a")] += 1
while distinct > k:
count[ord(s[i]) - ord("a")] -= 1
if count[ord(s[i]) - ord("a")] == 0:
distinct -= 1
i += 1
if distinct == k:
ans = max(ans, j - i + 1)
j += 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER WHILE VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
i = 0
j = 0
d = {}
res = -1
n = len(s)
while i < n:
if s[i] not in d:
if len(d) == k:
while len(d) == k:
d[s[j]] -= 1
if d[s[j]] == 0:
del d[s[j]]
j += 1
d[s[i]] = d.get(s[i], 0) + 1
if len(d) == k:
res = max(res, i + 1 - j)
i += 1
return res
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR VAR IF FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
count = 0
l = 0
r = 0
arr = {}
temp = -1
maxx = 0
while r < len(s):
if s[r] not in arr:
count += 1
arr[s[r]] = 1
elif r != temp1:
arr[s[r]] += 1
if count > k:
temp = l
while l < r:
arr[s[l]] -= 1
l += 1
if arr[s[l - 1]] == 0:
break
if l != temp:
arr.pop(s[l - 1])
count -= 1
else:
arr.pop(s[l])
count -= 1
l += 1
if len(s[l : r + 1]) > maxx and count == k:
maxx = len(s[l : r + 1])
else:
if len(s[l : r + 1]) > maxx and count == k:
maxx = len(s[l : r + 1])
temp1 = r
r += 1
if maxx == 0:
return -1
return maxx
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN VAR
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
dic = {}
maxi = -(10**9)
c = 0
start = 0
mini = -(10**9)
for i in range(len(s)):
if s[i] in dic:
dic[s[i]] += 1
else:
dic[s[i]] = 1
c += 1
if c == k:
maxi = max(maxi, i - start + 1)
elif c > k:
while start < i:
dic[s[start]] -= 1
start += 1
if dic[s[start - 1]] == 0:
dic.pop(s[start - 1])
break
maxi = max(maxi, i - start + 1)
c -= 1
if maxi == -(10**9):
return -1
return maxi
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP NUMBER NUMBER RETURN NUMBER RETURN VAR
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
d = {}
i = 0
j = 0
maxl = 0
while j < len(s):
if s[j] in d:
d[s[j]] = d[s[j]] + 1
else:
d[s[j]] = 1
if len(d) < k:
j = j + 1
elif len(d) == k:
if j - i + 1 > maxl:
maxl = j - i + 1
j = j + 1
else:
while len(d) > k:
d[s[i]] = d[s[i]] - 1
if d[s[i]] == 0:
del d[s[i]]
i = i + 1
j = j + 1
if maxl == 0:
return -1
return maxl
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER 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 VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE FUNC_CALL VAR 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 IF VAR NUMBER RETURN NUMBER RETURN VAR
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
ans = -1
ws = 0
d = {}
for i in range(len(s)):
c = s[i]
if c in d:
d[c] += 1
else:
d[c] = 1
while len(d) > k:
a = s[ws]
d[a] -= 1
if d[a] == 0:
del d[a]
ws += 1
if len(d) == k:
ans = max(ans, i - ws + 1)
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
mp = dict()
i, j, ans = 0, 0, -1
n = len(s)
while j < n:
if s[j] in mp:
mp[s[j]] += 1
else:
mp[s[j]] = 1
if len(mp) < k:
j += 1
if len(mp) == k:
ans = max(ans, j - i + 1)
j += 1
elif len(mp) > k:
while len(mp) > k:
mp[s[i]] -= 1
if mp[s[i]] == 0:
mp.pop(s[i])
i += 1
j += 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
l = 0
count = {}
maxLen = float("-inf")
for r in range(len(s)):
if s[r] not in count:
count[s[r]] = 0
count[s[r]] += 1
while len(count) > k:
count[s[l]] -= 1
if count[s[l]] == 0:
del count[s[l]]
l += 1
if len(count) == k:
maxLen = max(maxLen, r - l + 1)
return -1 if maxLen == float("-inf") else maxLen
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR FUNC_CALL VAR STRING NUMBER VAR
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
ump = {}
ump1 = {}
start = 0
maxi = -1
if len(s) < k:
return maxi
for i in range(len(s)):
ump1[s[i]] = ump1.get(s[i], 0) + 1
if len(ump1) < k:
return maxi
for end in range(len(s)):
ump[s[end]] = ump.get(s[end], 0) + 1
while len(ump) > k:
ump[s[start]] = ump.get(s[start], 0) - 1
if ump[s[start]] == 0:
del ump[s[start]]
start += 1
maxi = max(maxi, end - start + 1)
return maxi
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
characterSet = {}
start = 0
result = ""
i = 0
while i < len(s):
if characterSet.get(s[i]) is not None:
characterSet[s[i]] += 1
elif len(characterSet) == k:
if i - start > len(result):
result = s[start:i]
characterSet[s[start]] -= 1
if characterSet[s[start]] == 0:
del characterSet[s[start]]
start += 1
i -= 1
else:
characterSet[s[i]] = 1
i += 1
if i - start > len(result) and len(characterSet) == k:
result = s[start:i]
return len(result) if result else -1
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NONE VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR VAR NUMBER
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
i = 0
n = len(s)
j = 0
res = ""
maxres = 0
d = {}
while j < n:
res += s[j]
if s[j] in d:
d[s[j]] += 1
else:
d[s[j]] = 1
if len(d) > k:
while len(d) > k:
d[s[i]] -= 1
if d[s[i]] == 0:
d.pop(s[i])
res = res[1:]
i += 1
elif len(d) == k:
maxres = max(maxres, len(res))
j += 1
if maxres > 0:
return maxres
return -1
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR DICT WHILE VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER RETURN VAR RETURN NUMBER
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
map = {}
start = 0
end = 0
maxlen = -1
while end < len(s):
map[s[end]] = map.get(s[end], 0) + 1
if len(map) == k:
maxlen = max(maxlen, end - start + 1)
while len(map) > k:
map[s[start]] -= 1
if map[s[start]] == 0:
map.pop(s[start])
start += 1
end += 1
return maxlen
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP 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 NUMBER RETURN VAR
|
Given a string you need to print the size of the longest possible substring that has exactly K unique characters. If there is no possible substring then print -1.
Example 1:
Input:
S = "aabacbebebe", K = 3
Output: 7
Explanation: "cbebebe" is the longest
substring with K distinct characters.
Example 2:
Input:
S = "aaaa", K = 2
Output: -1
Explanation: There's no substring with K
distinct characters.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestKSubstr() which takes the string S and an integer K as input and returns the length of the longest substring with exactly K distinct characters. If there is no substring with exactly K distinct characters then return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1 ≤ |S| ≤ 10^{5}
1 ≤ K ≤ 10^{5}
All characters are lowercase latin characters.
|
class Solution:
def longestKSubstr(self, s, k):
i = 0
j = 0
n = len(s)
unique = {}
longest = 0
while j < n:
unique[s[j]] = unique.get(s[j], 0) + 1
if len(unique) == k:
longest = max(longest, j - i + 1)
elif len(unique) > k:
while len(unique) > k:
unique[s[i]] = unique.get(s[i]) - 1
if unique[s[i]] == 0:
del unique[s[i]]
i += 1
j += 1
if longest != 0:
return longest
return -1
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN VAR RETURN NUMBER
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
for _ in range(int(input())):
n, r = map(int, input().split(" "))
s = input()
res = ""
prev_s, last_res = s[:r], s[r:]
p1, p2 = 0, r - 1
while p1 <= p2:
res += prev_s[p1]
if p1 != p2:
res += prev_s[p2]
p1 += 1
p2 -= 1
print(res[::-1] + last_res)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
n = int(input())
for i in range(n):
a, b = map(int, input().split())
s = input()
lst = s[b:]
s = s[:b]
r = ""
for i in range(len(s) // 2):
r += s[i]
r += s[-(i + 1)]
if len(s) % 2:
r += s[len(s) // 2]
r = r[::-1]
print(r + lst)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
import sys
def reverse(answer, i):
for j in range(i // 2):
answer[j], answer[i - j - 1] = answer[i - j - 1], answer[j]
def findOriginalString(N, K, string):
answer = []
answer.append(string[K // 2])
if K % 2:
x = 2
flag = -1
j = K // 2 + 1
while j >= 0 and j < K:
answer.append(string[j])
if flag == -1:
j -= x
flag = 1
else:
j += x
flag = -1
x += 1
else:
x = 2
flag = -1
j = K // 2 - 1
while j >= 0 and j < K:
answer.append(string[j])
if flag == -1:
j += x
flag = 1
else:
j -= x
flag = -1
x += 1
for i in range(K, N):
answer.append(string[i])
return "".join(answer)
def main():
test_case = int(sys.stdin.readline())
for tc in range(test_case):
N, K = map(int, sys.stdin.readline().split())
string = sys.stdin.readline().strip("\n")
print(findOriginalString(N, K, string))
main()
|
IMPORT FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL STRING VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
for _ in range(int(input())):
n, k = map(int, input().split())
s = input()
t = ""
i = 0
p = k - 1
while i < p:
t = s[i] + t
t = s[p] + t
i += 1
p -= 1
if k % 2 != 0:
t = s[i] + t
print(t + s[k:])
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
for _ in range(int(input())):
n, k = map(int, input().split())
arr = list(str(input()))[:n]
narr = []
i = 0
j = k - 1
while i < j:
narr.append(arr[i])
narr.append(arr[j])
i += 1
j -= 1
if i == j:
narr.append(arr[i])
farr = narr[::-1] + arr[k:n]
print("".join(farr))
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
t = int(input())
for _ in range(t):
n, k = map(int, input().split())
s = list(input())
indlist = []
if k % 2 == 0:
count = k
x = 0
for i in range(k):
indlist.append(count)
if count - 2 == 0:
x = 1
count = -1
if x == 0:
count -= 2
else:
count += 2
else:
count = k
x = 0
for i in range(k):
indlist.append(count)
if count == 1:
x = 1
count = 0
if x == 0:
count -= 2
else:
count += 2
sliced = s[0:k]
for i in range(len(indlist)):
s[indlist[i] - 1] = sliced[i]
for i in s:
print(i, end="")
print()
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
t = int(input())
while t:
n, k = map(int, input().split())
s = input()
s1 = ""
if k % 2 != 0:
s1 += s[k // 2]
i = k // 2 + 1
j = k // 2 - 1
else:
i = k // 2
j = k // 2 - 1
while j >= 0 and i < k:
s1 += s[i]
s1 += s[j]
i += 1
j -= 1
s1 += s[k:]
print(s1)
t -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING IF BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
t = int(input())
while t > 0:
t -= 1
n, k = [int(k) for k in input().split(" ")]
s = str(input())
rev = s[:k]
orig = s[k:]
ans = []
while len(rev) > 0:
if len(rev) > 0:
ans.append(rev[0])
rev = rev[1:]
if len(rev) > 0:
ans.append(rev[-1])
rev = rev[:-1]
ans.reverse()
for c in orig:
ans.append(c)
print("".join(ans))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
ans = []
testcase = int(input())
for i in range(0, testcase):
string = ""
li = []
flag = 0
lenght, key = map(int, input().split())
ss = list(input())
for i in range(lenght):
li.append([ss[i], i + 1])
for i in range(lenght):
if key == 0:
key = 1
flag = 1
elif key == -1:
key = 2
flag = 1
li[i][1] = key
if flag == 0:
key -= 2
elif flag == 1:
key += 2
li.sort(key=lambda x: x[1])
for ele in range(lenght):
string = string + li[ele][0]
ans.append(string)
for sol in ans:
print(sol)
|
ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
n = int(input())
for i in range(n):
length, moves = [int(x) for x in str(input()).split(" ")]
word = str(input())
a = ""
b = word[moves:]
c = word[:moves]
is_odd = moves % 2 != 0
for j in range(moves // 2):
a = c[j] + a
a = c[moves - 1 - j] + a
if is_odd:
a = c[moves // 2] + a
print(a + b)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
t = int(input())
for i in range(t):
n, k = map(int, input().split())
str = input()
ans = ""
s = 0
e = k - 1
while s < e:
ans += str[s]
ans += str[e]
s += 1
e -= 1
if s == e:
ans += str[s]
print(ans[::-1] + str[k:])
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
def check(n, k, s):
sharma = s[0:k]
data = s[k:]
ans = ""
for i in range(0, k // 2):
ans = sharma[-(i + 1)] + sharma[i] + ans
if k % 2 != 0:
ans = sharma[k // 2 : k // 2 + 1] + ans
return ans + data
t = int(input())
for _ in range(t):
n, k = map(int, input().split())
s = input()
print(check(n, k, s))
|
FUNC_DEF ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
for _ in range(int(input())):
n, k = map(int, input().split())
s = input()
d = k
l = k
x = ""
x += s[d // 2]
if l % 2 == 1:
i = 1
while i <= d // 2:
x += s[d // 2 + i]
x += s[d // 2 - i]
i += 1
print(x + s[d:])
else:
i = 1
while i < d // 2:
x += s[d // 2 - i]
x += s[d // 2 + i]
i += 1
print(x + s[0] + s[d:])
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR STRING VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
t = int(input())
for i in range(t):
n, k = map(int, input().split())
s = input()
ans = ""
even = 0
odd = 0
for j in range(k):
if j % 2 == 0:
ans += s[j - even]
even += 1
else:
ans += s[k - j + odd]
odd += 1
ans_1 = ans[::-1]
ans_1 += s[k:]
print(ans_1)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
def rev(x):
return x[::-1]
def func():
n, k = map(int, input().split())
s = input()
l = 0
r = k - 1
ans = ""
while l <= r:
ans += s[l]
l += 1
if l <= r:
ans += s[r]
r -= 1
ans = rev(ans)
for i in range(k, n):
ans += s[i]
print(ans)
t = int(input())
for i in range(t):
func()
|
FUNC_DEF RETURN VAR NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR STRING WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
for _ in range(int(input())):
n, k = list(map(int, input().split()))
str1 = input()
str2 = str1[k:]
emp = ""
i = 0
j = k - 1
while i < j:
emp += str1[i]
emp += str1[j]
i += 1
j -= 1
if i == j:
emp += str1[j]
k1 = emp[::-1]
print(k1 + str2)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
for _ in range(int(input())):
n, k = map(int, input().split())
s = input()
ans = list(s)
ans[k - 1 :: -2] = s[0 : (k + 1) // 2]
ans[k % 2 : k : 2] = s[(k + 1) // 2 : k]
print("".join(ans))
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
for _ in range(int(input())):
n, k = map(int, input().split())
srt = input()
kstr = srt[:k]
lst_str = srt[k:]
oprn_lst = [" "] * (n + 1)
odd = False
if k % 2 == 1:
odd = True
nm = k // 2
k -= 1
cnt = 0
fin_ind = 0
for i in range(nm):
oprn_lst[n - cnt] = kstr[i]
oprn_lst[n - cnt - 1] = kstr[k - i]
cnt += 2
fin_ind += 1
if odd:
oprn_lst[0] = kstr[fin_ind]
print("".join(oprn_lst).replace(" ", "") + lst_str)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL FUNC_CALL STRING VAR STRING STRING VAR
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
try:
t = int(input())
while t:
n, k = map(int, input().split())
s = input()
org = ""
i, j = 0, k - 1
while i < j:
org += s[i]
org += s[j]
i += 1
j -= 1
if i == j:
org += s[i]
org = org[::-1] + s[k:n]
print(org)
t -= 1
except EOFError as e:
pass
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
import sys
for _ in range(int(input())):
n, k = map(int, input().split())
s = str(input())
a = ""
p = int(k / 2)
for i in range(k):
if i % 2 == 0:
if k % 2 == 0:
p += i
else:
p -= i
elif k % 2 == 0:
p -= i
else:
p += i
a += s[p]
print(a + s[k:])
|
IMPORT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
def solve(s, k):
res = ["-"] * len(s)
res[-len(s) + k :] = s[-len(s) + k :]
is_left = True
i = -len(s) + k - 1
left, right = 0, k - 1
while left <= right:
if is_left:
res[i] = s[left]
left += 1
else:
res[i] = s[right]
right -= 1
i -= 1
is_left = not is_left
return "".join(res)
def main():
for _ in range(int(input())):
n, k = map(int, input().split())
s = input()
result = solve(s, k)
print(result)
main()
|
FUNC_DEF ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR IF VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR RETURN FUNC_CALL STRING VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
t = int(input())
for i in range(t):
n, k = map(int, input().split())
l = [0] * n
s = input()
c = 0
if k % 2 == 0:
for j in range(k - 1, 0, -2):
l[j] = s[c]
c += 1
for x in range(n):
if l[x] == 0:
l[x] = s[c]
c += 1
ans = ""
for y in l:
ans += y
print(ans)
else:
for z in range(k - 1, -1, -2):
l[z] = s[c]
c += 1
for w in range(n):
if l[w] == 0:
l[w] = s[c]
c += 1
ans = ""
for q in l:
ans += q
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR STRING FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR STRING FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
for t in range(int(input())):
n, k = [int(i) for i in input().split()]
s = input()
original_right = s[k:]
tl = s[:k]
sl = ""
l = 0
r = k - 1
steps = k
while l != r > 0:
sl += tl[l]
if l < r:
l += 1
else:
l -= 1
l, r = r, l
sl += tl[l]
print(sl[::-1] + original_right)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
for _ in range(int(input())):
n, r = map(int, input().split())
s = list(input())
ans = ""
s1 = r // 2
s2 = r // 2 - 1
if r % 2 == 1:
print(s[s1], end="")
s1 += 1
for i in range(r // 2):
print(s[s1], end="")
print(s[s2], end="")
s2 -= 1
s1 += 1
print(ans, end="")
print("".join(s[r:]))
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
for _ in range(int(input())):
n, k = map(int, input().split())
s1 = input()
s = list(s1)
x = 0
for i in range(k - 1, -1, -2):
s[i] = s1[x]
x += 1
for j in range((i + 1) % 2, k - 1, 2):
s[j] = s1[x]
x += 1
print("".join(s))
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
def inpNum():
return int(input())
def inpNums():
return tuple(list(map(int, input().split())))
def inpList():
return list(map(int, input().split()))
def solve(t: int):
N, K = inpNums()
s = input()
ans = ""
dir = 1 if K % 2 == 0 else -1
step = 0
i = int(K / 2)
while K:
idx = i + step * dir
dir *= -1
step += 1
K -= 1
i = idx
ans = ans + s[idx]
for i in range(len(ans), N):
ans = ans + s[i]
print(ans)
for t in range(int(input())):
solve(t)
|
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
t = int(input())
while t > 0:
n, k = map(int, input().split())
S = input()
p1 = S[:k]
p2 = S[k:n]
p11 = p1[: k // 2]
p11 = p11[::-1]
p12 = p1[k // 2 :]
ans = ""
if k % 2 == 0:
for i in range(k // 2):
ans = ans + p12[i] + p11[i]
else:
ans = ans + p12[0]
for i in range(1, k // 2 + 1):
ans = ans + p12[i] + p11[i - 1]
print(ans + p2)
t -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR STRING IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
for t in range(int(input())):
n, k = map(int, input().split())
s = input()
mid = k // 2
if k % 2 == 1:
ans, i, j = s[mid], mid - 1, mid + 1
while i >= 0:
ans += s[j]
ans += s[i]
i -= 1
j += 1
else:
ans, j, i = "", mid, mid - 1
while i >= 0:
ans += s[j]
ans += s[i]
i -= 1
j += 1
for i in range(k, n):
ans += s[i]
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR STRING VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
for _ in range(int(input())):
n, k = map(int, input().split())
s = input()
i = 0
j = k - 1
res = ""
while i < j:
res += s[i]
res += s[j]
i += 1
j -= 1
if i == j:
res = res + s[i]
m = res[::-1]
o = s[k:]
print(m + o)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR STRING WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
for _ in range(int(input())):
n, d = map(int, input().split())
st = input()
par = +1
if d % 2 == 1:
par = -1
s = ""
j = d // 2
for i in range(d):
j += par * i
s += st[j]
par = -par
try:
print(s + st[d:])
except:
print(s)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
for _ in range(int(input())):
n, k = map(int, input().split())
inp = input()
pref = ""
i = 0
l = 0
while l <= k:
if i != k - i - 1:
pref += f"{inp[i]}{inp[k - i - 1]}"
l += 2
i += 1
else:
pref += inp[i]
break
if l == k:
break
print(pref[::-1] + inp[k:])
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
ans = ""
for _ in range(int(input())):
n, k = list(map(int, input().split()))
s = input()
sub1 = ""
sub2 = s[k:]
l, r = 0, k - 1
while l <= r:
sub1 += s[l]
if l != r:
sub1 += s[r]
r -= 1
l += 1
ans += sub1[::-1] + sub2 + "\n"
print(ans)
|
ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR STRING EXPR FUNC_CALL VAR VAR
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
t = int(input())
for i in range(t):
n, m = map(int, input().split())
l = input()
le = []
ri = []
r = [None] * m
if m % 2 == 0:
ri = l[m // 2 : m]
le = l[m // 2 - 1 :: -1]
r[::2] = ri
r[1::2] = le
r[m:] = l[m:]
else:
ri = l[m // 2 + 1 : m]
le = l[m // 2 :: -1]
r[1::2] = ri
r[::2] = le
r[m:] = l[m:]
print(*r, sep="")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NONE VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
T = int(input())
for _ in range(T):
N, K = map(int, input().split())
S = input()
res = ""
i = 0
j = K - 1
while i < j:
res = res + S[i]
res = res + S[j]
i = i + 1
j = j - 1
if i == j:
res = res + S[i]
res = res[::-1]
tmp = res + S[K:N]
print(tmp)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
for _ in range(int(input())):
n, k = list(map(int, input().split()))
s = input()
p1 = k // 2
p2 = p1 + 1
flag = True
if k % 2 == 0:
flag = False
p1 -= 1
p2 -= 1
tmp = k
while tmp > 0:
if flag:
print(s[p1], end="")
p1 -= 1
flag = False
else:
print(s[p2], end="")
p2 += 1
flag = True
tmp -= 1
for i in range(k, n):
print(s[i], end="")
print()
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
test_cases = int(input())
for _ in range(test_cases):
N, K = map(int, input().split())
S = input().rstrip("\n")
S1 = list(S)
Current_Character = 0
for i in range(K, 0, -2):
S1[i - 1] = S[Current_Character]
Current_Character += 1
Next = 1 + K % 2
for i in range(Next, K, 2):
S1[i - 1] = S[Current_Character]
Current_Character += 1
print("".join(S1))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
x = int(input())
for i in range(x):
n, k = input().split()
n = int(n)
k = int(k)
a = input()
if k & 1 == 0:
z = k // 2
b = 1
while z < k:
print(a[z] + a[z - b], end="")
b += 2
z += 1
for ll in range(k, n):
print(a[ll], end="")
else:
z = k // 2
b = 2
print(a[z], end="")
z += 1
while z < k:
print(a[z] + a[z - b], end="")
b += 2
z += 1
for ll in range(k, n):
print(a[ll], end="")
print()
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR STRING VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR STRING VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
def soln(n, k, s):
beg = 0
end = k - 1
res = [""] * n
for i in range(k):
if not i & 1:
res[i] = s[beg]
beg += 1
else:
res[i] = s[end]
end -= 1
res[:k] = res[k - 1 :: -1]
for i in range(k, n):
res[i] = s[i]
return "".join(res)
for _ in range(int(input())):
n, k = map(int, input().split())
s = input()
print(soln(n, k, s))
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
for _ in range(int(input())):
m, n = map(int, input().split())
s = input()
mainarr = list(s)
arr = []
arr[:] = mainarr[:n]
newarr = []
l, r = 0, n - 1
for i in range(0, n):
if i % 2 == 0:
newarr.append(arr[l])
l += 1
else:
newarr.append(arr[r])
r -= 1
newarr = newarr[::-1]
ans = newarr + mainarr[n:]
print("".join(ans))
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
Alice initially has a string S of length N, and she decides to modify it! Her modification consists of K steps numbered from 1 to K. In the i-th step, Alice reverses the prefix of length i of S.
For example, suppose Alice starts with S = \texttt{abferty}, and she modifies it via 3 steps, then:
After the first step, \textcolor{blue}{\texttt{a}}\texttt{bferty} \rightarrow S = \texttt{abferty}
After the second step, \textcolor{blue}{\texttt{ab}}\texttt{ferty} \rightarrow S = \texttt{baferty}
After the third step, \textcolor{blue}{\texttt{baf}}\texttt{erty} \rightarrow S = \texttt{faberty}
So after 3 steps, her string becomes S = \texttt{faberty}.
After performing her K-step modification, Alice ends up with the string S'. Now she challenges you to find the original string S. Can you do it?
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The test cases then follow.
- The first line of the test case contains two space-separated integers N and K - the length of the string and the number of steps in Alice's modification.
- The second line of the test case contains S' - the final string obtained after the modification.
------ Output Format ------
For each test case, output the original string S.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ N$
$|S'| = N$
$S'$ contains only lowercase alphabets
- Sum of $N$ overall cases does not exceed $10^{6}$.
------ subtasks ------
Subtask $1$ (30 points): $1 ≤ N \cdot K ≤ 2000$
Subtask $2$ (70 points): Original constraints
----- Sample Input 1 ------
3
7 3
faberty
7 5
bbaaaba
4 4
zxwy
----- Sample Output 1 ------
abferty
aababba
wxyz
----- explanation 1 ------
- Test case $2$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{a}}\texttt{ababba} \rightarrow S = \texttt{aababba}$
- $2$-nd step: $\textcolor{blue}{\texttt{aa}}\texttt{babba} \rightarrow S = \texttt{aababba}$
- $3$-rd step: $\textcolor{blue}{\texttt{aab}}\texttt{abba} \rightarrow S = \texttt{baaabba}$
- $4$-th step: $\textcolor{blue}{\texttt{baaa}}\texttt{bba} \rightarrow S = \texttt{aaabbba}$
- $5$-th step: $\textcolor{blue}{\texttt{aaabb}}\texttt{ba} \rightarrow S = \texttt{bbaaaba}$
- Test case $3$: The modification steps from $S$ to $S'$ is as follow:
- $1$-st step: $\textcolor{blue}{\texttt{w}}\texttt{xyz} \rightarrow S = \texttt{wxyz}$
- $2$-nd step: $\textcolor{blue}{\texttt{wx}}\texttt{yz} \rightarrow S = \texttt{xwyz}$
- $3$-rd step: $\textcolor{blue}{\texttt{xwy}}\texttt{z} \rightarrow S = \texttt{ywxz}$
- $4$-th step: $\textcolor{blue}{\texttt{ywxz}} \rightarrow S = \texttt{zxwy}$
|
t = int(input())
while t:
n, k = map(int, input().split())
s = input()
a = s[0:k]
b = s[k:]
c = []
var1 = 0
var2 = k - 1
for i in range(k):
if i % 2 == 0:
c.append(a[var1])
var1 = var1 + 1
else:
c.append(a[var2])
var2 = var2 - 1
d = c[::-1]
e = "".join(d)
res = e + b
print(res)
t = t - 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
|
Given a string that consists of only uppercase English letters, you can replace any letter in the string with another letter at most k times. Find the length of a longest substring containing all repeating letters you can get after performing the above operations.
Note:
Both the string's length and k will not exceed 104.
Example 1:
Input:
s = "ABAB", k = 2
Output:
4
Explanation:
Replace the two 'A's with two 'B's or vice versa.
Example 2:
Input:
s = "AABABBA", k = 1
Output:
4
Explanation:
Replace the one 'A' in the middle with 'B' and form "AABBBBA".
The substring "BBBB" has the longest repeating letters, which is 4.
|
class Solution:
def characterReplacement(self, s, k):
start = end = maxn = 0
alpha_dict = collections.defaultdict(int)
for end in range(1, len(s) + 1):
alpha_dict[s[end - 1]] += 1
maxn = max(maxn, alpha_dict[s[end - 1]])
if end - start > k + maxn:
alpha_dict[s[start]] -= 1
start += 1
return end - start
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR
|
Given a string that consists of only uppercase English letters, you can replace any letter in the string with another letter at most k times. Find the length of a longest substring containing all repeating letters you can get after performing the above operations.
Note:
Both the string's length and k will not exceed 104.
Example 1:
Input:
s = "ABAB", k = 2
Output:
4
Explanation:
Replace the two 'A's with two 'B's or vice versa.
Example 2:
Input:
s = "AABABBA", k = 1
Output:
4
Explanation:
Replace the one 'A' in the middle with 'B' and form "AABBBBA".
The substring "BBBB" has the longest repeating letters, which is 4.
|
class Solution:
def characterReplacement(self, s, k):
if s == "":
return 0
count = {}
lo = 0
hi = 0
max_letter = 0
for hi in range(len(s)):
try:
count[s[hi]] += 1
except:
count[s[hi]] = 1
if count[s[hi]] > max_letter:
max_letter = count[s[hi]]
if max_letter < hi - lo + 1 - k:
if max_letter == count[s[lo]]:
max_letter -= 1
count[s[lo]] -= 1
lo += 1
return hi - lo + 1
|
CLASS_DEF FUNC_DEF IF VAR STRING RETURN NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER
|
Given a string that consists of only uppercase English letters, you can replace any letter in the string with another letter at most k times. Find the length of a longest substring containing all repeating letters you can get after performing the above operations.
Note:
Both the string's length and k will not exceed 104.
Example 1:
Input:
s = "ABAB", k = 2
Output:
4
Explanation:
Replace the two 'A's with two 'B's or vice versa.
Example 2:
Input:
s = "AABABBA", k = 1
Output:
4
Explanation:
Replace the one 'A' in the middle with 'B' and form "AABBBBA".
The substring "BBBB" has the longest repeating letters, which is 4.
|
class Solution:
def characterReplacement(self, s, k):
ls = len(s)
start, end, max_len = 0, 0, 0
char_map, cnt, cur_max_char = {}, 0, 0
while end < ls:
if s[end] in char_map:
char_map[s[end]] += 1
else:
char_map[s[end]] = 1
cnt += 1
if char_map[s[end]] > cur_max_char:
cur_max_char = char_map[s[end]]
if cnt - cur_max_char > k:
char_map[s[start]] -= 1
start += 1
cnt -= 1
else:
max_len = max(max_len, cnt)
end += 1
return max_len
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR DICT NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, S, K):
char_freq = [0] * 26
l, res, distinct = 0, 0, 0
for i in range(len(S)):
indx = ord(S[i]) - ord("a")
char_freq[indx] += 1
if char_freq[indx] == 1:
distinct += 1
if i >= K:
char_freq[ord(S[l]) - ord("a")] -= 1
if not char_freq[ord(S[l]) - ord("a")]:
distinct -= 1
l += 1
if i >= K - 1 and distinct == K - 1:
res += 1
return res
|
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, s, k):
count = 0
mp = {}
for i in range(k):
if s[i] not in mp:
mp[s[i]] = 1
else:
mp[s[i]] += 1
if len(mp) == k - 1:
count += 1
j = 0
n = len(s)
for i in range(k, n):
a = s[i]
b = s[j]
if b in mp:
mp[b] -= 1
if mp[b] == 0:
del mp[b]
if a not in mp:
mp[a] = 1
else:
mp[a] += 1
j += 1
if len(mp) == k - 1:
count += 1
return count
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, s, k):
arr = [(0) for i in range(26)]
i = 0
j = 0
n = len(s)
c = 0
count = 0
while j < n:
arr[ord(s[j]) - 97] += 1
if arr[ord(s[j]) - 97] == 1:
c += 1
if j - i + 1 == k:
if c == k - 1:
count += 1
arr[ord(s[i]) - 97] -= 1
if arr[ord(s[i]) - 97] == 0:
c -= 1
i += 1
j += 1
return count
|
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 ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, s, k):
i, j = 0, 0
def check(d, k):
count = 0
fl = 0
for i in d:
if d[i] != 0:
if d[i] > 2:
return False
elif d[i] == 2:
if fl == 1:
return False
else:
fl = 1
count += 1
if count == k - 1:
return True
output = 0
d = {}
while j < len(s):
if j - i + 1 < k:
if s[j] in d:
d[s[j]] += 1
else:
d[s[j]] = 1
j += 1
elif j - i + 1 == k:
if s[j] in d:
d[s[j]] += 1
else:
d[s[j]] = 1
if check(d, k):
output += 1
d[s[i]] -= 1
j += 1
i += 1
return output
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER IF VAR VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT WHILE VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, S, K):
if len(S) < K:
return 0
d = {}
uniq = 0
ans = 0
for i in range(K):
if S[i] in d:
d[S[i]] += 1
else:
uniq += 1
d[S[i]] = 1
if uniq == K - 1:
ans += 1
r = K
while r < len(S):
d[S[r - K]] -= 1
if d[S[r - K]] == 0:
uniq -= 1
del d[S[r - K]]
if S[r] in d:
d[S[r]] += 1
else:
uniq += 1
d[S[r]] = 1
if uniq == K - 1:
ans += 1
r += 1
return ans
|
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN NUMBER 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 IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, S, k):
l = 0
ans = 0
map = [0] * 26
for i in range(k):
if map[ord(S[i]) - ord("a")] == 0:
map[ord(S[i]) - ord("a")] = 1
l += 1
else:
map[ord(S[i]) - ord("a")] += 1
if k - 1 == l:
ans += 1
i = 0
j = k
while j < len(S):
map[ord(S[i]) - ord("a")] -= 1
if map[ord(S[i]) - ord("a")] == 0:
l -= 1
if map[ord(S[j]) - ord("a")] > 0:
map[ord(S[j]) - ord("a")] += 1
if map[ord(S[j]) - ord("a")] == 0:
l += 1
map[ord(S[j]) - ord("a")] += 1
if k - 1 == l:
ans += 1
i += 1
j += 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, S, K):
res = list()
l = 0
n = len(S)
count = 0
map = dict()
for r in range(n):
map[S[r]] = map.get(S[r], 0) + 1
if r - l + 1 == K:
if len(map) == K - 1:
count += 1
if map.get(S[l], 0) > 1:
map[S[l]] = map.get(S[l]) - 1
else:
del map[S[l]]
l += 1
return count
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, S, K):
if K > 27:
return 0
result = 0
for i in range(len(S) - K + 1):
A = S[i : i + K]
if len(set(A)) == K - 1:
result += 1
return result
|
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, s, k):
ans = {}
res = 0
for i in range(k):
ans[s[i]] = ans.get(s[i], 0) + 1
if len(ans) == k - 1:
res += 1
for i in range(k, len(s)):
ans[s[i - k]] -= 1
if ans[s[i - k]] == 0:
del ans[s[i - k]]
ans[s[i]] = ans.get(s[i], 0) + 1
if len(ans) == k - 1:
res += 1
return res
|
CLASS_DEF FUNC_DEF 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 IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, S, K):
res = 0
i, j = 0, 0
map = {}
while i < len(S):
map[S[i]] = 1 + map.get(S[i], 0)
if i + 1 >= K:
if len(map) == K - 1:
res = res + 1
map[S[j]] -= 1
if map[S[j]] == 0:
del map[S[j]]
j = j + 1
i = i + 1
return res
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR DICT WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER 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 a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, S, K):
d = dict()
dist = 0
for i in range(K):
j = S[i]
if j in d.keys():
d[j] = d.get(j) + 1
else:
dist += 1
d[j] = 1
tot = 0
if dist == K - 1:
tot += 1
top_ind = 0
for i in S[K:]:
top = S[top_ind]
if d[top] == 1:
del d[top]
dist -= 1
else:
d[top] = d.get(top) - 1
if i in d.keys():
d[i] = d.get(i) + 1
else:
dist += 1
d[i] = 1
if dist == K - 1:
tot += 1
top_ind += 1
return tot
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, S, K):
x = 0
s = {}
for i in range(K):
if s.get(S[i]):
s[S[i]] += 1
else:
s[S[i]] = 1
i, j = 0, K
while j <= len(S):
if len(s) == K - 1:
x += 1
if j != len(S):
if s[S[i]] == 1:
s.pop(S[i])
else:
s[S[i]] -= 1
if s.get(S[j]):
s[S[j]] += 1
else:
s[S[j]] = 1
i, j = i + 1, j + 1
return x
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, s, k):
i = 0
j = 0
c = 0
dic = {}
while j < len(s):
if s[j] not in dic:
dic[s[j]] = 1
else:
dic[s[j]] += 1
if j - i + 1 == k:
if len(dic) == k - 1:
c += 1
dic[s[i]] -= 1
if dic[s[i]] == 0:
dic.pop(s[i])
i += 1
j += 1
return c
|
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 ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, S, K):
hm = {}
final_answer = 0
for i in range(K):
if S[i] in hm:
hm[S[i]] += 1
else:
hm[S[i]] = 1
if len(hm) == K - 1:
final_answer += 1
i = 0
j = K
while j < len(S):
hm[S[i]] -= 1
if hm[S[i]] == 0:
del hm[S[i]]
if S[j] in hm:
hm[S[j]] += 1
else:
hm[S[j]] = 1
if len(hm) == K - 1:
final_answer += 1
j += 1
i += 1
return final_answer
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, S, K):
fr = [0] * 26
d = 0
c = 0
for i in range(K):
fr[ord(S[i]) - 97] += 1
if fr[ord(S[i]) - 97] == 1:
d += 1
if d == K - 1:
c += 1
le = 0
ri = K
while ri < len(S):
fr[ord(S[le]) - 97] -= 1
if fr[ord(S[le]) - 97] == 0:
d -= 1
fr[ord(S[ri]) - 97] += 1
if fr[ord(S[ri]) - 97] == 1:
d += 1
if d == K - 1:
c += 1
le += 1
ri += 1
return c
|
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.