description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals.
Example 1:
Input:
N = 8
in[] = 4 8 2 5 1 6 3 7
post[] =8 4 5 2 6 7 3 1
Output: 1 2 4 8 5 3 6 7
Explanation: For the given postorder and
inorder traversal of tree the resultant
binary tree will be
1
/ \
2 3
/ \ / \
4 5 6 7
\
8
Example 2:
Input:
N = 5
in[] = 9 5 2 3 4
post[] = 5 9 3 4 2
Output: 2 9 5 4 3
Explanation:
the resultant binary tree will be
2
/ \
9 4
\ /
5 3
Your Task:
You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree.
The generated output contains the preorder traversal of the constructed tree.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{3}
1 <= in[i], post[i] <= 10^{3} | def buildTree(In, post, n):
if In:
index = In.index(post.pop())
root = Node(In[index])
root.right = buildTree(In[index + 1 :], post, n)
root.left = buildTree(In[:index], post, n)
return root | FUNC_DEF IF VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR |
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals.
Example 1:
Input:
N = 8
in[] = 4 8 2 5 1 6 3 7
post[] =8 4 5 2 6 7 3 1
Output: 1 2 4 8 5 3 6 7
Explanation: For the given postorder and
inorder traversal of tree the resultant
binary tree will be
1
/ \
2 3
/ \ / \
4 5 6 7
\
8
Example 2:
Input:
N = 5
in[] = 9 5 2 3 4
post[] = 5 9 3 4 2
Output: 2 9 5 4 3
Explanation:
the resultant binary tree will be
2
/ \
9 4
\ /
5 3
Your Task:
You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree.
The generated output contains the preorder traversal of the constructed tree.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{3}
1 <= in[i], post[i] <= 10^{3} | postIndex = -1
def helper(d, post, l, r):
global postIndex
if l > r:
return None
x = post[postIndex]
postIndex -= 1
index = d[x]
node = Node(x)
if l == r:
return node
node.right = helper(d, post, index + 1, r)
node.left = helper(d, post, l, index - 1)
return node
def buildTree(In, post, n):
global postIndex
postIndex = n - 1
d = dict()
for i in range(n):
d[In[i]] = i
return helper(d, post, 0, n - 1) | ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER |
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals.
Example 1:
Input:
N = 8
in[] = 4 8 2 5 1 6 3 7
post[] =8 4 5 2 6 7 3 1
Output: 1 2 4 8 5 3 6 7
Explanation: For the given postorder and
inorder traversal of tree the resultant
binary tree will be
1
/ \
2 3
/ \ / \
4 5 6 7
\
8
Example 2:
Input:
N = 5
in[] = 9 5 2 3 4
post[] = 5 9 3 4 2
Output: 2 9 5 4 3
Explanation:
the resultant binary tree will be
2
/ \
9 4
\ /
5 3
Your Task:
You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree.
The generated output contains the preorder traversal of the constructed tree.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{3}
1 <= in[i], post[i] <= 10^{3} | def buildTree(In, post, n):
global preOrderIndex
preOrderIndex = n - 1
def solve(In, post, inorderStart, inorderEnd):
global preOrderIndex
if preOrderIndex < 0 or inorderStart > inorderEnd:
return None
root = Node(post[preOrderIndex])
preOrderIndex -= 1
for i in range(inorderStart, inorderEnd + 1):
if In[i] == root.data:
break
root.right = solve(In, post, i + 1, inorderEnd)
root.left = solve(In, post, inorderStart, i - 1)
return root
n = len(In)
ans = solve(In, post, 0, n - 1)
return ans | FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER RETURN VAR |
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals.
Example 1:
Input:
N = 8
in[] = 4 8 2 5 1 6 3 7
post[] =8 4 5 2 6 7 3 1
Output: 1 2 4 8 5 3 6 7
Explanation: For the given postorder and
inorder traversal of tree the resultant
binary tree will be
1
/ \
2 3
/ \ / \
4 5 6 7
\
8
Example 2:
Input:
N = 5
in[] = 9 5 2 3 4
post[] = 5 9 3 4 2
Output: 2 9 5 4 3
Explanation:
the resultant binary tree will be
2
/ \
9 4
\ /
5 3
Your Task:
You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree.
The generated output contains the preorder traversal of the constructed tree.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{3}
1 <= in[i], post[i] <= 10^{3} | def buildTree(inorder, postorder, n):
lookup = dict()
for i, num in enumerate(inorder):
lookup[num] = i
return buildTreeRecu(lookup, postorder, inorder, len(postorder), 0, len(inorder))
def buildTreeRecu(lookup, postorder, inorder, postEnd, inStart, inEnd):
if inStart == inEnd:
return None
node = Node(postorder[postEnd - 1])
i = lookup[postorder[postEnd - 1]]
node.left = buildTreeRecu(
lookup, postorder, inorder, postEnd - 1 - (inEnd - i - 1), inStart, i
)
node.right = buildTreeRecu(lookup, postorder, inorder, postEnd - 1, i + 1, inEnd)
return node | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR |
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals.
Example 1:
Input:
N = 8
in[] = 4 8 2 5 1 6 3 7
post[] =8 4 5 2 6 7 3 1
Output: 1 2 4 8 5 3 6 7
Explanation: For the given postorder and
inorder traversal of tree the resultant
binary tree will be
1
/ \
2 3
/ \ / \
4 5 6 7
\
8
Example 2:
Input:
N = 5
in[] = 9 5 2 3 4
post[] = 5 9 3 4 2
Output: 2 9 5 4 3
Explanation:
the resultant binary tree will be
2
/ \
9 4
\ /
5 3
Your Task:
You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree.
The generated output contains the preorder traversal of the constructed tree.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{3}
1 <= in[i], post[i] <= 10^{3} | def buildTree(In, post, n):
i = n - 1
j = n - 1
root = Node("a")
st = []
st.append(root)
cur = root
while i >= 0:
if st[-1].data != In[j]:
if st[-1].data == cur.data:
cur.right = Node(post[i])
cur = cur.right
else:
cur.left = Node(post[i])
cur = cur.left
i -= 1
st.append(cur)
else:
j -= 1
cur = st.pop()
return root.right | FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER IF VAR NUMBER VAR VAR IF VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR RETURN VAR |
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals.
Example 1:
Input:
N = 8
in[] = 4 8 2 5 1 6 3 7
post[] =8 4 5 2 6 7 3 1
Output: 1 2 4 8 5 3 6 7
Explanation: For the given postorder and
inorder traversal of tree the resultant
binary tree will be
1
/ \
2 3
/ \ / \
4 5 6 7
\
8
Example 2:
Input:
N = 5
in[] = 9 5 2 3 4
post[] = 5 9 3 4 2
Output: 2 9 5 4 3
Explanation:
the resultant binary tree will be
2
/ \
9 4
\ /
5 3
Your Task:
You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree.
The generated output contains the preorder traversal of the constructed tree.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{3}
1 <= in[i], post[i] <= 10^{3} | def buildTree(inorder, post, n):
inmap = {}
for i, n in enumerate(inorder):
if inmap.get(n, False):
inmap[n].append(i)
else:
inmap[n] = [i]
return Util(inorder, 0, len(inorder) - 1, post, 0, len(inorder) - 1, inmap)
def findInRoot(num, inmap, offset):
val = inmap[num]
if len(val) > 0:
for each in val:
if each >= offset:
return each
return val[0]
def Util(inorder, instart, inend, post, pstart, pend, inmap):
if instart > inend or pstart > pend:
return None
num = post[pend]
inroot = findInRoot(num, inmap, instart)
numsleft = inroot - instart
node = Node(num)
node.left = Util(
inorder, instart, inroot - 1, post, pstart, pstart + numsleft - 1, inmap
)
node.right = Util(
inorder, inroot + 1, inend, post, pstart + numsleft, pend - 1, inmap
)
return node | FUNC_DEF ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_DEF ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR VAR RETURN VAR RETURN VAR NUMBER FUNC_DEF IF VAR VAR VAR VAR RETURN NONE ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR |
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals.
Example 1:
Input:
N = 8
in[] = 4 8 2 5 1 6 3 7
post[] =8 4 5 2 6 7 3 1
Output: 1 2 4 8 5 3 6 7
Explanation: For the given postorder and
inorder traversal of tree the resultant
binary tree will be
1
/ \
2 3
/ \ / \
4 5 6 7
\
8
Example 2:
Input:
N = 5
in[] = 9 5 2 3 4
post[] = 5 9 3 4 2
Output: 2 9 5 4 3
Explanation:
the resultant binary tree will be
2
/ \
9 4
\ /
5 3
Your Task:
You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree.
The generated output contains the preorder traversal of the constructed tree.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{3}
1 <= in[i], post[i] <= 10^{3} | def tree(In, post, start, end, n):
global index
if index < 0 or start > end:
return None
element = post[index]
index -= 1
root = Node(element)
for pos in range(start, end + 1):
if In[pos] == element:
break
root.right = tree(In, post, pos + 1, end, n)
root.left = tree(In, post, start, pos - 1, n)
return root
def buildTree(In, post, n):
global index
index = n - 1
root = tree(In, post, 0, n - 1, n)
return root | FUNC_DEF IF VAR NUMBER VAR VAR RETURN NONE ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR |
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals.
Example 1:
Input:
N = 8
in[] = 4 8 2 5 1 6 3 7
post[] =8 4 5 2 6 7 3 1
Output: 1 2 4 8 5 3 6 7
Explanation: For the given postorder and
inorder traversal of tree the resultant
binary tree will be
1
/ \
2 3
/ \ / \
4 5 6 7
\
8
Example 2:
Input:
N = 5
in[] = 9 5 2 3 4
post[] = 5 9 3 4 2
Output: 2 9 5 4 3
Explanation:
the resultant binary tree will be
2
/ \
9 4
\ /
5 3
Your Task:
You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree.
The generated output contains the preorder traversal of the constructed tree.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{3}
1 <= in[i], post[i] <= 10^{3} | def positioner(In, start, end, element):
i = start
while i <= end:
if In[i] == element:
return i
i += 1
def treeBuilder(In, post, n, index, start, end):
if index[0] < 0 or start > end:
return None
element = post[index[0]]
index[0] = index[0] - 1
root = Node(element)
position = positioner(In, start, end, element)
root.right = treeBuilder(In, post, n, index, position + 1, end)
root.left = treeBuilder(In, post, n, index, start, position - 1)
return root
def buildTree(In, post, n):
start = 0
end = n - 1
index = [n - 1]
ans = treeBuilder(In, post, n, index, start, end)
return ans | FUNC_DEF ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR VAR RETURN VAR VAR NUMBER FUNC_DEF IF VAR NUMBER NUMBER VAR VAR RETURN NONE ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR |
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals.
Example 1:
Input:
N = 8
in[] = 4 8 2 5 1 6 3 7
post[] =8 4 5 2 6 7 3 1
Output: 1 2 4 8 5 3 6 7
Explanation: For the given postorder and
inorder traversal of tree the resultant
binary tree will be
1
/ \
2 3
/ \ / \
4 5 6 7
\
8
Example 2:
Input:
N = 5
in[] = 9 5 2 3 4
post[] = 5 9 3 4 2
Output: 2 9 5 4 3
Explanation:
the resultant binary tree will be
2
/ \
9 4
\ /
5 3
Your Task:
You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree.
The generated output contains the preorder traversal of the constructed tree.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{3}
1 <= in[i], post[i] <= 10^{3} | def helper(inorder, postorder, indexS, indexE, mp):
global postindex
if indexS > indexE:
return None
root = Node(postorder[postindex])
postindex -= 1
index = mp[root.data]
root.right = helper(inorder, postorder, index + 1, indexE, mp)
root.left = helper(inorder, postorder, indexS, index - 1, mp)
return root
def buildTree(inorder, postorder, n):
global postindex
postindex = n - 1
mp = {}
for i in range(n):
mp[inorder[i]] = i
return helper(inorder, postorder, 0, n - 1, mp) | FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR |
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals.
Example 1:
Input:
N = 8
in[] = 4 8 2 5 1 6 3 7
post[] =8 4 5 2 6 7 3 1
Output: 1 2 4 8 5 3 6 7
Explanation: For the given postorder and
inorder traversal of tree the resultant
binary tree will be
1
/ \
2 3
/ \ / \
4 5 6 7
\
8
Example 2:
Input:
N = 5
in[] = 9 5 2 3 4
post[] = 5 9 3 4 2
Output: 2 9 5 4 3
Explanation:
the resultant binary tree will be
2
/ \
9 4
\ /
5 3
Your Task:
You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree.
The generated output contains the preorder traversal of the constructed tree.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{3}
1 <= in[i], post[i] <= 10^{3} | def buildUtil(In, post, inStrt, inEnd, pIndex):
if inStrt > inEnd:
return None
node = Node(post[pIndex[0]])
pIndex[0] -= 1
if inStrt == inEnd:
return node
iIndex = search(In, inStrt, inEnd, node.data)
node.right = buildUtil(In, post, iIndex + 1, inEnd, pIndex)
node.left = buildUtil(In, post, inStrt, iIndex - 1, pIndex)
return node
def buildTree(In, post, n):
pIndex = [n - 1]
return buildUtil(In, post, 0, n - 1, pIndex)
def search(arr, strt, end, value):
i = 0
for i in range(strt, end + 1):
if arr[i] == value:
break
return i | FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER NUMBER IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR RETURN VAR |
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals.
Example 1:
Input:
N = 8
in[] = 4 8 2 5 1 6 3 7
post[] =8 4 5 2 6 7 3 1
Output: 1 2 4 8 5 3 6 7
Explanation: For the given postorder and
inorder traversal of tree the resultant
binary tree will be
1
/ \
2 3
/ \ / \
4 5 6 7
\
8
Example 2:
Input:
N = 5
in[] = 9 5 2 3 4
post[] = 5 9 3 4 2
Output: 2 9 5 4 3
Explanation:
the resultant binary tree will be
2
/ \
9 4
\ /
5 3
Your Task:
You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree.
The generated output contains the preorder traversal of the constructed tree.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{3}
1 <= in[i], post[i] <= 10^{3} | def buildTree(In, post, n):
global index
def findPosition(element, In):
for i in range(len(In)):
if In[i] == element:
return i
def rec(In, post):
global index
if len(In) == 0:
return None
elem = post[index]
index -= 1
root = Node(elem)
pos = findPosition(elem, In)
root.right = rec(In[pos + 1 :], post)
root.left = rec(In[:pos], post)
return root
index = n - 1
return rec(In, post) | FUNC_DEF FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NONE ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR |
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals.
Example 1:
Input:
N = 8
in[] = 4 8 2 5 1 6 3 7
post[] =8 4 5 2 6 7 3 1
Output: 1 2 4 8 5 3 6 7
Explanation: For the given postorder and
inorder traversal of tree the resultant
binary tree will be
1
/ \
2 3
/ \ / \
4 5 6 7
\
8
Example 2:
Input:
N = 5
in[] = 9 5 2 3 4
post[] = 5 9 3 4 2
Output: 2 9 5 4 3
Explanation:
the resultant binary tree will be
2
/ \
9 4
\ /
5 3
Your Task:
You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree.
The generated output contains the preorder traversal of the constructed tree.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{3}
1 <= in[i], post[i] <= 10^{3} | def buildTree(inorder, postorder, n):
if inorder == [] or postorder == []:
return
root = Node(postorder[-1])
index = inorder.index(postorder[-1])
root.left = buildTree(inorder[0:index], postorder[0:index], n)
root.right = buildTree(
inorder[index + 1 : len(inorder)], postorder[index : len(inorder) - 1], n
)
return root | FUNC_DEF IF VAR LIST VAR LIST RETURN ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN VAR |
For the multiset of positive integers s=\\{s_1,s_2,...,s_k\}, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of s as follow:
* \gcd(s) is the maximum positive integer x, such that all integers in s are divisible on x.
* lcm(s) is the minimum positive integer x, that divisible on all integers from s.
For example, \gcd(\{8,12\})=4,\gcd(\{12,18,6\})=6 and lcm(\{4,6\})=12. Note that for any positive integer x, \gcd(\\{x\})=lcm(\\{x\})=x.
Orac has a sequence a with length n. He come up with the multiset t=\{lcm(\\{a_i,a_j\})\ |\ i<j\}, and asked you to find the value of \gcd(t) for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence.
Input
The first line contains one integer n\ (2≤ n≤ 100 000).
The second line contains n integers, a_1, a_2, …, a_n (1 ≤ a_i ≤ 200 000).
Output
Print one integer: \gcd(\{lcm(\\{a_i,a_j\})\ |\ i<j\}).
Examples
Input
2
1 1
Output
1
Input
4
10 24 40 80
Output
40
Input
10
540 648 810 648 720 540 594 864 972 648
Output
54
Note
For the first example, t=\{lcm(\{1,1\})\}=\{1\}, so \gcd(t)=1.
For the second example, t=\{120,40,80,120,240,80\}, and it's not hard to see that \gcd(t)=40. | def sieve(x):
is_prime = [True] * (x + 1)
is_prime[0] = is_prime[1] = False
ret = []
for i in range(2, x + 1):
if is_prime[i]:
ret.append(i)
for j in range(i * 2, x + 1, i):
is_prime[j] = False
return ret
def main():
n = int(input())
a = list(map(int, input().split()))
INF = 10**9
MAX = max(a)
primes = sieve(MAX)
mn1 = [INF] * (MAX + 1)
mn2 = [INF] * (MAX + 1)
for p in primes:
for i in range(n):
cnt = 0
while a[i] % p == 0:
a[i] //= p
cnt += 1
if cnt <= mn1[p]:
mn1[p], mn2[p] = cnt, mn1[p]
elif cnt < mn2[p]:
mn2[p] = cnt
if mn2[p] == 0:
break
ans = 1
for i, e in enumerate(mn2[2:], 2):
if e == INF:
continue
ans *= pow(i, e)
print(ans)
main() | FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
For the multiset of positive integers s=\\{s_1,s_2,...,s_k\}, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of s as follow:
* \gcd(s) is the maximum positive integer x, such that all integers in s are divisible on x.
* lcm(s) is the minimum positive integer x, that divisible on all integers from s.
For example, \gcd(\{8,12\})=4,\gcd(\{12,18,6\})=6 and lcm(\{4,6\})=12. Note that for any positive integer x, \gcd(\\{x\})=lcm(\\{x\})=x.
Orac has a sequence a with length n. He come up with the multiset t=\{lcm(\\{a_i,a_j\})\ |\ i<j\}, and asked you to find the value of \gcd(t) for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence.
Input
The first line contains one integer n\ (2≤ n≤ 100 000).
The second line contains n integers, a_1, a_2, …, a_n (1 ≤ a_i ≤ 200 000).
Output
Print one integer: \gcd(\{lcm(\\{a_i,a_j\})\ |\ i<j\}).
Examples
Input
2
1 1
Output
1
Input
4
10 24 40 80
Output
40
Input
10
540 648 810 648 720 540 594 864 972 648
Output
54
Note
For the first example, t=\{lcm(\{1,1\})\}=\{1\}, so \gcd(t)=1.
For the second example, t=\{120,40,80,120,240,80\}, and it's not hard to see that \gcd(t)=40. | import sys
input = sys.stdin.readline
def mult_input():
return map(int, input().split())
def list_input():
return list(map(int, input().split()))
prime = [[] for i in range(200001)]
for i in range(2, 200001):
if len(prime[i]) == 0:
for j in range(i, 200001, i):
prime[j].append(i)
n = int(input())
l = list_input()
s = []
location = {}
for i in range(n):
for j in prime[l[i]]:
if j not in location:
location[j] = {i: 0}
if True:
m = l[i]
while m % j == 0:
if i not in location[j]:
location[j][i] = 1
else:
location[j][i] += 1
m = m // j
s.append(j)
s = set(s)
ans = 1
for i in s:
if len(location[i]) < n - 1:
continue
else:
m = 10**9
for j in location[i]:
m = min(location[i][j], m)
if len(location[i]) == n - 1:
ans = ans * i**m
continue
count = 0
for j in location[i]:
if location[i][j] == m:
count += 1
if count >= 2:
ans = ans * i**m
continue
minn = 10**9
for j in location[i]:
if location[i][j] > m:
minn = min(minn, location[i][j])
ans = ans * i**minn
print(ans) | IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR DICT VAR NUMBER IF NUMBER ASSIGN VAR VAR VAR WHILE BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
For the multiset of positive integers s=\\{s_1,s_2,...,s_k\}, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of s as follow:
* \gcd(s) is the maximum positive integer x, such that all integers in s are divisible on x.
* lcm(s) is the minimum positive integer x, that divisible on all integers from s.
For example, \gcd(\{8,12\})=4,\gcd(\{12,18,6\})=6 and lcm(\{4,6\})=12. Note that for any positive integer x, \gcd(\\{x\})=lcm(\\{x\})=x.
Orac has a sequence a with length n. He come up with the multiset t=\{lcm(\\{a_i,a_j\})\ |\ i<j\}, and asked you to find the value of \gcd(t) for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence.
Input
The first line contains one integer n\ (2≤ n≤ 100 000).
The second line contains n integers, a_1, a_2, …, a_n (1 ≤ a_i ≤ 200 000).
Output
Print one integer: \gcd(\{lcm(\\{a_i,a_j\})\ |\ i<j\}).
Examples
Input
2
1 1
Output
1
Input
4
10 24 40 80
Output
40
Input
10
540 648 810 648 720 540 594 864 972 648
Output
54
Note
For the first example, t=\{lcm(\{1,1\})\}=\{1\}, so \gcd(t)=1.
For the second example, t=\{120,40,80,120,240,80\}, and it's not hard to see that \gcd(t)=40. | def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a % b)
def solve(arr, n):
ans = arr[0]
for i in range(1, n):
ans = gcd(ans, arr[i])
return ans
n = int(input())
arr = [int(i) for i in input().split()]
arr.insert(0, 0)
suffix = [int(0) for _ in range(n + 1)]
suffix[n] = arr[n]
for i in range(2, n + 1):
suffix[n + 1 - i] = gcd(arr[n + 1 - i], suffix[n + 2 - i])
d = [int(0) for _ in range(n + 1)]
for i in range(1, n):
d[i] = int(arr[i] * suffix[i + 1] / gcd(arr[i], suffix[i + 1]))
print(solve(d[1:], len(d) - 1)) | FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER |
For the multiset of positive integers s=\\{s_1,s_2,...,s_k\}, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of s as follow:
* \gcd(s) is the maximum positive integer x, such that all integers in s are divisible on x.
* lcm(s) is the minimum positive integer x, that divisible on all integers from s.
For example, \gcd(\{8,12\})=4,\gcd(\{12,18,6\})=6 and lcm(\{4,6\})=12. Note that for any positive integer x, \gcd(\\{x\})=lcm(\\{x\})=x.
Orac has a sequence a with length n. He come up with the multiset t=\{lcm(\\{a_i,a_j\})\ |\ i<j\}, and asked you to find the value of \gcd(t) for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence.
Input
The first line contains one integer n\ (2≤ n≤ 100 000).
The second line contains n integers, a_1, a_2, …, a_n (1 ≤ a_i ≤ 200 000).
Output
Print one integer: \gcd(\{lcm(\\{a_i,a_j\})\ |\ i<j\}).
Examples
Input
2
1 1
Output
1
Input
4
10 24 40 80
Output
40
Input
10
540 648 810 648 720 540 594 864 972 648
Output
54
Note
For the first example, t=\{lcm(\{1,1\})\}=\{1\}, so \gcd(t)=1.
For the second example, t=\{120,40,80,120,240,80\}, and it's not hard to see that \gcd(t)=40. | def gcd(num1, num2):
while num1:
num2, num1 = num1, num2 % num1
return num2
n = int(input())
arr = list(map(int, input().rstrip().split()))
gcdPreComp = [0] * (n - 1) + [arr[-1]]
for pos in range(len(arr) - 2, -1, -1):
gcdPreComp[pos] = gcd(arr[pos], gcdPreComp[pos + 1])
res = 0
for pos in range(n - 1):
temp = arr[pos] * gcdPreComp[pos + 1] // gcdPreComp[pos]
res = gcd(res, temp)
print(res) | FUNC_DEF WHILE VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER BIN_OP VAR NUMBER LIST VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
For the multiset of positive integers s=\\{s_1,s_2,...,s_k\}, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of s as follow:
* \gcd(s) is the maximum positive integer x, such that all integers in s are divisible on x.
* lcm(s) is the minimum positive integer x, that divisible on all integers from s.
For example, \gcd(\{8,12\})=4,\gcd(\{12,18,6\})=6 and lcm(\{4,6\})=12. Note that for any positive integer x, \gcd(\\{x\})=lcm(\\{x\})=x.
Orac has a sequence a with length n. He come up with the multiset t=\{lcm(\\{a_i,a_j\})\ |\ i<j\}, and asked you to find the value of \gcd(t) for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence.
Input
The first line contains one integer n\ (2≤ n≤ 100 000).
The second line contains n integers, a_1, a_2, …, a_n (1 ≤ a_i ≤ 200 000).
Output
Print one integer: \gcd(\{lcm(\\{a_i,a_j\})\ |\ i<j\}).
Examples
Input
2
1 1
Output
1
Input
4
10 24 40 80
Output
40
Input
10
540 648 810 648 720 540 594 864 972 648
Output
54
Note
For the first example, t=\{lcm(\{1,1\})\}=\{1\}, so \gcd(t)=1.
For the second example, t=\{120,40,80,120,240,80\}, and it's not hard to see that \gcd(t)=40. | n = int(input())
arr = input().split(" ")
lis = [int(s) for s in arr]
prime = [(True) for i in range(200001)]
p = 2
while p <= 200000:
if prime[p] == True:
for i in range(p * p, 200001, p):
prime[i] = False
p += 1
prime[1] = False
primes = []
ans = 1
for i in range(2, 200000):
if prime[i] == True:
primes.append(i)
for p in primes:
fmn = 100
smn = 100
for ele in lis:
if fmn == 0 and smn == 0:
break
cnt = 0
while ele % p == 0:
cnt += 1
ele = ele / p
if cnt <= fmn and cnt < smn:
smn = fmn
fmn = cnt
elif cnt < smn:
smn = cnt
ans = ans * pow(p, smn)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
For the multiset of positive integers s=\\{s_1,s_2,...,s_k\}, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of s as follow:
* \gcd(s) is the maximum positive integer x, such that all integers in s are divisible on x.
* lcm(s) is the minimum positive integer x, that divisible on all integers from s.
For example, \gcd(\{8,12\})=4,\gcd(\{12,18,6\})=6 and lcm(\{4,6\})=12. Note that for any positive integer x, \gcd(\\{x\})=lcm(\\{x\})=x.
Orac has a sequence a with length n. He come up with the multiset t=\{lcm(\\{a_i,a_j\})\ |\ i<j\}, and asked you to find the value of \gcd(t) for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence.
Input
The first line contains one integer n\ (2≤ n≤ 100 000).
The second line contains n integers, a_1, a_2, …, a_n (1 ≤ a_i ≤ 200 000).
Output
Print one integer: \gcd(\{lcm(\\{a_i,a_j\})\ |\ i<j\}).
Examples
Input
2
1 1
Output
1
Input
4
10 24 40 80
Output
40
Input
10
540 648 810 648 720 540 594 864 972 648
Output
54
Note
For the first example, t=\{lcm(\{1,1\})\}=\{1\}, so \gcd(t)=1.
For the second example, t=\{120,40,80,120,240,80\}, and it's not hard to see that \gcd(t)=40. | import sys
chk = [0] * 200001
prm = []
fact = {(1): [1]}
for i in range(2, 200001):
if chk[i] == 0:
prm.append(i)
j = 1
while True:
if i * j > 200000:
break
if i * j not in fact:
fact[i * j] = [i]
else:
fact[i * j].append(i)
chk[i * j] = 1
j += 1
n = int(sys.stdin.readline().rstrip())
a = list(map(int, sys.stdin.readline().rstrip().split()))
chk = []
for i in a:
for val in fact[i]:
if val not in chk:
chk.append(val)
result = 1
chk.sort()
for target in chk:
i = 1
while True:
if target == 1:
break
cnt = 0
for p in a:
if p % target**i != 0:
cnt += 1
if cnt > 1:
break
if cnt > 1:
break
i += 1
result *= target ** (i - 1)
print(result) | IMPORT ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR DICT NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE NUMBER IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR LIST VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
For the multiset of positive integers s=\\{s_1,s_2,...,s_k\}, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of s as follow:
* \gcd(s) is the maximum positive integer x, such that all integers in s are divisible on x.
* lcm(s) is the minimum positive integer x, that divisible on all integers from s.
For example, \gcd(\{8,12\})=4,\gcd(\{12,18,6\})=6 and lcm(\{4,6\})=12. Note that for any positive integer x, \gcd(\\{x\})=lcm(\\{x\})=x.
Orac has a sequence a with length n. He come up with the multiset t=\{lcm(\\{a_i,a_j\})\ |\ i<j\}, and asked you to find the value of \gcd(t) for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence.
Input
The first line contains one integer n\ (2≤ n≤ 100 000).
The second line contains n integers, a_1, a_2, …, a_n (1 ≤ a_i ≤ 200 000).
Output
Print one integer: \gcd(\{lcm(\\{a_i,a_j\})\ |\ i<j\}).
Examples
Input
2
1 1
Output
1
Input
4
10 24 40 80
Output
40
Input
10
540 648 810 648 720 540 594 864 972 648
Output
54
Note
For the first example, t=\{lcm(\{1,1\})\}=\{1\}, so \gcd(t)=1.
For the second example, t=\{120,40,80,120,240,80\}, and it's not hard to see that \gcd(t)=40. | n = int(input())
a = sorted(list(map(int, input().split(" "))))
d = {}
def pow_of(prime, number):
i = 0
divisor = 1
while number % divisor == 0:
i += 1
divisor *= prime
return i - 1
def p_upto(n):
primes = [2, 3, 5, 7]
i = 11
while i <= n:
bhul = True
for p in primes:
if i % p == 0:
bhul = False
break
if bhul:
primes.append(i)
i += 1
return primes
if len(a) == 1:
print(a[0])
else:
primes = p_upto(a[1])
for prime in primes:
min2 = []
min2.append(pow_of(prime, a[0]))
min2.append(pow_of(prime, a[1]))
min2.sort()
for i in range(2, len(a)):
kk = pow_of(prime, a[i])
if kk < min2[1]:
min2[1] = kk
min2.sort()
if min2[1] == 0:
break
d[prime] = min2[1]
ans = 1
for x in primes:
ans *= round(x ** d[x])
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR DICT FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR RETURN BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR |
For the multiset of positive integers s=\\{s_1,s_2,...,s_k\}, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of s as follow:
* \gcd(s) is the maximum positive integer x, such that all integers in s are divisible on x.
* lcm(s) is the minimum positive integer x, that divisible on all integers from s.
For example, \gcd(\{8,12\})=4,\gcd(\{12,18,6\})=6 and lcm(\{4,6\})=12. Note that for any positive integer x, \gcd(\\{x\})=lcm(\\{x\})=x.
Orac has a sequence a with length n. He come up with the multiset t=\{lcm(\\{a_i,a_j\})\ |\ i<j\}, and asked you to find the value of \gcd(t) for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence.
Input
The first line contains one integer n\ (2≤ n≤ 100 000).
The second line contains n integers, a_1, a_2, …, a_n (1 ≤ a_i ≤ 200 000).
Output
Print one integer: \gcd(\{lcm(\\{a_i,a_j\})\ |\ i<j\}).
Examples
Input
2
1 1
Output
1
Input
4
10 24 40 80
Output
40
Input
10
540 648 810 648 720 540 594 864 972 648
Output
54
Note
For the first example, t=\{lcm(\{1,1\})\}=\{1\}, so \gcd(t)=1.
For the second example, t=\{120,40,80,120,240,80\}, and it's not hard to see that \gcd(t)=40. | n = int(input())
As = list(map(int, input().split()))
def gcd(n, m):
a = max(n, m)
b = min(n, m)
while b:
a, b = b, a % b
return a
def lcm(n, m):
return n * m // gcd(n, m)
def make_divisors(n):
divisors = []
for i in range(1, int(n**0.5) + 1):
if n % i == 0:
divisors.append(i)
if i != n // i:
divisors.append(n // i)
return divisors
left = [As[0]]
right = [As[-1]]
for i in range(1, n):
left.append(gcd(left[i - 1], As[i]))
right.append(gcd(right[i - 1], As[-i - 1]))
right = right[::-1]
gcds = []
for i in range(n):
if i == 0:
gcds.append(right[1])
elif i == n - 1:
gcds.append(left[n - 2])
else:
l = gcd(left[i - 1], right[i + 1])
gcds.append(l)
ans = gcds[0]
for g in gcds:
ans = lcm(ans, g)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
For the multiset of positive integers s=\\{s_1,s_2,...,s_k\}, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of s as follow:
* \gcd(s) is the maximum positive integer x, such that all integers in s are divisible on x.
* lcm(s) is the minimum positive integer x, that divisible on all integers from s.
For example, \gcd(\{8,12\})=4,\gcd(\{12,18,6\})=6 and lcm(\{4,6\})=12. Note that for any positive integer x, \gcd(\\{x\})=lcm(\\{x\})=x.
Orac has a sequence a with length n. He come up with the multiset t=\{lcm(\\{a_i,a_j\})\ |\ i<j\}, and asked you to find the value of \gcd(t) for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence.
Input
The first line contains one integer n\ (2≤ n≤ 100 000).
The second line contains n integers, a_1, a_2, …, a_n (1 ≤ a_i ≤ 200 000).
Output
Print one integer: \gcd(\{lcm(\\{a_i,a_j\})\ |\ i<j\}).
Examples
Input
2
1 1
Output
1
Input
4
10 24 40 80
Output
40
Input
10
540 648 810 648 720 540 594 864 972 648
Output
54
Note
For the first example, t=\{lcm(\{1,1\})\}=\{1\}, so \gcd(t)=1.
For the second example, t=\{120,40,80,120,240,80\}, and it's not hard to see that \gcd(t)=40. | import sys
input = sys.stdin.readline
def prime(n):
if n == 1:
return []
if n == 2:
return [2]
p = 2
while p * p <= n:
if n % p == 0:
return [p] + prime(n // p)
p += 1
if p * p > n and n % p != 0:
return [n]
n = int(input())
a = list(map(int, input().split()))
exp = [[] for _ in range(200000)]
for x in a:
temp = prime(x)
while temp != []:
y = temp[0]
c = temp.count(y)
exp[y].append(c)
temp = temp[c:]
prod = 1
for i, x in enumerate(exp):
if len(x) <= n - 2:
continue
if len(x) == n - 1:
x.append(0)
x.sort()
prod *= i ** x[1]
print(prod) | IMPORT ASSIGN VAR VAR FUNC_DEF IF VAR NUMBER RETURN LIST IF VAR NUMBER RETURN LIST NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER RETURN BIN_OP LIST VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN LIST VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
For the multiset of positive integers s=\\{s_1,s_2,...,s_k\}, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of s as follow:
* \gcd(s) is the maximum positive integer x, such that all integers in s are divisible on x.
* lcm(s) is the minimum positive integer x, that divisible on all integers from s.
For example, \gcd(\{8,12\})=4,\gcd(\{12,18,6\})=6 and lcm(\{4,6\})=12. Note that for any positive integer x, \gcd(\\{x\})=lcm(\\{x\})=x.
Orac has a sequence a with length n. He come up with the multiset t=\{lcm(\\{a_i,a_j\})\ |\ i<j\}, and asked you to find the value of \gcd(t) for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence.
Input
The first line contains one integer n\ (2≤ n≤ 100 000).
The second line contains n integers, a_1, a_2, …, a_n (1 ≤ a_i ≤ 200 000).
Output
Print one integer: \gcd(\{lcm(\\{a_i,a_j\})\ |\ i<j\}).
Examples
Input
2
1 1
Output
1
Input
4
10 24 40 80
Output
40
Input
10
540 648 810 648 720 540 594 864 972 648
Output
54
Note
For the first example, t=\{lcm(\{1,1\})\}=\{1\}, so \gcd(t)=1.
For the second example, t=\{120,40,80,120,240,80\}, and it's not hard to see that \gcd(t)=40. | from sys import stdin, stdout
def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a % b)
def lcm(a, b):
return a * b / gcd(a, b)
a_Length = int(stdin.readline())
a = list(map(int, stdin.readline().split()))
pref = [0] * a_Length
suf = [0] * a_Length
pref[0] = a[0]
suf[a_Length - 1] = a[a_Length - 1]
for i in range(1, a_Length):
pref[i] = gcd(pref[i - 1], a[i])
for i in range(a_Length - 2, -1, -1):
suf[i] = gcd(suf[i + 1], a[i])
result = 1
for i in range(a_Length):
if i == 0:
result = lcm(result, suf[1])
elif i == a_Length - 1:
result = lcm(result, pref[a_Length - 2])
else:
result = lcm(result, gcd(pref[i - 1], suf[i + 1]))
stdout.write(str(int(result)) + "\n") | FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR STRING |
For the multiset of positive integers s=\\{s_1,s_2,...,s_k\}, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of s as follow:
* \gcd(s) is the maximum positive integer x, such that all integers in s are divisible on x.
* lcm(s) is the minimum positive integer x, that divisible on all integers from s.
For example, \gcd(\{8,12\})=4,\gcd(\{12,18,6\})=6 and lcm(\{4,6\})=12. Note that for any positive integer x, \gcd(\\{x\})=lcm(\\{x\})=x.
Orac has a sequence a with length n. He come up with the multiset t=\{lcm(\\{a_i,a_j\})\ |\ i<j\}, and asked you to find the value of \gcd(t) for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence.
Input
The first line contains one integer n\ (2≤ n≤ 100 000).
The second line contains n integers, a_1, a_2, …, a_n (1 ≤ a_i ≤ 200 000).
Output
Print one integer: \gcd(\{lcm(\\{a_i,a_j\})\ |\ i<j\}).
Examples
Input
2
1 1
Output
1
Input
4
10 24 40 80
Output
40
Input
10
540 648 810 648 720 540 594 864 972 648
Output
54
Note
For the first example, t=\{lcm(\{1,1\})\}=\{1\}, so \gcd(t)=1.
For the second example, t=\{120,40,80,120,240,80\}, and it's not hard to see that \gcd(t)=40. | n = int(input())
arr = list(map(int, input().split()))
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
arr.insert(0, 0)
pre = [0] * (n + 1)
suf = [0] * (n + 1)
pre[1] = arr[1]
suf[n] = arr[n]
for i in range(2, n + 1):
pre[i] = gcd(pre[i - 1], arr[i])
for i in range(n - 1, 0, -1):
suf[i] = gcd(suf[i + 1], arr[i])
ans = None
for i in range(1, n + 1):
if i == 1:
ans = suf[2]
elif i == n:
ans = ans * pre[n - 1] // gcd(pre[n - 1], ans)
else:
ans = ans * gcd(pre[i - 1], suf[i + 1]) // gcd(gcd(pre[i - 1], suf[i + 1]), ans)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
For the multiset of positive integers s=\\{s_1,s_2,...,s_k\}, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of s as follow:
* \gcd(s) is the maximum positive integer x, such that all integers in s are divisible on x.
* lcm(s) is the minimum positive integer x, that divisible on all integers from s.
For example, \gcd(\{8,12\})=4,\gcd(\{12,18,6\})=6 and lcm(\{4,6\})=12. Note that for any positive integer x, \gcd(\\{x\})=lcm(\\{x\})=x.
Orac has a sequence a with length n. He come up with the multiset t=\{lcm(\\{a_i,a_j\})\ |\ i<j\}, and asked you to find the value of \gcd(t) for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence.
Input
The first line contains one integer n\ (2≤ n≤ 100 000).
The second line contains n integers, a_1, a_2, …, a_n (1 ≤ a_i ≤ 200 000).
Output
Print one integer: \gcd(\{lcm(\\{a_i,a_j\})\ |\ i<j\}).
Examples
Input
2
1 1
Output
1
Input
4
10 24 40 80
Output
40
Input
10
540 648 810 648 720 540 594 864 972 648
Output
54
Note
For the first example, t=\{lcm(\{1,1\})\}=\{1\}, so \gcd(t)=1.
For the second example, t=\{120,40,80,120,240,80\}, and it's not hard to see that \gcd(t)=40. | import sys
def gcd(a, b):
while b:
a, b = b, a % b
return a
def lcm(a, b):
return a // gcd(a, b) * b
def main(n, a):
l = [0]
for ai in a:
l.append(gcd(l[-1], ai))
r = [0]
for ai in reversed(a):
r.append(gcd(r[-1], ai))
r.reverse()
ans = 1
for i in range(n):
ans = lcm(ans, gcd(l[i], r[i + 1]))
print(ans)
input = sys.stdin.readline
n = int(input())
(*a,) = map(int, input().split())
main(n, a) | IMPORT FUNC_DEF WHILE VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR |
For the multiset of positive integers s=\\{s_1,s_2,...,s_k\}, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of s as follow:
* \gcd(s) is the maximum positive integer x, such that all integers in s are divisible on x.
* lcm(s) is the minimum positive integer x, that divisible on all integers from s.
For example, \gcd(\{8,12\})=4,\gcd(\{12,18,6\})=6 and lcm(\{4,6\})=12. Note that for any positive integer x, \gcd(\\{x\})=lcm(\\{x\})=x.
Orac has a sequence a with length n. He come up with the multiset t=\{lcm(\\{a_i,a_j\})\ |\ i<j\}, and asked you to find the value of \gcd(t) for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence.
Input
The first line contains one integer n\ (2≤ n≤ 100 000).
The second line contains n integers, a_1, a_2, …, a_n (1 ≤ a_i ≤ 200 000).
Output
Print one integer: \gcd(\{lcm(\\{a_i,a_j\})\ |\ i<j\}).
Examples
Input
2
1 1
Output
1
Input
4
10 24 40 80
Output
40
Input
10
540 648 810 648 720 540 594 864 972 648
Output
54
Note
For the first example, t=\{lcm(\{1,1\})\}=\{1\}, so \gcd(t)=1.
For the second example, t=\{120,40,80,120,240,80\}, and it's not hard to see that \gcd(t)=40. | def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
n = int(input())
a = [int(x) for x in input().split()]
a.sort()
g = []
for i in range(n):
g.append(0)
g[n - 1] = a[n - 2] * a[n - 1]
g1 = a[n - 1]
for i in range(n - 2, 0, -1):
g1 = gcd(a[i], g1)
g[i] = a[i - 1] * g1
g1 = gcd(g1, a[0])
ans = g[1]
for i in range(1, n):
ans = gcd(g[i], ans)
print(ans // g1) | FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
For the multiset of positive integers s=\\{s_1,s_2,...,s_k\}, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of s as follow:
* \gcd(s) is the maximum positive integer x, such that all integers in s are divisible on x.
* lcm(s) is the minimum positive integer x, that divisible on all integers from s.
For example, \gcd(\{8,12\})=4,\gcd(\{12,18,6\})=6 and lcm(\{4,6\})=12. Note that for any positive integer x, \gcd(\\{x\})=lcm(\\{x\})=x.
Orac has a sequence a with length n. He come up with the multiset t=\{lcm(\\{a_i,a_j\})\ |\ i<j\}, and asked you to find the value of \gcd(t) for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence.
Input
The first line contains one integer n\ (2≤ n≤ 100 000).
The second line contains n integers, a_1, a_2, …, a_n (1 ≤ a_i ≤ 200 000).
Output
Print one integer: \gcd(\{lcm(\\{a_i,a_j\})\ |\ i<j\}).
Examples
Input
2
1 1
Output
1
Input
4
10 24 40 80
Output
40
Input
10
540 648 810 648 720 540 594 864 972 648
Output
54
Note
For the first example, t=\{lcm(\{1,1\})\}=\{1\}, so \gcd(t)=1.
For the second example, t=\{120,40,80,120,240,80\}, and it's not hard to see that \gcd(t)=40. | import sys
input = sys.stdin.readline
n = int(input())
ar = [int(x) for x in input().split()]
d = {}
count = {}
def power(x, p):
res = 1
while p:
if p & 1:
res = res * x
x = x * x
p >>= 1
return res
for i in ar:
ct = 0
temp = i
while i % 2 == 0:
ct += 1
i //= 2
if ct > 0:
if 2 not in count:
count[2] = 1
else:
count[2] += 1
if 2 not in d:
d[2] = [ct]
else:
d[2].append(ct)
for j in range(3, int(i**0.5) + 1, 2):
ct = 0
while i % j == 0:
ct += 1
i //= j
if ct > 0:
if j not in count:
count[j] = 1
else:
count[j] += 1
if j not in d:
d[j] = [ct]
else:
d[j].append(ct)
if i > 2:
if i not in count:
count[i] = 1
else:
count[i] += 1
if i not in d:
d[i] = [1]
else:
d[i].append(1)
ans = 1
for i in count.keys():
if count[i] == n:
d[i].sort()
ans *= power(i, d[i][1])
elif count[i] == n - 1:
d[i].sort()
ans *= power(i, d[i][0])
print(ans) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT FUNC_DEF ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER IF NUMBER VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER IF NUMBER VAR ASSIGN VAR NUMBER LIST VAR EXPR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR LIST NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
For the multiset of positive integers s=\\{s_1,s_2,...,s_k\}, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of s as follow:
* \gcd(s) is the maximum positive integer x, such that all integers in s are divisible on x.
* lcm(s) is the minimum positive integer x, that divisible on all integers from s.
For example, \gcd(\{8,12\})=4,\gcd(\{12,18,6\})=6 and lcm(\{4,6\})=12. Note that for any positive integer x, \gcd(\\{x\})=lcm(\\{x\})=x.
Orac has a sequence a with length n. He come up with the multiset t=\{lcm(\\{a_i,a_j\})\ |\ i<j\}, and asked you to find the value of \gcd(t) for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence.
Input
The first line contains one integer n\ (2≤ n≤ 100 000).
The second line contains n integers, a_1, a_2, …, a_n (1 ≤ a_i ≤ 200 000).
Output
Print one integer: \gcd(\{lcm(\\{a_i,a_j\})\ |\ i<j\}).
Examples
Input
2
1 1
Output
1
Input
4
10 24 40 80
Output
40
Input
10
540 648 810 648 720 540 594 864 972 648
Output
54
Note
For the first example, t=\{lcm(\{1,1\})\}=\{1\}, so \gcd(t)=1.
For the second example, t=\{120,40,80,120,240,80\}, and it's not hard to see that \gcd(t)=40. | import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
divs = {}
i = 0
for numb in a:
for d in divs:
foo = 0
if divs[d][1] > 0:
while numb % d == 0:
numb //= d
foo += 1
if foo < divs[d][0]:
divs[d][1] = divs[d][0]
divs[d][0] = foo
elif foo < divs[d][1]:
divs[d][1] = foo
if numb > 1 and i < 2:
j = 2
while numb > 1:
if numb % j == 0:
foo = 0
while numb % j == 0:
numb //= j
foo += 1
if i == 0:
divs[j] = [foo, 1000]
else:
divs[j] = [0, foo]
j += 1
i += 1
sol = 1
for d in divs:
sol *= d ** divs[d][1]
print(sol) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR LIST VAR NUMBER ASSIGN VAR VAR LIST NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
For the multiset of positive integers s=\\{s_1,s_2,...,s_k\}, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of s as follow:
* \gcd(s) is the maximum positive integer x, such that all integers in s are divisible on x.
* lcm(s) is the minimum positive integer x, that divisible on all integers from s.
For example, \gcd(\{8,12\})=4,\gcd(\{12,18,6\})=6 and lcm(\{4,6\})=12. Note that for any positive integer x, \gcd(\\{x\})=lcm(\\{x\})=x.
Orac has a sequence a with length n. He come up with the multiset t=\{lcm(\\{a_i,a_j\})\ |\ i<j\}, and asked you to find the value of \gcd(t) for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence.
Input
The first line contains one integer n\ (2≤ n≤ 100 000).
The second line contains n integers, a_1, a_2, …, a_n (1 ≤ a_i ≤ 200 000).
Output
Print one integer: \gcd(\{lcm(\\{a_i,a_j\})\ |\ i<j\}).
Examples
Input
2
1 1
Output
1
Input
4
10 24 40 80
Output
40
Input
10
540 648 810 648 720 540 594 864 972 648
Output
54
Note
For the first example, t=\{lcm(\{1,1\})\}=\{1\}, so \gcd(t)=1.
For the second example, t=\{120,40,80,120,240,80\}, and it's not hard to see that \gcd(t)=40. | def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
def lcm(a, b):
return a * b // gcd(a, b)
def solve(n, array):
pref = [(0) for i in range(n)]
suff = [(0) for i in range(n)]
g = [(0) for i in range(n)]
for i in range(1, n):
pref[i] = gcd(pref[i - 1], array[i - 1])
suff[n - 1 - i] = gcd(suff[n - i], array[n - i])
g[0] = suff[0]
g[-1] = pref[-1]
for i in range(1, n - 1):
g[i] = gcd(pref[i], suff[i])
ans = 1
for i in range(n):
ans = lcm(ans, g[i])
return ans
n = int(input())
array = [int(s) for s in input().split()]
print(solve(n, array)) | FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
For the multiset of positive integers s=\\{s_1,s_2,...,s_k\}, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of s as follow:
* \gcd(s) is the maximum positive integer x, such that all integers in s are divisible on x.
* lcm(s) is the minimum positive integer x, that divisible on all integers from s.
For example, \gcd(\{8,12\})=4,\gcd(\{12,18,6\})=6 and lcm(\{4,6\})=12. Note that for any positive integer x, \gcd(\\{x\})=lcm(\\{x\})=x.
Orac has a sequence a with length n. He come up with the multiset t=\{lcm(\\{a_i,a_j\})\ |\ i<j\}, and asked you to find the value of \gcd(t) for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence.
Input
The first line contains one integer n\ (2≤ n≤ 100 000).
The second line contains n integers, a_1, a_2, …, a_n (1 ≤ a_i ≤ 200 000).
Output
Print one integer: \gcd(\{lcm(\\{a_i,a_j\})\ |\ i<j\}).
Examples
Input
2
1 1
Output
1
Input
4
10 24 40 80
Output
40
Input
10
540 648 810 648 720 540 594 864 972 648
Output
54
Note
For the first example, t=\{lcm(\{1,1\})\}=\{1\}, so \gcd(t)=1.
For the second example, t=\{120,40,80,120,240,80\}, and it's not hard to see that \gcd(t)=40. | def primes235(limit):
yield 2
yield 3
yield 5
if limit < 7:
return
modPrms = [7, 11, 13, 17, 19, 23, 29, 31]
gaps = [4, 2, 4, 2, 4, 6, 2, 6, 4, 2, 4, 2, 4, 6, 2, 6]
ndxs = [
0,
0,
0,
0,
1,
1,
2,
2,
2,
2,
3,
3,
4,
4,
4,
4,
5,
5,
5,
5,
5,
5,
6,
6,
7,
7,
7,
7,
7,
7,
]
lmtbf = (limit + 23) // 30 * 8 - 1
lmtsqrt = int(limit**0.5) - 7
lmtsqrt = lmtsqrt // 30 * 8 + ndxs[lmtsqrt % 30]
buf = [True] * (lmtbf + 1)
for i in range(lmtsqrt + 1):
if buf[i]:
ci = i & 7
p = 30 * (i >> 3) + modPrms[ci]
s = p * p - 7
p8 = p << 3
for j in range(8):
c = s // 30 * 8 + ndxs[s % 30]
buf[c::p8] = [False] * ((lmtbf - c) // p8 + 1)
s += p * gaps[ci]
ci += 1
for i in range(lmtbf - 6 + ndxs[(limit - 7) % 30]):
if buf[i]:
yield 30 * (i >> 3) + modPrms[i & 7]
pp = list(primes235(5000))
def decomp(n):
d = {}
for p in pp:
if n % p == 0:
k = 0
while n % p == 0:
n //= p
k += 1
d[p] = k
if n == 1:
break
if n > 1:
d[n] = 1
return d
def cnt(n, p):
k = 0
while n % p == 0:
n //= p
k += 1
return k
n = int(input())
a = map(int, input().split())
x = decomp(next(a))
y = decomp(next(a))
for v in x:
y[v] = y.get(v, 0)
for v in y:
x[v] = x.get(v, 0)
x[v], y[v] = min(x[v], y[v]), max(x[v], y[v])
for u in a:
for v in x:
if y[v] > 0:
z = cnt(u, v)
if z < x[v]:
y[v] = x[v]
x[v] = z
elif z < y[v]:
y[v] = z
res = 1
for p, k in y.items():
if k > 0:
res *= p**k
print(res) | FUNC_DEF EXPR NUMBER EXPR NUMBER EXPR NUMBER IF VAR NUMBER RETURN ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP LIST NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR VAR FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
For the multiset of positive integers s=\\{s_1,s_2,...,s_k\}, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of s as follow:
* \gcd(s) is the maximum positive integer x, such that all integers in s are divisible on x.
* lcm(s) is the minimum positive integer x, that divisible on all integers from s.
For example, \gcd(\{8,12\})=4,\gcd(\{12,18,6\})=6 and lcm(\{4,6\})=12. Note that for any positive integer x, \gcd(\\{x\})=lcm(\\{x\})=x.
Orac has a sequence a with length n. He come up with the multiset t=\{lcm(\\{a_i,a_j\})\ |\ i<j\}, and asked you to find the value of \gcd(t) for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence.
Input
The first line contains one integer n\ (2≤ n≤ 100 000).
The second line contains n integers, a_1, a_2, …, a_n (1 ≤ a_i ≤ 200 000).
Output
Print one integer: \gcd(\{lcm(\\{a_i,a_j\})\ |\ i<j\}).
Examples
Input
2
1 1
Output
1
Input
4
10 24 40 80
Output
40
Input
10
540 648 810 648 720 540 594 864 972 648
Output
54
Note
For the first example, t=\{lcm(\{1,1\})\}=\{1\}, so \gcd(t)=1.
For the second example, t=\{120,40,80,120,240,80\}, and it's not hard to see that \gcd(t)=40. | import sys
I = sys.stdin.readline
n = int(I())
a = list(map(int, I().split()))
def gcd(a, b):
while b != 0:
temp = b
b = a % b
a = temp
return a
suff_gcd = [0] * n
suff_gcd[-1] = a[-1]
gcd_list = []
for i in range(n - 2, -1, -1):
suff_gcd[i] = gcd(a[i], suff_gcd[i + 1])
for i in range(n - 1):
gcd_list.append(a[i] * suff_gcd[i + 1] // gcd(a[i], suff_gcd[i + 1]))
ans = gcd_list[0]
for i in gcd_list[1:]:
ans = gcd(ans, i)
print(ans) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF WHILE VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
For the multiset of positive integers s=\\{s_1,s_2,...,s_k\}, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of s as follow:
* \gcd(s) is the maximum positive integer x, such that all integers in s are divisible on x.
* lcm(s) is the minimum positive integer x, that divisible on all integers from s.
For example, \gcd(\{8,12\})=4,\gcd(\{12,18,6\})=6 and lcm(\{4,6\})=12. Note that for any positive integer x, \gcd(\\{x\})=lcm(\\{x\})=x.
Orac has a sequence a with length n. He come up with the multiset t=\{lcm(\\{a_i,a_j\})\ |\ i<j\}, and asked you to find the value of \gcd(t) for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence.
Input
The first line contains one integer n\ (2≤ n≤ 100 000).
The second line contains n integers, a_1, a_2, …, a_n (1 ≤ a_i ≤ 200 000).
Output
Print one integer: \gcd(\{lcm(\\{a_i,a_j\})\ |\ i<j\}).
Examples
Input
2
1 1
Output
1
Input
4
10 24 40 80
Output
40
Input
10
540 648 810 648 720 540 594 864 972 648
Output
54
Note
For the first example, t=\{lcm(\{1,1\})\}=\{1\}, so \gcd(t)=1.
For the second example, t=\{120,40,80,120,240,80\}, and it's not hard to see that \gcd(t)=40. | def gcd(a, b):
if a == 0:
return b
return gcd(b % a, a)
def lcm(a, b):
return a * b // gcd(a, b)
n = int(input())
a = list(map(int, input().split()))
gcda = [a[-1]]
m = a[::-1]
for i in range(1, n):
gcda.append(gcd(gcda[-1], m[i]))
gcda = gcda[::-1]
answer = lcm(a[0], gcda[1])
for i in range(1, len(a)):
if i == len(a) - 1:
continue
else:
answer = gcd(answer, lcm(a[i], gcda[i + 1]))
if answer == 1:
break
print(answer) | FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR |
For the multiset of positive integers s=\\{s_1,s_2,...,s_k\}, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of s as follow:
* \gcd(s) is the maximum positive integer x, such that all integers in s are divisible on x.
* lcm(s) is the minimum positive integer x, that divisible on all integers from s.
For example, \gcd(\{8,12\})=4,\gcd(\{12,18,6\})=6 and lcm(\{4,6\})=12. Note that for any positive integer x, \gcd(\\{x\})=lcm(\\{x\})=x.
Orac has a sequence a with length n. He come up with the multiset t=\{lcm(\\{a_i,a_j\})\ |\ i<j\}, and asked you to find the value of \gcd(t) for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence.
Input
The first line contains one integer n\ (2≤ n≤ 100 000).
The second line contains n integers, a_1, a_2, …, a_n (1 ≤ a_i ≤ 200 000).
Output
Print one integer: \gcd(\{lcm(\\{a_i,a_j\})\ |\ i<j\}).
Examples
Input
2
1 1
Output
1
Input
4
10 24 40 80
Output
40
Input
10
540 648 810 648 720 540 594 864 972 648
Output
54
Note
For the first example, t=\{lcm(\{1,1\})\}=\{1\}, so \gcd(t)=1.
For the second example, t=\{120,40,80,120,240,80\}, and it's not hard to see that \gcd(t)=40. | def prime_factorizatoon(n):
ans = {}
ta = 0
while n % 2 == 0:
n //= 2
ta += 1
if ta > 0:
ans[2] = ta
i = 3
while i * i <= n:
ta = 0
while n % i == 0:
n //= i
ta += 1
if ta > 0:
ans[i] = ta
i += 2
if n > 1:
ans[n] = 1
return ans
n = int(input())
va = list(map(int, input().split()))
factors = []
for i in va:
factors.append(prime_factorizatoon(i))
occ = {}
for i in range(n):
for j in factors[i].keys():
if j in occ:
occ[j].append(factors[i][j])
else:
occ[j] = [factors[i][j]]
ans = 1
for i in occ.keys():
if len(occ[i]) >= n - 1:
occ[i].sort()
if len(occ[i]) == n:
ans *= pow(i, occ[i][1])
else:
ans *= pow(i, occ[i][0])
print(ans) | FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
For the multiset of positive integers s=\\{s_1,s_2,...,s_k\}, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of s as follow:
* \gcd(s) is the maximum positive integer x, such that all integers in s are divisible on x.
* lcm(s) is the minimum positive integer x, that divisible on all integers from s.
For example, \gcd(\{8,12\})=4,\gcd(\{12,18,6\})=6 and lcm(\{4,6\})=12. Note that for any positive integer x, \gcd(\\{x\})=lcm(\\{x\})=x.
Orac has a sequence a with length n. He come up with the multiset t=\{lcm(\\{a_i,a_j\})\ |\ i<j\}, and asked you to find the value of \gcd(t) for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence.
Input
The first line contains one integer n\ (2≤ n≤ 100 000).
The second line contains n integers, a_1, a_2, …, a_n (1 ≤ a_i ≤ 200 000).
Output
Print one integer: \gcd(\{lcm(\\{a_i,a_j\})\ |\ i<j\}).
Examples
Input
2
1 1
Output
1
Input
4
10 24 40 80
Output
40
Input
10
540 648 810 648 720 540 594 864 972 648
Output
54
Note
For the first example, t=\{lcm(\{1,1\})\}=\{1\}, so \gcd(t)=1.
For the second example, t=\{120,40,80,120,240,80\}, and it's not hard to see that \gcd(t)=40. | n = int(input())
a = list(map(int, input().split()))
ans = 1
m = [0] * 200000
for i in range(2, 200000):
counter = 0
fstpos = -1
breakflag = 0
r = 0
while r < 2:
breakflag = 0
for j in range(n):
if a[j] % i == 0:
b = a[j] // i
if (b + 1) * i <= a[j]:
b += 1
a[j] = b
elif j != fstpos:
r += 1
if r == 2:
breakflag = 1
fstpos = j
if breakflag == 1:
break
if breakflag == 1:
break
ans = ans * i
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
For the multiset of positive integers s=\\{s_1,s_2,...,s_k\}, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of s as follow:
* \gcd(s) is the maximum positive integer x, such that all integers in s are divisible on x.
* lcm(s) is the minimum positive integer x, that divisible on all integers from s.
For example, \gcd(\{8,12\})=4,\gcd(\{12,18,6\})=6 and lcm(\{4,6\})=12. Note that for any positive integer x, \gcd(\\{x\})=lcm(\\{x\})=x.
Orac has a sequence a with length n. He come up with the multiset t=\{lcm(\\{a_i,a_j\})\ |\ i<j\}, and asked you to find the value of \gcd(t) for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence.
Input
The first line contains one integer n\ (2≤ n≤ 100 000).
The second line contains n integers, a_1, a_2, …, a_n (1 ≤ a_i ≤ 200 000).
Output
Print one integer: \gcd(\{lcm(\\{a_i,a_j\})\ |\ i<j\}).
Examples
Input
2
1 1
Output
1
Input
4
10 24 40 80
Output
40
Input
10
540 648 810 648 720 540 594 864 972 648
Output
54
Note
For the first example, t=\{lcm(\{1,1\})\}=\{1\}, so \gcd(t)=1.
For the second example, t=\{120,40,80,120,240,80\}, and it's not hard to see that \gcd(t)=40. | n = int(input())
A = list(map(int, input().split(" ")))
gcd = {}
gcd[1] = A[-1]
def GCD(a, b):
if a == 0:
return b
return GCD(b % a, a)
temp = A[-1]
for i in range(n - 2, -1, -1):
temp = GCD(temp, A[i])
gcd[n - i] = temp
arr = []
for i in range(n - 1, 0, -1):
arr.append(gcd[i] * A[n - i - 1])
temp = arr[0]
for i in range(1, len(arr) - 1):
temp = GCD(temp, arr[i])
print(temp // gcd[len(A)]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR DICT ASSIGN VAR NUMBER VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR |
For the multiset of positive integers s=\\{s_1,s_2,...,s_k\}, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of s as follow:
* \gcd(s) is the maximum positive integer x, such that all integers in s are divisible on x.
* lcm(s) is the minimum positive integer x, that divisible on all integers from s.
For example, \gcd(\{8,12\})=4,\gcd(\{12,18,6\})=6 and lcm(\{4,6\})=12. Note that for any positive integer x, \gcd(\\{x\})=lcm(\\{x\})=x.
Orac has a sequence a with length n. He come up with the multiset t=\{lcm(\\{a_i,a_j\})\ |\ i<j\}, and asked you to find the value of \gcd(t) for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence.
Input
The first line contains one integer n\ (2≤ n≤ 100 000).
The second line contains n integers, a_1, a_2, …, a_n (1 ≤ a_i ≤ 200 000).
Output
Print one integer: \gcd(\{lcm(\\{a_i,a_j\})\ |\ i<j\}).
Examples
Input
2
1 1
Output
1
Input
4
10 24 40 80
Output
40
Input
10
540 648 810 648 720 540 594 864 972 648
Output
54
Note
For the first example, t=\{lcm(\{1,1\})\}=\{1\}, so \gcd(t)=1.
For the second example, t=\{120,40,80,120,240,80\}, and it's not hard to see that \gcd(t)=40. | maxn = 200005
fac = [[] for i in range(maxn)]
cnt = [[] for i in range(maxn)]
n = int(input())
arr = list(map(int, input().split()))
if n == 1:
print(arr[0])
else:
for i in range(n):
j = 2
while j * j <= arr[i]:
count = 0
if arr[i] % j == 0:
while arr[i] % j == 0:
count += 1
arr[i] = arr[i] // j
fac[j].append(1)
cnt[j].append(count)
j += 1
if arr[i] > 1:
fac[arr[i]].append(arr[i])
cnt[arr[i]].append(1)
ans = 1
for i in range(2, maxn):
if len(fac[i]) >= n - 1:
temp = cnt[i][:]
temp.sort()
if len(fac[i]) == n:
ans = ans * pow(i, temp[1])
else:
ans = ans * pow(i, temp[0])
print(ans) | ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER WHILE BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
a = S[:]
s = 0
for i, j, k in sorted(zip(index, sources, targets)):
if S[i : i + len(j)] == j:
i = i + s
a = a[:i] + k + a[i + len(j) :]
s += len(k) - len(j)
return a | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
n = len(S)
m = 0
for i in range(Q - 1, -1, -1):
idx = index[i]
s = sources[i]
t = targets[i]
if S[idx : idx + len(s)] == s:
S = S[:idx] + t + S[idx + len(s) :]
m = len(t) - 1
return S | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
for index, sources, targets in sorted(
zip(index, sources, targets), reverse=True
):
if S[index : index + len(sources)] == sources:
S = S[:index] + targets + S[index + len(sources) :]
else:
S = S
return S | CLASS_DEF FUNC_DEF FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
S = list(S)
to_replace_target = []
to_replace_source = []
for q_no in range(Q):
i = index[q_no]
n = len(sources[q_no])
if S[i : i + n] == list(sources[q_no]):
S[i : i + n] = ["$"] * n
to_replace_target.append(targets[q_no])
to_replace_source.append(sources[q_no])
S = "".join(S)
for i in range(len(to_replace_target)):
S = S.replace("$" * len(to_replace_source[i]), to_replace_target[i], 1)
return S | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP LIST STRING VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
index.reverse()
sources.reverse()
targets.reverse()
for i, s, t in zip(index, sources, targets):
if S[i : i + len(s)] == s:
S = S[0:i] + t + S[i + len(s) :]
return S | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR RETURN VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
answer = ""
input_idx = 0
read_idx = 0
while read_idx < len(S):
if input_idx == Q:
answer += S[read_idx:]
return answer
if read_idx == index[input_idx]:
src, trg = sources[input_idx], targets[input_idx]
st, ed = read_idx, read_idx + len(src)
if S[st:ed] == src:
answer += trg
read_idx = ed
else:
answer += S[read_idx]
read_idx += 1
input_idx += 1
else:
answer += S[read_idx]
read_idx += 1
return answer | CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR RETURN VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
leng = 0
for i in range(len(index)):
index[i] += leng
if S[index[i] : index[i] + len(sources[i])] != sources[i]:
continue
str = S[: index[i]] + targets[i] + S[index[i] + len(sources[i]) :]
S = str
leng += len(targets[i]) - len(sources[i])
return S | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
idx = {index[i]: i for i in range(len(index))}
in_window, i, j, end, valid = False, 0, 0, -1, False
ans, sr_idx, s = [], -1, ""
while i < len(S):
if i in idx:
in_window = True
end = i + len(sources[idx[i]])
sr_idx, j, valid = idx[i], 0, True
s = ""
if not in_window:
ans.append(S[i])
else:
s += S[i]
if i == end - 1:
if valid and S[i] == sources[sr_idx][j]:
ans.append(targets[sr_idx])
else:
ans.append(s)
in_window = False
s = ""
elif S[i] == sources[sr_idx][j]:
j += 1
else:
valid = False
i += 1
return "".join(ans) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR LIST NUMBER STRING WHILE VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR STRING IF VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER RETURN FUNC_CALL STRING VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, s, Q, index, sources, targets):
list_new = []
ctr = 0
range_list = [x for x in range(0, len(s))]
range_list_iter = iter(range_list)
steps = 0
i = 0
for r in range_list_iter:
if r in index:
try:
indexing = s.index(sources[ctr], r)
except:
indexing = -1
list_new.append(s[r])
ctr += 1
continue
if r == indexing:
list_new.append(targets[ctr])
else:
list_new.append(s[r])
ctr += 1
continue
leng = len(sources[ctr]) - 1
if leng > 0:
while i < leng:
try:
next(range_list_iter)
except StopIteration:
break
i += 1
i = 0
ctr += 1
if ctr > len(index) - 1 and r >= len(s):
break
else:
list_new.append(s[r])
return "".join(list_new) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL STRING VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
diff = 0
for j in range(len(index)):
x = sources[j]
y = targets[j]
i = index[j]
if x == S[i + diff : i + diff + len(x)]:
S = S[: i + diff] + y + S[i + diff + len(x) :]
diff += len(y) - len(x)
return S | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
replacements = [(index[i], sources[i], targets[i]) for i in range(Q)]
replacements.sort(reverse=True)
for i, source, target in replacements:
if S[i : i + len(source)] == source:
S = S[:i] + target + S[i + len(source) :]
return S | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR RETURN VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
res = []
i = 0
while i < len(S):
if i in index:
temp = index.index(i)
if S[i : i + len(sources[temp])] == sources[temp]:
res.append(targets[temp])
else:
res.append(S[i : i + len(sources[temp])])
i += len(sources[temp])
continue
else:
res.append(S[i])
i += 1
return "".join(res) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN FUNC_CALL STRING VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
n = len(S)
diff = 0
for i in range(Q):
curr_ind = index[i]
curr_s = sources[i]
curr_t = targets[i]
m = len(curr_s)
p = len(curr_t)
if curr_ind + diff + m <= n:
if S[curr_ind + diff : curr_ind + diff + m] == curr_s:
new = S[: curr_ind + diff] + curr_t + S[curr_ind + diff + m :]
diff += p - m
n += p - m
S = new
return S
if __name__ == "__main__":
t = int(input())
for _ in range(t):
S = input()
Q = int(input())
index = list(map(int, input().split()))
sources = list(map(str, input().split()))
targets = list(map(str, input().split()))
ob = Solution()
print(ob.findAndReplace(S, Q, index, sources, targets)) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
arr = [i for i in S]
for i in range(Q):
if sources[i] == "".join(arr[index[i] : index[i] + len(sources[i])]):
arr[index[i]] = targets[i]
for x in range(index[i] + 1, index[i] + len(sources[i])):
arr[x] = ""
return "".join(arr) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL STRING VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR STRING RETURN FUNC_CALL STRING VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def find(self, S, source, index):
l = len(source)
for i in range(l):
if S[index + i] != source[i]:
return -1
return 1
def replaceStr(self, S, index, source, target):
s1 = S[index:]
S = S[:index]
s1 = s1.replace(source, target, 1)
S += s1
return S
def findAndReplace(self, S, Q, index, sources, targets):
for i in range(Q - 1, -1, -1):
if self.find(S, sources[i], index[i]) != -1:
S = self.replaceStr(S, index[i], sources[i], targets[i])
return S | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
listChanges = []
for i in range(Q):
l = len(sources[i])
start = index[i]
end = start + l
if S[start:end] == sources[i]:
listChanges.append(i)
else:
pass
extra = 0
for j in range(len(listChanges)):
i = listChanges[j]
l = len(sources[i])
t = len(targets[i])
start = extra + index[i]
end = start + l
S = S[0:start] + targets[i] + S[end:]
extra += t - l
return S | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
n = len(S)
res = ""
i = 0
while i < n:
j = 0
while j < Q:
if i == index[j]:
if S[i : i + len(sources[j])] == sources[j]:
res += targets[j]
i += len(sources[j]) - 1
break
j += 1
if j == Q:
res += S[i]
i += 1
return res | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR IF VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
x = ""
i = 0
while i < len(S):
if len(index) and i == index[0]:
j = i
k = 0
while j < len(S) and k < len(sources[0]) and sources[0][k] == S[j]:
j += 1
k += 1
if k == len(sources[0]):
x = x + targets[0]
i = j
else:
x = x + S[i]
i += 1
sources.pop(0)
targets.pop(0)
index.pop(0)
else:
x = x + S[i]
i += 1
return x | CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
res = list(S)
for i in range(Q):
start = index[i]
end = index[i] + len(sources[i])
if S[start:end] == sources[i]:
res[index[i]] = targets[i]
for i in range(start + 1, end):
res[i] = ""
return "".join(res) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR STRING RETURN FUNC_CALL STRING VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
sNew = ""
i = 0
currIndex = 0
while i < Q:
if i == 0 and index[i] != 0:
sNew = sNew + S[0 : index[i]]
currIndex = index[i]
indexSub = len(sources[i])
if S[currIndex : currIndex + indexSub] == sources[i]:
sNew = sNew + targets[i]
else:
sNew = sNew + S[currIndex : currIndex + indexSub]
i += 1
if i == Q:
sNew = sNew + S[index[i - 1] + indexSub :]
return sNew
sNew = sNew + S[index[i - 1] + indexSub : index[i]] | CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
ans = ""
pos = 0
for i in range(Q):
idx = index[i]
if S[idx : idx + len(sources[i])] == sources[i]:
ans += S[pos:idx]
ans += targets[i]
pos = idx + len(sources[i])
ans += S[pos : len(S)]
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
valid_queries = list(
filter(
lambda x: S[x[0] : x[0] + len(x[1])] == x[1],
zip(index, sources, targets),
)
)
valid_queries.sort()
diff = 0
res = S
for i, s, t in valid_queries:
res = res[: i + diff] + t + res[i + diff + len(s) :]
diff += len(t) - len(s)
return res | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
sortd = []
for i in range(Q):
sortd.append([index[i], i])
sortd.sort(reverse=True)
for ind in sortd:
i = ind[0]
s = sources[ind[1]]
t = targets[ind[1]]
if S[i : i + len(s)] == s:
S = S[0:i] + t + S[i + len(s) :]
return S | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR RETURN VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
test = S
z = 0
for i in range(0, len(index)):
x = sources[i]
y = len(x)
if S[index[i] : index[i] + y] == x:
test = test[: index[i] + z] + targets[i] + test[index[i] + y + z :]
z = z + len(targets[i]) - y
S = test
return S | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
string = []
st, j = "", 0
rep = -1
i = 0
while i < len(S):
if j < Q:
if i == index[j]:
if S[i : i + len(sources[j])] == sources[j]:
if j == Q - 1:
rep = 1
string.append(st)
string.append(targets[j])
i += len(sources[j])
j += 1
st = ""
else:
j += 1
st += S[i]
i += 1
else:
st += S[i]
i += 1
else:
st += S[i]
i += 1
string.append(st)
return "".join(string) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR STRING NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR IF VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR STRING VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL STRING VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
rlen = 0
for x in range(Q):
substr = S[index[x] + rlen : index[x] + len(sources[x]) + rlen]
if substr == sources[x]:
S = (
S[0 : index[x] + rlen]
+ targets[x]
+ S[index[x] + len(sources[x]) + rlen :]
)
rlen += len(targets[x]) - len(substr)
return S | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
replace_index = []
for i in range(Q):
if S.find(sources[i], index[i], index[i] + len(sources[i])) == index[i]:
replace_index.append(i)
if not replace_index:
return S
offset = 0
for i in replace_index:
cur_index = index[i] + offset
S = S[:cur_index] + S[cur_index + len(sources[i]) :]
S = S[:cur_index] + targets[i] + S[cur_index:]
offset += len(targets[i]) - len(sources[i])
return S | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
new = S
prevt = 0
prevs = 0
l2 = 0
for i in range(Q):
l = int(len(sources[i]))
upto = index[i] + l
l2 += l
if S[index[i] : upto] == sources[i]:
new = new[: index[i] + prevt - prevs] + targets[i] + S[index[i] + l :]
prevt += len(targets[i])
prevs += len(sources[i])
return new | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
index.reverse()
sources.reverse()
targets.reverse()
for ind, so, tar in zip(index, sources, targets):
if S[ind : ind + len(so)] == so:
S = S[0:ind] + tar + S[ind + len(so) :]
return S
if __name__ == "__main__":
t = int(input())
for _ in range(t):
S = input()
Q = int(input())
index = list(map(int, input().split()))
sources = list(map(str, input().split()))
targets = list(map(str, input().split()))
ob = Solution()
print(ob.findAndReplace(S, Q, index, sources, targets)) | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
res = []
beg = 0
for i in range(Q):
ind = index[i]
if S[ind : ind + len(sources[i])] == sources[i]:
res.append(S[beg:ind])
res.append(targets[i])
beg = ind + len(sources[i])
res.append(S[beg : len(S)])
return "".join(res) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL STRING VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
org = S
for i in range(Q - 1, -1, -1):
l = 0
m = 0
source = sources[i]
s_ind = index[i]
while l < len(source):
if S[s_ind] == source[l]:
l += 1
s_ind += 1
else:
break
if l == len(source):
temp = S[: index[i]] + targets[i] + S[s_ind:]
S = temp
return S
if __name__ == "__main__":
t = int(input())
for _ in range(t):
S = input()
Q = int(input())
index = list(map(int, input().split()))
sources = list(map(str, input().split()))
targets = list(map(str, input().split()))
ob = Solution()
print(ob.findAndReplace(S, Q, index, sources, targets)) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
for i in range(Q):
n = len(sources[-i - 1])
j = index[-i - 1]
if S[j : j + n] == sources[-i - 1]:
S = S[:j] + targets[-i - 1] + S[j + n :]
return S | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
st = 0
res = []
for i, src, tgt in zip(index, sources, targets):
if S[i:].startswith(src):
res.extend(S[st:i])
res.extend(tgt)
st = i + len(src)
if st < len(S):
res.extend(S[st : len(S)])
return "".join(res) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL STRING VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
lookup = {i: (src, tgt) for i, src, tgt in zip(index, sources, targets)}
res = ""
i = 0
while i < len(S):
if i in lookup:
ptn, tar = lookup[i]
if S[i:].startswith(ptn):
res += tar
i += len(ptn)
else:
res += S[i]
i += 1
else:
res += S[i]
i += 1
return res
if __name__ == "__main__":
t = int(input())
for _ in range(t):
S = input()
Q = int(input())
index = list(map(int, input().split()))
sources = list(map(str, input().split()))
targets = list(map(str, input().split()))
ob = Solution()
print(ob.findAndReplace(S, Q, index, sources, targets)) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
s = S[:]
toBeAdded = 0
for i in range(Q):
sourceLen = len(sources[i])
if S[index[i] : index[i] + sourceLen] == sources[i]:
s = s[: index[i] + toBeAdded] + targets[i] + S[index[i] + sourceLen :]
toBeAdded += len(targets[i]) - sourceLen
return s | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
idx = []
for i in range(Q):
if S.find(sources[i], index[i]) == index[i]:
idx.append(i)
for i in idx:
S = S[: index[i]] + targets[i] + S[index[i] + len(sources[i]) :]
for j in range(i + 1, Q):
index[j] += len(targets[i]) - len(sources[i])
return S | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
indexEnum = []
for i, val in enumerate(index):
indexEnum.append((i, val))
indexEnum.sort(reverse=True)
for i, index in indexEnum:
s = sources[i]
t = targets[i]
if s == S[index : index + len(s)]:
S = "".join([S[:index], t, S[index + len(s) :]])
return S | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING LIST VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR RETURN VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
ans = ""
j = 0
for i, s, t in zip(index, sources, targets):
while j < i:
ans += S[j]
j += 1
if S[j : j + len(s)] == s:
ans += t
j += len(s)
while j < len(S):
ans += S[j]
j += 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
arr = []
for k in range(Q):
ind = index[k]
pat = sources[k]
flag = True
i = ind
for j in range(len(pat)):
if i >= len(S) or S[i] != pat[j]:
flag = False
break
i += 1
if flag == True:
arr.append(k)
temp = 0
for i in range(len(arr)):
ind = index[arr[i]] + temp
temp += len(targets[arr[i]]) - len(sources[arr[i]])
prefix = S[:ind]
suffix = S[ind + len(sources[arr[i]]) :]
S = prefix + targets[arr[i]] + suffix
return S | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR RETURN VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, s, q, index, sources, targets):
arr = [(index[i], sources[i], targets[i]) for i in range(q)]
arr.sort()
d = []
for i in range(q):
m = len(arr[i][1])
if s[index[i] : arr[i][0] + m] == arr[i][1]:
d.append(i)
c = 0
ans = ""
i = 0
j = 0
n = len(d)
while i < len(s) and j < n:
m = len(arr[d[j]][1])
ans += s[i : arr[d[j]][0]]
i = arr[d[j]][0] + m
ans += arr[d[j]][2]
j += 1
ans += s[i:]
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR RETURN VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, s, Q, index, sources, targets):
for i in range(Q - 1, -1, -1):
if s[index[i] : len(sources[i]) + index[i]] == sources[i]:
s = s[: index[i]] + targets[i] + s[index[i] + len(sources[i]) :]
return s | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
modified = list(S)
for idx, source, target in zip(index, sources, targets):
if not S[idx:].startswith(source):
continue
else:
modified[idx] = target
for i in range(idx + 1, len(source) + idx):
modified[i] = ""
return "".join(modified) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR VAR STRING RETURN FUNC_CALL STRING VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
c = 0
m = {}
for i in range(Q):
d = S.find(sources[i], index[i])
if d == index[i]:
m[d] = [len(sources[i]), targets[i]]
t = ""
i = 0
n = len(S)
while i < n:
if i in m:
t += m[i][1]
i += m[i][0]
else:
t += S[i]
i += 1
return t | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR LIST FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
l = len(S)
ans = ""
i = 0
while i < l:
if i in index:
a = index.index(i)
s = sources[a]
t = targets[a]
if i + len(s) <= l:
b = S[i : i + len(s)]
if b == s:
ans += t
i += len(s)
else:
ans += S[i]
i += 1
else:
ans += S[i]
i += 1
else:
ans += S[i]
i += 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
prev = 0
cnt = 0
for i in range(Q):
if S[index[i] : index[i] + len(sources[i])] == sources[i]:
St = list(S)
tar = list(targets[i])
St[index[i] : index[i] + len(sources[i])] = tar
S = "".join(map(str, St))
if i != Q - 1:
prev += len(targets[i])
cnt += len(sources[i])
index[i + 1] = prev + index[i + 1] - cnt
elif i != Q - 1:
index[i + 1] = prev + index[i + 1] - cnt
return S | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
word = ""
i = j = 0
while i < len(S):
if j < len(index) and index[j] == i:
till = index[j] + len(sources[j])
str1 = S[index[j] : till]
if sources[j] in str1:
word += targets[j]
i += len(sources[j]) - 1
else:
word += S[i]
j += 1
else:
word += S[i]
i += 1
return word | CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
d = {}
for i in range(Q):
if S[index[i] : len(sources[i]) + index[i]] == sources[i]:
d[index[i]] = [sources[i], targets[i]]
ans = ""
n = len(S)
i = 0
while i < n:
if i in d:
ans = ans + d[i][1]
i = i + len(d[i][0])
else:
ans = ans + S[i]
i += 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
res = []
i = 0
while i < len(S):
if i in index:
temp = index.index(i)
if S[i : i + len(sources[temp])] == sources[temp]:
res.append(targets[temp])
else:
res.append(S[i : i + len(sources[temp])])
i += len(sources[temp])
continue
else:
res.append(S[i])
i += 1
return "".join(res)
if __name__ == "__main__":
t = int(input())
for _ in range(t):
S = input()
Q = int(input())
index = list(map(int, input().split()))
sources = list(map(str, input().split()))
targets = list(map(str, input().split()))
ob = Solution()
print(ob.findAndReplace(S, Q, index, sources, targets)) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN FUNC_CALL STRING VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR |
Given a string S on which you need to perform Q replace operations.
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
Note: All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0,1], sources = ["ab", "bc"] is not a valid test case.
Example 1:
Input:
S = "gforks"
Q = 2
index[] = {0, 4}
sources[] = {"g", "ks"}
targets[] = {"geeks", "geeks"}
Output:
geeksforgeeks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". Similarly, "ks" starts at index 4,
and is replaced by "geeks".
Example 2:
Input:
S = "gforks"
Q = 2
index[] = {0, 3}
sources[] = {"g", "ss"}
targets[] = {"geeks", "geeks"}
Output:
geeksforks
Explanation:
"g" starts at index 0, so, it's replaced by
"geeks". "ss" doesn't start at index 3 in
original S, so it's not replaced.
Your Task:
You don't need to read input or print anything. You only need to complete the function findAndReplace() that takes a string S, an integer Q, and 3 arrays index, sources, and targets of size Q, as input and returns the new string after all the operations. index[i], sources[i], and targets[i] denotes the index, sources, and targets for i_{th} query.
Expected Time Complexity: O(|S| * Q)
Expected Auxilliary Space: O(Q)
Constraints:
1 ≤ |S| ≤ 10^{4}
1 ≤ Q ≤ 100
1 ≤ length of sources_{i}, targets_{i} ≤ 100 | class Solution:
def findAndReplace(self, S, Q, index, sources, targets):
te = S
S = list(S)
for i in range(Q):
if sources[i] in te:
ind = te.find(sources[i], index[i])
else:
ind = -1
if index[i] == ind:
S[index[i]] = targets[i]
for j in range(ind + 1, len(sources[i]) + ind):
S[j] = ""
return "".join(S) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR STRING RETURN FUNC_CALL STRING VAR |
Let's define a function, $\mbox{f}$, on a string, $\boldsymbol{p}$, of length $\boldsymbol{l}$ as follows:
$f(p)=(p_1\cdot a^{l-1}+p_2\cdot a^{l-2}+\cdots+p_l\cdot a^0)\ \text{mod}\ m$
where $p_i$ denotes the ASCII value of the $i^{\mbox{th}}$ character in string $\boldsymbol{p}$, $a=1\textbf{0000}1$, and $m=10^9+7$.
Nikita has a string, $\boldsymbol{\mathrm{~S~}}$, consisting of $n$ lowercase letters that she wants to perform $\textit{q}$ queries on. Each query consists of an integer, $\boldsymbol{\mbox{k}}$, and you have to find the value of $f(w_k)$ where $w_k$ is the $k^{th}$ alphabetically smallest palindromic substring of $\boldsymbol{\mathrm{~S~}}$. If $w_k$ doesn't exist, print $-1$ instead.
Input Format
The first line contains $2$ space-separated integers describing the respective values of $n$ (the length of string $\boldsymbol{\mathrm{~S~}}$) and $\textit{q}$ (the number of queries).
The second line contains a single string denoting $\boldsymbol{\mathrm{~S~}}$.
Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{\mbox{k}}$ for a query.
Constraints
$1\leq n,q\leq10^5$
$1\leq k\leq\frac{n\cdot(n+1)}{2}$
It is guaranteed that string $\boldsymbol{\mathrm{~S~}}$ consists of lowercase English alphabetic letters only (i.e., $\mbox{a}$ to $\textbf{Z}$).
$a=10^5+1$
$m=10^9+7$.
Scoring
$1\leq n,q\leq10^3$ for $25\%$ of the test cases.
$1\leq n,q\leq10^5$ for $\textbf{100\%}$ of the test cases.
Output Format
For each query, print the value of function $f(w_k)$ where $w_k$ is the $k^{th}$ alphabetically smallest palindromic substring of $\boldsymbol{\mathrm{~S~}}$; if $w_k$ doesn't exist, print $-1$ instead.
Sample Input
5 7
abcba
1
2
3
4
6
7
8
Sample Output
97
97
696207567
98
29493435
99
-1
Explanation
There are $7$ palindromic substrings of $\textbf{"abcba''}$. Let's list them in lexicographical order and find value of $w_k$:
$w_1=\textbf{”a''}$, $f(w_1)=97$
$w_2=\textbf{“a''}$, $f(w_2)=97$
$w_3=\textbf{"abcba"}$, $f(w_3)=696207567$
$w_4=\textbf{“b''}$, $f(w_4)=98$
$w_5=\textbf{“b''}$, $f(w_5)=98$
$w_6=\textbf{"bcb''}$, $f(w_6)=29493435$
$w_7=\textbf{“c''}$, $f(w_7)=99$
$w_8=$ doesn't exist, so we print $-1$ for $k=8$. | n, q = map(int, input().split())
s = input()
palindromes = [[] for i in range(27)]
def getIndex(char):
return ord(char) - ord("a")
def prepare(s):
for i in range(len(s)):
for j in range(len(s)):
if i + j >= len(s) or i - j < 0:
break
elif s[i + j] == s[i - j]:
palin = s[i - j : i + j + 1]
palindromes[getIndex(s[i - j])].append(palin)
else:
break
if i < len(s) - 1:
for j in range(len(s)):
if i + 1 + j >= len(s) or i - j < 0:
break
elif s[i + 1 + j] == s[i - j]:
palin = s[i - j : i + 1 + j + 1]
palindromes[getIndex(s[i - j])].append(palin)
else:
break
def f(palin):
a = 10**5 + 1
m = 10**9 + 7
l = len(palin)
power = [0] * l
power[0] = 1
res = 1 * ord(palin[l - 1])
for i in range(1, l):
power[i] = power[i - 1] * a % m
index = l - i - 1
res = (res + ord(palin[index]) * power[i]) % m
return res
prepare(s)
totalPalins = 0
for i in range(27):
palindromes[i].sort()
totalPalins += len(palindromes[i])
for i in range(q):
k = int(input())
if k > totalPalins:
print(-1)
else:
for i in range(0, 27):
if k > len(palindromes[i]):
k = k - len(palindromes[i])
else:
palin = palindromes[i][k - 1]
print(f(palin))
break | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Let's define a function, $\mbox{f}$, on a string, $\boldsymbol{p}$, of length $\boldsymbol{l}$ as follows:
$f(p)=(p_1\cdot a^{l-1}+p_2\cdot a^{l-2}+\cdots+p_l\cdot a^0)\ \text{mod}\ m$
where $p_i$ denotes the ASCII value of the $i^{\mbox{th}}$ character in string $\boldsymbol{p}$, $a=1\textbf{0000}1$, and $m=10^9+7$.
Nikita has a string, $\boldsymbol{\mathrm{~S~}}$, consisting of $n$ lowercase letters that she wants to perform $\textit{q}$ queries on. Each query consists of an integer, $\boldsymbol{\mbox{k}}$, and you have to find the value of $f(w_k)$ where $w_k$ is the $k^{th}$ alphabetically smallest palindromic substring of $\boldsymbol{\mathrm{~S~}}$. If $w_k$ doesn't exist, print $-1$ instead.
Input Format
The first line contains $2$ space-separated integers describing the respective values of $n$ (the length of string $\boldsymbol{\mathrm{~S~}}$) and $\textit{q}$ (the number of queries).
The second line contains a single string denoting $\boldsymbol{\mathrm{~S~}}$.
Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{\mbox{k}}$ for a query.
Constraints
$1\leq n,q\leq10^5$
$1\leq k\leq\frac{n\cdot(n+1)}{2}$
It is guaranteed that string $\boldsymbol{\mathrm{~S~}}$ consists of lowercase English alphabetic letters only (i.e., $\mbox{a}$ to $\textbf{Z}$).
$a=10^5+1$
$m=10^9+7$.
Scoring
$1\leq n,q\leq10^3$ for $25\%$ of the test cases.
$1\leq n,q\leq10^5$ for $\textbf{100\%}$ of the test cases.
Output Format
For each query, print the value of function $f(w_k)$ where $w_k$ is the $k^{th}$ alphabetically smallest palindromic substring of $\boldsymbol{\mathrm{~S~}}$; if $w_k$ doesn't exist, print $-1$ instead.
Sample Input
5 7
abcba
1
2
3
4
6
7
8
Sample Output
97
97
696207567
98
29493435
99
-1
Explanation
There are $7$ palindromic substrings of $\textbf{"abcba''}$. Let's list them in lexicographical order and find value of $w_k$:
$w_1=\textbf{”a''}$, $f(w_1)=97$
$w_2=\textbf{“a''}$, $f(w_2)=97$
$w_3=\textbf{"abcba"}$, $f(w_3)=696207567$
$w_4=\textbf{“b''}$, $f(w_4)=98$
$w_5=\textbf{“b''}$, $f(w_5)=98$
$w_6=\textbf{"bcb''}$, $f(w_6)=29493435$
$w_7=\textbf{“c''}$, $f(w_7)=99$
$w_8=$ doesn't exist, so we print $-1$ for $k=8$. | import sys
a = 100001
m = 10**9 + 7
n, q = map(int, input().strip().split(" "))
s = str(input().strip())
pals = list()
for ndx, char in enumerate(s):
start, end = ndx, ndx
while start >= 0 and end < n and s[start] == s[end]:
pals.append(s[start : end + 1])
start -= 1
end += 1
start, end = ndx, ndx + 1
while start >= 0 and end < n and s[start] == s[end]:
pals.append(s[start : end + 1])
start -= 1
end += 1
pals.sort()
while True:
try:
query = int(input().strip())
try:
char = pals[query - 1]
sumP = 0
l = len(char)
for idx, c in enumerate(char):
sumP += ord(c) * a ** (l - (idx + 1))
print(sumP % m)
except IndexError:
print("-1")
except EOFError:
break | IMPORT ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING VAR |
Let's define a function, $\mbox{f}$, on a string, $\boldsymbol{p}$, of length $\boldsymbol{l}$ as follows:
$f(p)=(p_1\cdot a^{l-1}+p_2\cdot a^{l-2}+\cdots+p_l\cdot a^0)\ \text{mod}\ m$
where $p_i$ denotes the ASCII value of the $i^{\mbox{th}}$ character in string $\boldsymbol{p}$, $a=1\textbf{0000}1$, and $m=10^9+7$.
Nikita has a string, $\boldsymbol{\mathrm{~S~}}$, consisting of $n$ lowercase letters that she wants to perform $\textit{q}$ queries on. Each query consists of an integer, $\boldsymbol{\mbox{k}}$, and you have to find the value of $f(w_k)$ where $w_k$ is the $k^{th}$ alphabetically smallest palindromic substring of $\boldsymbol{\mathrm{~S~}}$. If $w_k$ doesn't exist, print $-1$ instead.
Input Format
The first line contains $2$ space-separated integers describing the respective values of $n$ (the length of string $\boldsymbol{\mathrm{~S~}}$) and $\textit{q}$ (the number of queries).
The second line contains a single string denoting $\boldsymbol{\mathrm{~S~}}$.
Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{\mbox{k}}$ for a query.
Constraints
$1\leq n,q\leq10^5$
$1\leq k\leq\frac{n\cdot(n+1)}{2}$
It is guaranteed that string $\boldsymbol{\mathrm{~S~}}$ consists of lowercase English alphabetic letters only (i.e., $\mbox{a}$ to $\textbf{Z}$).
$a=10^5+1$
$m=10^9+7$.
Scoring
$1\leq n,q\leq10^3$ for $25\%$ of the test cases.
$1\leq n,q\leq10^5$ for $\textbf{100\%}$ of the test cases.
Output Format
For each query, print the value of function $f(w_k)$ where $w_k$ is the $k^{th}$ alphabetically smallest palindromic substring of $\boldsymbol{\mathrm{~S~}}$; if $w_k$ doesn't exist, print $-1$ instead.
Sample Input
5 7
abcba
1
2
3
4
6
7
8
Sample Output
97
97
696207567
98
29493435
99
-1
Explanation
There are $7$ palindromic substrings of $\textbf{"abcba''}$. Let's list them in lexicographical order and find value of $w_k$:
$w_1=\textbf{”a''}$, $f(w_1)=97$
$w_2=\textbf{“a''}$, $f(w_2)=97$
$w_3=\textbf{"abcba"}$, $f(w_3)=696207567$
$w_4=\textbf{“b''}$, $f(w_4)=98$
$w_5=\textbf{“b''}$, $f(w_5)=98$
$w_6=\textbf{"bcb''}$, $f(w_6)=29493435$
$w_7=\textbf{“c''}$, $f(w_7)=99$
$w_8=$ doesn't exist, so we print $-1$ for $k=8$. | import sys
def all_palindromes(text):
results = []
text_length = len(text)
for idx, char in enumerate(text):
start, end = idx, idx
while start >= 0 and end < text_length and text[start] == text[end]:
results.append(text[start : end + 1])
start -= 1
end += 1
start, end = idx, idx + 1
while start >= 0 and end < text_length and text[start] == text[end]:
results.append(text[start : end + 1])
start -= 1
end += 1
return results
global a
global m
a = 100001
m = 10**9 + 7
def fx(obj):
res = 0
l = len(obj)
for i, c in enumerate(obj):
res += ord(c) * a ** (l - (i + 1))
return res % m
def main():
s, q = input().split(" ")
q = int(q)
s = int(s)
string = input()
queryval = []
for i in range(q):
queryval.append(int(input()))
output = all_palindromes(string)
output.sort()
maxi = len(output)
for i in range(q):
if queryval[i] > maxi:
sys.stdout.write("-1 \n")
else:
sys.stdout.write(str(fx(output[queryval[i] - 1])) + "\n")
main() | IMPORT FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER STRING EXPR FUNC_CALL VAR |
Let's define a function, $\mbox{f}$, on a string, $\boldsymbol{p}$, of length $\boldsymbol{l}$ as follows:
$f(p)=(p_1\cdot a^{l-1}+p_2\cdot a^{l-2}+\cdots+p_l\cdot a^0)\ \text{mod}\ m$
where $p_i$ denotes the ASCII value of the $i^{\mbox{th}}$ character in string $\boldsymbol{p}$, $a=1\textbf{0000}1$, and $m=10^9+7$.
Nikita has a string, $\boldsymbol{\mathrm{~S~}}$, consisting of $n$ lowercase letters that she wants to perform $\textit{q}$ queries on. Each query consists of an integer, $\boldsymbol{\mbox{k}}$, and you have to find the value of $f(w_k)$ where $w_k$ is the $k^{th}$ alphabetically smallest palindromic substring of $\boldsymbol{\mathrm{~S~}}$. If $w_k$ doesn't exist, print $-1$ instead.
Input Format
The first line contains $2$ space-separated integers describing the respective values of $n$ (the length of string $\boldsymbol{\mathrm{~S~}}$) and $\textit{q}$ (the number of queries).
The second line contains a single string denoting $\boldsymbol{\mathrm{~S~}}$.
Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{\mbox{k}}$ for a query.
Constraints
$1\leq n,q\leq10^5$
$1\leq k\leq\frac{n\cdot(n+1)}{2}$
It is guaranteed that string $\boldsymbol{\mathrm{~S~}}$ consists of lowercase English alphabetic letters only (i.e., $\mbox{a}$ to $\textbf{Z}$).
$a=10^5+1$
$m=10^9+7$.
Scoring
$1\leq n,q\leq10^3$ for $25\%$ of the test cases.
$1\leq n,q\leq10^5$ for $\textbf{100\%}$ of the test cases.
Output Format
For each query, print the value of function $f(w_k)$ where $w_k$ is the $k^{th}$ alphabetically smallest palindromic substring of $\boldsymbol{\mathrm{~S~}}$; if $w_k$ doesn't exist, print $-1$ instead.
Sample Input
5 7
abcba
1
2
3
4
6
7
8
Sample Output
97
97
696207567
98
29493435
99
-1
Explanation
There are $7$ palindromic substrings of $\textbf{"abcba''}$. Let's list them in lexicographical order and find value of $w_k$:
$w_1=\textbf{”a''}$, $f(w_1)=97$
$w_2=\textbf{“a''}$, $f(w_2)=97$
$w_3=\textbf{"abcba"}$, $f(w_3)=696207567$
$w_4=\textbf{“b''}$, $f(w_4)=98$
$w_5=\textbf{“b''}$, $f(w_5)=98$
$w_6=\textbf{"bcb''}$, $f(w_6)=29493435$
$w_7=\textbf{“c''}$, $f(w_7)=99$
$w_8=$ doesn't exist, so we print $-1$ for $k=8$. | def Function(S):
result = 0
for i, s in enumerate(reversed(S)):
result += ord(s) * (10**5 + 1) ** i
result %= 10**9 + 7
return result
def FindPalindromes(S, left, right):
result = []
while left >= 0 and right < len(S) and S[left] == S[right]:
result += [S[left : right + 1]]
left -= 1
right += 1
return result
_, nQueries = map(int, input().split())
S = input()
result = list(S)
for i in range(len(S)):
result += FindPalindromes(S, i - 1, i)
result += FindPalindromes(S, i - 1, i + 1)
result.sort()
for __ in range(nQueries):
K = int(input()) - 1
print(Function(result[K]) if K < len(result) else -1) | FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST WHILE VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR LIST VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER |
Let's define a function, $\mbox{f}$, on a string, $\boldsymbol{p}$, of length $\boldsymbol{l}$ as follows:
$f(p)=(p_1\cdot a^{l-1}+p_2\cdot a^{l-2}+\cdots+p_l\cdot a^0)\ \text{mod}\ m$
where $p_i$ denotes the ASCII value of the $i^{\mbox{th}}$ character in string $\boldsymbol{p}$, $a=1\textbf{0000}1$, and $m=10^9+7$.
Nikita has a string, $\boldsymbol{\mathrm{~S~}}$, consisting of $n$ lowercase letters that she wants to perform $\textit{q}$ queries on. Each query consists of an integer, $\boldsymbol{\mbox{k}}$, and you have to find the value of $f(w_k)$ where $w_k$ is the $k^{th}$ alphabetically smallest palindromic substring of $\boldsymbol{\mathrm{~S~}}$. If $w_k$ doesn't exist, print $-1$ instead.
Input Format
The first line contains $2$ space-separated integers describing the respective values of $n$ (the length of string $\boldsymbol{\mathrm{~S~}}$) and $\textit{q}$ (the number of queries).
The second line contains a single string denoting $\boldsymbol{\mathrm{~S~}}$.
Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{\mbox{k}}$ for a query.
Constraints
$1\leq n,q\leq10^5$
$1\leq k\leq\frac{n\cdot(n+1)}{2}$
It is guaranteed that string $\boldsymbol{\mathrm{~S~}}$ consists of lowercase English alphabetic letters only (i.e., $\mbox{a}$ to $\textbf{Z}$).
$a=10^5+1$
$m=10^9+7$.
Scoring
$1\leq n,q\leq10^3$ for $25\%$ of the test cases.
$1\leq n,q\leq10^5$ for $\textbf{100\%}$ of the test cases.
Output Format
For each query, print the value of function $f(w_k)$ where $w_k$ is the $k^{th}$ alphabetically smallest palindromic substring of $\boldsymbol{\mathrm{~S~}}$; if $w_k$ doesn't exist, print $-1$ instead.
Sample Input
5 7
abcba
1
2
3
4
6
7
8
Sample Output
97
97
696207567
98
29493435
99
-1
Explanation
There are $7$ palindromic substrings of $\textbf{"abcba''}$. Let's list them in lexicographical order and find value of $w_k$:
$w_1=\textbf{”a''}$, $f(w_1)=97$
$w_2=\textbf{“a''}$, $f(w_2)=97$
$w_3=\textbf{"abcba"}$, $f(w_3)=696207567$
$w_4=\textbf{“b''}$, $f(w_4)=98$
$w_5=\textbf{“b''}$, $f(w_5)=98$
$w_6=\textbf{"bcb''}$, $f(w_6)=29493435$
$w_7=\textbf{“c''}$, $f(w_7)=99$
$w_8=$ doesn't exist, so we print $-1$ for $k=8$. | A = 10**5 + 1
M = 10**9 + 7
a_powers = []
def add_palindromes(result, string, i, j):
while i >= 0 and j < len(string) and string[i] == string[j]:
result.append(string[i : j + 1])
i -= 1
j += 1
def get_palindromes(string):
result = []
for i in range(len(string)):
add_palindromes(result, string, i, i + 1)
add_palindromes(result, string, i, i)
return result
def f(w):
total = 0
for i in range(len(w)):
total += ord(w[i]) * a_powers[len(w) - i - 1] % M
total %= M
return total
n, q = map(int, input().split())
a_cur = 1
a_powers.append(a_cur)
for i in range(n):
a_cur = a_cur * A % M
a_powers.append(a_cur)
s = input()
palindromes = sorted(get_palindromes(s))
for i in range(q):
k = int(input())
if k > len(palindromes):
print(-1)
else:
print(f(palindromes[k - 1])) | ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST FUNC_DEF WHILE VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER |
Let's define a function, $\mbox{f}$, on a string, $\boldsymbol{p}$, of length $\boldsymbol{l}$ as follows:
$f(p)=(p_1\cdot a^{l-1}+p_2\cdot a^{l-2}+\cdots+p_l\cdot a^0)\ \text{mod}\ m$
where $p_i$ denotes the ASCII value of the $i^{\mbox{th}}$ character in string $\boldsymbol{p}$, $a=1\textbf{0000}1$, and $m=10^9+7$.
Nikita has a string, $\boldsymbol{\mathrm{~S~}}$, consisting of $n$ lowercase letters that she wants to perform $\textit{q}$ queries on. Each query consists of an integer, $\boldsymbol{\mbox{k}}$, and you have to find the value of $f(w_k)$ where $w_k$ is the $k^{th}$ alphabetically smallest palindromic substring of $\boldsymbol{\mathrm{~S~}}$. If $w_k$ doesn't exist, print $-1$ instead.
Input Format
The first line contains $2$ space-separated integers describing the respective values of $n$ (the length of string $\boldsymbol{\mathrm{~S~}}$) and $\textit{q}$ (the number of queries).
The second line contains a single string denoting $\boldsymbol{\mathrm{~S~}}$.
Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{\mbox{k}}$ for a query.
Constraints
$1\leq n,q\leq10^5$
$1\leq k\leq\frac{n\cdot(n+1)}{2}$
It is guaranteed that string $\boldsymbol{\mathrm{~S~}}$ consists of lowercase English alphabetic letters only (i.e., $\mbox{a}$ to $\textbf{Z}$).
$a=10^5+1$
$m=10^9+7$.
Scoring
$1\leq n,q\leq10^3$ for $25\%$ of the test cases.
$1\leq n,q\leq10^5$ for $\textbf{100\%}$ of the test cases.
Output Format
For each query, print the value of function $f(w_k)$ where $w_k$ is the $k^{th}$ alphabetically smallest palindromic substring of $\boldsymbol{\mathrm{~S~}}$; if $w_k$ doesn't exist, print $-1$ instead.
Sample Input
5 7
abcba
1
2
3
4
6
7
8
Sample Output
97
97
696207567
98
29493435
99
-1
Explanation
There are $7$ palindromic substrings of $\textbf{"abcba''}$. Let's list them in lexicographical order and find value of $w_k$:
$w_1=\textbf{”a''}$, $f(w_1)=97$
$w_2=\textbf{“a''}$, $f(w_2)=97$
$w_3=\textbf{"abcba"}$, $f(w_3)=696207567$
$w_4=\textbf{“b''}$, $f(w_4)=98$
$w_5=\textbf{“b''}$, $f(w_5)=98$
$w_6=\textbf{"bcb''}$, $f(w_6)=29493435$
$w_7=\textbf{“c''}$, $f(w_7)=99$
$w_8=$ doesn't exist, so we print $-1$ for $k=8$. | PRIME = 1000000007
BASE = 100001
def power(a, n, modulo):
if a == 0:
if n == 0:
return 1
else:
return 0
result = 1
current = a
while n > 0:
if n % 2 == 1:
result = result * current % modulo
n //= 2
current = current * current % modulo
return result
def function(w, base, modulo):
value = 0
coefficient = 1
for c in reversed(w):
value = (value + ord(c) * coefficient) % modulo
coefficient = coefficient * base % modulo
return value
def generate_substrings(string, predicate):
result = []
for length in range(1, len(string) + 1):
for position in range(len(string) - length + 1):
substring = string[position : position + length]
if predicate(substring):
result.append(substring)
return result
class SubString:
def __init__(self, origin, start, length):
self.origin = origin
self.start = start
self.length = length
self._hash = None
def value(self):
return self.origin[self.start : self.start + self.length]
def _get_hash(self):
return hash(self.value())
def __hash__(self):
if not self._hash:
self._hash = self._get_hash()
return self._hash
def __cmp__(self, other):
return self.value().__cmp__(other.value())
def generate_subpalindromes(string):
result = []
string_length = len(string)
for location in range(string_length):
start = location
end = location + 1
while start >= 0 and end <= string_length and string[start] == string[end - 1]:
palindrome = string[start:end]
result.append(palindrome)
start -= 1
end += 1
start = location
end = location + 2
while start >= 0 and end <= string_length and string[start] == string[end - 1]:
palindrome = string[start:end]
result.append(palindrome)
start -= 1
end += 1
return result
def is_palindrome(s):
for position in range(len(s) // 2):
if s[position] != s[len(s) - 1 - position]:
return False
return True
def main():
_, queries = list(map(int, input().split()))
string = input().strip()
substrings = generate_subpalindromes(string)
substrings.sort()
for _ in range(queries):
k = int(input()) - 1
if k < len(substrings):
substring = substrings[k]
print(function(substring, BASE, PRIME))
else:
print(-1)
main() | ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR 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 VAR EXPR FUNC_CALL VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE FUNC_DEF RETURN VAR VAR BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR ASSIGN VAR FUNC_CALL VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR |
Let's define a function, $\mbox{f}$, on a string, $\boldsymbol{p}$, of length $\boldsymbol{l}$ as follows:
$f(p)=(p_1\cdot a^{l-1}+p_2\cdot a^{l-2}+\cdots+p_l\cdot a^0)\ \text{mod}\ m$
where $p_i$ denotes the ASCII value of the $i^{\mbox{th}}$ character in string $\boldsymbol{p}$, $a=1\textbf{0000}1$, and $m=10^9+7$.
Nikita has a string, $\boldsymbol{\mathrm{~S~}}$, consisting of $n$ lowercase letters that she wants to perform $\textit{q}$ queries on. Each query consists of an integer, $\boldsymbol{\mbox{k}}$, and you have to find the value of $f(w_k)$ where $w_k$ is the $k^{th}$ alphabetically smallest palindromic substring of $\boldsymbol{\mathrm{~S~}}$. If $w_k$ doesn't exist, print $-1$ instead.
Input Format
The first line contains $2$ space-separated integers describing the respective values of $n$ (the length of string $\boldsymbol{\mathrm{~S~}}$) and $\textit{q}$ (the number of queries).
The second line contains a single string denoting $\boldsymbol{\mathrm{~S~}}$.
Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{\mbox{k}}$ for a query.
Constraints
$1\leq n,q\leq10^5$
$1\leq k\leq\frac{n\cdot(n+1)}{2}$
It is guaranteed that string $\boldsymbol{\mathrm{~S~}}$ consists of lowercase English alphabetic letters only (i.e., $\mbox{a}$ to $\textbf{Z}$).
$a=10^5+1$
$m=10^9+7$.
Scoring
$1\leq n,q\leq10^3$ for $25\%$ of the test cases.
$1\leq n,q\leq10^5$ for $\textbf{100\%}$ of the test cases.
Output Format
For each query, print the value of function $f(w_k)$ where $w_k$ is the $k^{th}$ alphabetically smallest palindromic substring of $\boldsymbol{\mathrm{~S~}}$; if $w_k$ doesn't exist, print $-1$ instead.
Sample Input
5 7
abcba
1
2
3
4
6
7
8
Sample Output
97
97
696207567
98
29493435
99
-1
Explanation
There are $7$ palindromic substrings of $\textbf{"abcba''}$. Let's list them in lexicographical order and find value of $w_k$:
$w_1=\textbf{”a''}$, $f(w_1)=97$
$w_2=\textbf{“a''}$, $f(w_2)=97$
$w_3=\textbf{"abcba"}$, $f(w_3)=696207567$
$w_4=\textbf{“b''}$, $f(w_4)=98$
$w_5=\textbf{“b''}$, $f(w_5)=98$
$w_6=\textbf{"bcb''}$, $f(w_6)=29493435$
$w_7=\textbf{“c''}$, $f(w_7)=99$
$w_8=$ doesn't exist, so we print $-1$ for $k=8$. | def f(word):
a = 100001
m = 10**9 + 7
l = len(word)
result = 0
for letter in word:
result = (result * a + ord(letter)) % m
return result
def all_palindromes(word):
palindromes = []
n = len(word)
for idx, char in enumerate(word):
i, j = idx, idx
while i >= 0 and j < n and word[i] == word[j]:
palindromes.append(word[i : j + 1])
i -= 1
j += 1
i, j = idx, idx + 1
while i >= 0 and j < n and word[i] == word[j]:
palindromes.append(word[i : j + 1])
i -= 1
j += 1
return sorted(palindromes)
n, q = [int(x) for x in input().strip().split()]
word = input().strip()
palindromes = all_palindromes(word)
N = len(palindromes)
for _ in range(q):
idx = int(input().strip())
if idx <= N:
print(f(palindromes[idx - 1]))
else:
print(-1) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
Let's define a function, $\mbox{f}$, on a string, $\boldsymbol{p}$, of length $\boldsymbol{l}$ as follows:
$f(p)=(p_1\cdot a^{l-1}+p_2\cdot a^{l-2}+\cdots+p_l\cdot a^0)\ \text{mod}\ m$
where $p_i$ denotes the ASCII value of the $i^{\mbox{th}}$ character in string $\boldsymbol{p}$, $a=1\textbf{0000}1$, and $m=10^9+7$.
Nikita has a string, $\boldsymbol{\mathrm{~S~}}$, consisting of $n$ lowercase letters that she wants to perform $\textit{q}$ queries on. Each query consists of an integer, $\boldsymbol{\mbox{k}}$, and you have to find the value of $f(w_k)$ where $w_k$ is the $k^{th}$ alphabetically smallest palindromic substring of $\boldsymbol{\mathrm{~S~}}$. If $w_k$ doesn't exist, print $-1$ instead.
Input Format
The first line contains $2$ space-separated integers describing the respective values of $n$ (the length of string $\boldsymbol{\mathrm{~S~}}$) and $\textit{q}$ (the number of queries).
The second line contains a single string denoting $\boldsymbol{\mathrm{~S~}}$.
Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{\mbox{k}}$ for a query.
Constraints
$1\leq n,q\leq10^5$
$1\leq k\leq\frac{n\cdot(n+1)}{2}$
It is guaranteed that string $\boldsymbol{\mathrm{~S~}}$ consists of lowercase English alphabetic letters only (i.e., $\mbox{a}$ to $\textbf{Z}$).
$a=10^5+1$
$m=10^9+7$.
Scoring
$1\leq n,q\leq10^3$ for $25\%$ of the test cases.
$1\leq n,q\leq10^5$ for $\textbf{100\%}$ of the test cases.
Output Format
For each query, print the value of function $f(w_k)$ where $w_k$ is the $k^{th}$ alphabetically smallest palindromic substring of $\boldsymbol{\mathrm{~S~}}$; if $w_k$ doesn't exist, print $-1$ instead.
Sample Input
5 7
abcba
1
2
3
4
6
7
8
Sample Output
97
97
696207567
98
29493435
99
-1
Explanation
There are $7$ palindromic substrings of $\textbf{"abcba''}$. Let's list them in lexicographical order and find value of $w_k$:
$w_1=\textbf{”a''}$, $f(w_1)=97$
$w_2=\textbf{“a''}$, $f(w_2)=97$
$w_3=\textbf{"abcba"}$, $f(w_3)=696207567$
$w_4=\textbf{“b''}$, $f(w_4)=98$
$w_5=\textbf{“b''}$, $f(w_5)=98$
$w_6=\textbf{"bcb''}$, $f(w_6)=29493435$
$w_7=\textbf{“c''}$, $f(w_7)=99$
$w_8=$ doesn't exist, so we print $-1$ for $k=8$. | n, q = (int(x) for x in input().split(" "))
text = input()
palindromes = {}
MOD = 1000000007
def func(string):
t = len(string) - 1
s = 0
for c in string:
s += ord(c) * pow(100001, t, MOD)
t -= 1
s %= MOD
return s
def calculate():
N = n
if N == 0:
return
N = 2 * N + 1
L = [0] * N
L[0] = 0
L[1] = 1
C = 1
R = 2
i = 0
iMirror = 0
start = -1
end = -1
diff = -1
palindromes[text[0]] = 1
for i in range(2, N):
iMirror = 2 * C - i
L[i] = 0
diff = R - i
if diff > 0:
L[i] = min(L[iMirror], diff)
try:
while (i + L[i] < N and i - L[i] > 0) and (
(i + L[i] + 1) % 2 == 0
or text[(i + L[i] + 1) // 2] == text[(i - L[i] - 1) // 2]
):
L[i] += 1
except Exception as e:
pass
if L[i] > 0:
j = L[i]
while j > 0:
start = (i - j) // 2
end = start + j
word = text[start:end]
j -= 2
if word in palindromes:
palindromes[word] += 1
else:
palindromes[word] = 1
if i + L[i] > R:
C = i
R = i + L[i]
calculate()
pals = list(palindromes.keys())
pals.sort()
counter = [(pals[0], palindromes[pals[0]], func(pals[0]))]
for k in range(1, len(pals)):
triple = pals[k], palindromes[pals[k]] + counter[k - 1][1], func(pals[k])
counter.append(triple)
while q > 0:
q -= 1
index = int(input()) - 1
if index >= counter[-1][1]:
print(-1)
else:
for t in counter:
if t[1] > index:
print(t[2])
break | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR IF VAR NUMBER RETURN ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR WHILE BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER |
Let's define a function, $\mbox{f}$, on a string, $\boldsymbol{p}$, of length $\boldsymbol{l}$ as follows:
$f(p)=(p_1\cdot a^{l-1}+p_2\cdot a^{l-2}+\cdots+p_l\cdot a^0)\ \text{mod}\ m$
where $p_i$ denotes the ASCII value of the $i^{\mbox{th}}$ character in string $\boldsymbol{p}$, $a=1\textbf{0000}1$, and $m=10^9+7$.
Nikita has a string, $\boldsymbol{\mathrm{~S~}}$, consisting of $n$ lowercase letters that she wants to perform $\textit{q}$ queries on. Each query consists of an integer, $\boldsymbol{\mbox{k}}$, and you have to find the value of $f(w_k)$ where $w_k$ is the $k^{th}$ alphabetically smallest palindromic substring of $\boldsymbol{\mathrm{~S~}}$. If $w_k$ doesn't exist, print $-1$ instead.
Input Format
The first line contains $2$ space-separated integers describing the respective values of $n$ (the length of string $\boldsymbol{\mathrm{~S~}}$) and $\textit{q}$ (the number of queries).
The second line contains a single string denoting $\boldsymbol{\mathrm{~S~}}$.
Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{\mbox{k}}$ for a query.
Constraints
$1\leq n,q\leq10^5$
$1\leq k\leq\frac{n\cdot(n+1)}{2}$
It is guaranteed that string $\boldsymbol{\mathrm{~S~}}$ consists of lowercase English alphabetic letters only (i.e., $\mbox{a}$ to $\textbf{Z}$).
$a=10^5+1$
$m=10^9+7$.
Scoring
$1\leq n,q\leq10^3$ for $25\%$ of the test cases.
$1\leq n,q\leq10^5$ for $\textbf{100\%}$ of the test cases.
Output Format
For each query, print the value of function $f(w_k)$ where $w_k$ is the $k^{th}$ alphabetically smallest palindromic substring of $\boldsymbol{\mathrm{~S~}}$; if $w_k$ doesn't exist, print $-1$ instead.
Sample Input
5 7
abcba
1
2
3
4
6
7
8
Sample Output
97
97
696207567
98
29493435
99
-1
Explanation
There are $7$ palindromic substrings of $\textbf{"abcba''}$. Let's list them in lexicographical order and find value of $w_k$:
$w_1=\textbf{”a''}$, $f(w_1)=97$
$w_2=\textbf{“a''}$, $f(w_2)=97$
$w_3=\textbf{"abcba"}$, $f(w_3)=696207567$
$w_4=\textbf{“b''}$, $f(w_4)=98$
$w_5=\textbf{“b''}$, $f(w_5)=98$
$w_6=\textbf{"bcb''}$, $f(w_6)=29493435$
$w_7=\textbf{“c''}$, $f(w_7)=99$
$w_8=$ doesn't exist, so we print $-1$ for $k=8$. | def f(text):
ans = 0
l = len(text)
if l % 2 == 0:
for i in range(l // 2):
ans += (
ord(text[i]) * a ** (l - (i + 1)) % m + ord(text[-(i + 1)]) * a**i % m
) % m
else:
for i in range(l // 2):
ans += (
ord(text[i]) * a ** (l - (i + 1)) % m + ord(text[-(i + 1)]) * a**i % m
) % m
ans += ord(text[l // 2]) * a ** (l // 2) % m
return ans % m
def all_palindromes(text):
results = list()
text_length = len(text)
for idx, char in enumerate(text):
results.append(char)
start, end = idx - 1, idx + 1
while start >= 0 and end < text_length and text[start] == text[end]:
results.append(text[start : end + 1])
start -= 1
end += 1
start, end = idx, idx + 1
while start >= 0 and end < text_length and text[start] == text[end]:
results.append(text[start : end + 1])
start -= 1
end += 1
return list(results)
n, q = map(int, input().split())
t = input()
ar = all_palindromes(t)
ar.sort()
a = 10**5 + 1
m = 10**9 + 7
for _ in range(q):
pos = int(input())
pos -= 1
if pos >= len(ar):
print(-1)
else:
palin = ar[pos]
print(f(palin)) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well.
Chef is planning to setup a secure password for his Codechef account. For a password to be secure the following conditions should be satisfied:
1) Password must contain at least one lower case letter $[a-z]$;
2) Password must contain at least one upper case letter $[A-Z]$ strictly inside, i.e. not as the first or the last character;
3) Password must contain at least one digit $[0-9]$ strictly inside;
4) Password must contain at least one special character from the set $\{$ '@', '\#', '%', '&', '?' $\}$ strictly inside;
5) Password must be at least $10$ characters in length, but it can be longer.
Chef has generated several strings and now wants you to check whether the passwords are secure based on the above criteria. Please help Chef in doing so.
------ Input ------
First line will contain $T$, number of testcases. Then the testcases follow.
Each testcase contains of a single line of input, string $S$.
------ Output ------
For each testcase, output in a single line "YES" if the password is secure and "NO" if it is not.
------ Constraints ------
$1 ≤ |S| ≤ 20$
All the characters in $S$ are one of the following: lower case letters $[a-z]$, upper case letters $[A-Z]$, digits $[0-9]$, special characters from the set $\{$ '@', '\#', '%', '&', '?' $\}$
Sum of length of strings over all tests is atmost $10^{6}$
----- Sample Input 1 ------
3
#cookOff#P1
U@code4CHEFINA
gR3@tPWD
----- Sample Output 1 ------
NO
YES
NO
----- explanation 1 ------
Example case 1: Condition $3$ is not satisfied, because the only digit is not strictly inside.
Example case 2: All conditions are satisfied.
Example case 3: Condition $5$ is not satisfied, because the length of this string is 8. | for i in range(int(input())):
Low, Up, Num, Spc = 0, 0, 0, 0
S = input()
if len(S) >= 10:
for i in range(len(S)):
if S[i] >= "a" and S[i] <= "z":
Low = 1
if i > 0 and i < len(S) - 1:
if S[i] >= "A" and S[i] <= "Z":
Up = 1
if S[i] >= "0" and S[i] <= "9":
Num = 1
if (
S[i] == "@"
or S[i] == "%"
or S[i] == "#"
or S[i] == "&"
or S[i] == "?"
):
Spc = 1
if Low and Up and Num and Spc:
print("YES")
else:
print("NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well.
Chef is planning to setup a secure password for his Codechef account. For a password to be secure the following conditions should be satisfied:
1) Password must contain at least one lower case letter $[a-z]$;
2) Password must contain at least one upper case letter $[A-Z]$ strictly inside, i.e. not as the first or the last character;
3) Password must contain at least one digit $[0-9]$ strictly inside;
4) Password must contain at least one special character from the set $\{$ '@', '\#', '%', '&', '?' $\}$ strictly inside;
5) Password must be at least $10$ characters in length, but it can be longer.
Chef has generated several strings and now wants you to check whether the passwords are secure based on the above criteria. Please help Chef in doing so.
------ Input ------
First line will contain $T$, number of testcases. Then the testcases follow.
Each testcase contains of a single line of input, string $S$.
------ Output ------
For each testcase, output in a single line "YES" if the password is secure and "NO" if it is not.
------ Constraints ------
$1 ≤ |S| ≤ 20$
All the characters in $S$ are one of the following: lower case letters $[a-z]$, upper case letters $[A-Z]$, digits $[0-9]$, special characters from the set $\{$ '@', '\#', '%', '&', '?' $\}$
Sum of length of strings over all tests is atmost $10^{6}$
----- Sample Input 1 ------
3
#cookOff#P1
U@code4CHEFINA
gR3@tPWD
----- Sample Output 1 ------
NO
YES
NO
----- explanation 1 ------
Example case 1: Condition $3$ is not satisfied, because the only digit is not strictly inside.
Example case 2: All conditions are satisfied.
Example case 3: Condition $5$ is not satisfied, because the length of this string is 8. | q = int(input())
for i in range(q):
s = input()
countC = 0
countD = 0
countS = 0
countL = 0
n = len(s)
if n > 9:
for t in s[0:n]:
if t.islower() == True:
countL += 1
break
for j in s[1 : n - 1]:
if j.isupper() == True:
countC += 1
break
for k in s[1 : n - 1]:
if k.isdigit() == True:
countD += 1
break
for p in s[1 : n - 1]:
if p == "@" or p == "#" or p == "%" or p == "&" or p == "?":
countS += 1
break
if countC == 1 and countD == 1 and countS == 1 and countL == 1:
print("YES")
else:
print("NO")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR VAR NUMBER VAR IF FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well.
Chef is planning to setup a secure password for his Codechef account. For a password to be secure the following conditions should be satisfied:
1) Password must contain at least one lower case letter $[a-z]$;
2) Password must contain at least one upper case letter $[A-Z]$ strictly inside, i.e. not as the first or the last character;
3) Password must contain at least one digit $[0-9]$ strictly inside;
4) Password must contain at least one special character from the set $\{$ '@', '\#', '%', '&', '?' $\}$ strictly inside;
5) Password must be at least $10$ characters in length, but it can be longer.
Chef has generated several strings and now wants you to check whether the passwords are secure based on the above criteria. Please help Chef in doing so.
------ Input ------
First line will contain $T$, number of testcases. Then the testcases follow.
Each testcase contains of a single line of input, string $S$.
------ Output ------
For each testcase, output in a single line "YES" if the password is secure and "NO" if it is not.
------ Constraints ------
$1 ≤ |S| ≤ 20$
All the characters in $S$ are one of the following: lower case letters $[a-z]$, upper case letters $[A-Z]$, digits $[0-9]$, special characters from the set $\{$ '@', '\#', '%', '&', '?' $\}$
Sum of length of strings over all tests is atmost $10^{6}$
----- Sample Input 1 ------
3
#cookOff#P1
U@code4CHEFINA
gR3@tPWD
----- Sample Output 1 ------
NO
YES
NO
----- explanation 1 ------
Example case 1: Condition $3$ is not satisfied, because the only digit is not strictly inside.
Example case 2: All conditions are satisfied.
Example case 3: Condition $5$ is not satisfied, because the length of this string is 8. | T = int(input())
while T != 0:
password = input()
if len(password) >= 10:
speclCon = False
upperCon = False
lowerCon = False
digitCon = False
insidePass = password[1 : len(password) - 1]
for i in insidePass:
if i in "abcdefghijklmnopqrstuvwxyz":
lowerCon = True
if i in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
upperCon = True
if i in "0123456789":
digitCon = True
if i in "@#%&?":
speclCon = True
if speclCon and digitCon and lowerCon and upperCon:
print("YES")
break
else:
lowerCon = (
password[0] in "abcdefghijklmnopqrstuvwxyz"
or password[-1] in "abcdefghijklmnopqrstuvwxyz"
)
if speclCon and digitCon and lowerCon and upperCon:
print("YES")
else:
print("NO")
else:
print("NO")
T -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR NUMBER IF VAR STRING ASSIGN VAR NUMBER IF VAR STRING ASSIGN VAR NUMBER IF VAR STRING ASSIGN VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER STRING VAR NUMBER STRING IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well.
Chef is planning to setup a secure password for his Codechef account. For a password to be secure the following conditions should be satisfied:
1) Password must contain at least one lower case letter $[a-z]$;
2) Password must contain at least one upper case letter $[A-Z]$ strictly inside, i.e. not as the first or the last character;
3) Password must contain at least one digit $[0-9]$ strictly inside;
4) Password must contain at least one special character from the set $\{$ '@', '\#', '%', '&', '?' $\}$ strictly inside;
5) Password must be at least $10$ characters in length, but it can be longer.
Chef has generated several strings and now wants you to check whether the passwords are secure based on the above criteria. Please help Chef in doing so.
------ Input ------
First line will contain $T$, number of testcases. Then the testcases follow.
Each testcase contains of a single line of input, string $S$.
------ Output ------
For each testcase, output in a single line "YES" if the password is secure and "NO" if it is not.
------ Constraints ------
$1 ≤ |S| ≤ 20$
All the characters in $S$ are one of the following: lower case letters $[a-z]$, upper case letters $[A-Z]$, digits $[0-9]$, special characters from the set $\{$ '@', '\#', '%', '&', '?' $\}$
Sum of length of strings over all tests is atmost $10^{6}$
----- Sample Input 1 ------
3
#cookOff#P1
U@code4CHEFINA
gR3@tPWD
----- Sample Output 1 ------
NO
YES
NO
----- explanation 1 ------
Example case 1: Condition $3$ is not satisfied, because the only digit is not strictly inside.
Example case 2: All conditions are satisfied.
Example case 3: Condition $5$ is not satisfied, because the length of this string is 8. | for _ in range(int(input())):
s = input()
flag1 = flag2 = flag3 = flag4 = 0
if len(s) > 9:
for i in range(1, len(s) - 1):
if s[i] in "@#%&?":
flag1 = 1
if 64 < ord(s[i]) < 91:
flag2 = 1
if s[i].isdigit():
flag3 = 1
for i in range(len(s)):
if 96 < ord(s[i]) < 123:
flag4 = 1
if flag1 and flag2 and flag3 and flag4:
print("YES")
else:
print("NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER IF NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well.
Chef is planning to setup a secure password for his Codechef account. For a password to be secure the following conditions should be satisfied:
1) Password must contain at least one lower case letter $[a-z]$;
2) Password must contain at least one upper case letter $[A-Z]$ strictly inside, i.e. not as the first or the last character;
3) Password must contain at least one digit $[0-9]$ strictly inside;
4) Password must contain at least one special character from the set $\{$ '@', '\#', '%', '&', '?' $\}$ strictly inside;
5) Password must be at least $10$ characters in length, but it can be longer.
Chef has generated several strings and now wants you to check whether the passwords are secure based on the above criteria. Please help Chef in doing so.
------ Input ------
First line will contain $T$, number of testcases. Then the testcases follow.
Each testcase contains of a single line of input, string $S$.
------ Output ------
For each testcase, output in a single line "YES" if the password is secure and "NO" if it is not.
------ Constraints ------
$1 ≤ |S| ≤ 20$
All the characters in $S$ are one of the following: lower case letters $[a-z]$, upper case letters $[A-Z]$, digits $[0-9]$, special characters from the set $\{$ '@', '\#', '%', '&', '?' $\}$
Sum of length of strings over all tests is atmost $10^{6}$
----- Sample Input 1 ------
3
#cookOff#P1
U@code4CHEFINA
gR3@tPWD
----- Sample Output 1 ------
NO
YES
NO
----- explanation 1 ------
Example case 1: Condition $3$ is not satisfied, because the only digit is not strictly inside.
Example case 2: All conditions are satisfied.
Example case 3: Condition $5$ is not satisfied, because the length of this string is 8. | import sys
input = sys.stdin.readline
def inp():
return int(input())
def inlt():
return list(map(int, input().split()))
def insr():
return input().strip()
def invr():
return map(int, input().split())
def outp(n):
sys.stdout.write(str(n) + "\n")
def outlt(lst):
sys.stdout.write(" ".join(map(str, lst)) + "\n")
def outplt(lst):
sys.stdout.write("\n".join(map(str, lst)))
def outpltlt(lst):
sys.stdout.write("\n".join(map(str, (" ".join(map(str, a)) for a in lst))))
special = set([x for x in "@#%&?"])
ans = []
for _ in range(inp()):
S = insr()
l, u, d, s = [False] * 4
an = "NO"
if len(S) >= 10:
if S[0].islower() or S[-1].islower():
l = True
for ch in S[1:-1]:
if not l and ch.islower():
l = True
if not u and ch.isupper():
u = True
if not d and ch.isdigit():
d = True
if not s and ch in special:
s = True
if l and u and d and s:
an = "YES"
ans.append(an)
outplt(ans) | IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR STRING IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER NUMBER IF VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well.
Chef is planning to setup a secure password for his Codechef account. For a password to be secure the following conditions should be satisfied:
1) Password must contain at least one lower case letter $[a-z]$;
2) Password must contain at least one upper case letter $[A-Z]$ strictly inside, i.e. not as the first or the last character;
3) Password must contain at least one digit $[0-9]$ strictly inside;
4) Password must contain at least one special character from the set $\{$ '@', '\#', '%', '&', '?' $\}$ strictly inside;
5) Password must be at least $10$ characters in length, but it can be longer.
Chef has generated several strings and now wants you to check whether the passwords are secure based on the above criteria. Please help Chef in doing so.
------ Input ------
First line will contain $T$, number of testcases. Then the testcases follow.
Each testcase contains of a single line of input, string $S$.
------ Output ------
For each testcase, output in a single line "YES" if the password is secure and "NO" if it is not.
------ Constraints ------
$1 ≤ |S| ≤ 20$
All the characters in $S$ are one of the following: lower case letters $[a-z]$, upper case letters $[A-Z]$, digits $[0-9]$, special characters from the set $\{$ '@', '\#', '%', '&', '?' $\}$
Sum of length of strings over all tests is atmost $10^{6}$
----- Sample Input 1 ------
3
#cookOff#P1
U@code4CHEFINA
gR3@tPWD
----- Sample Output 1 ------
NO
YES
NO
----- explanation 1 ------
Example case 1: Condition $3$ is not satisfied, because the only digit is not strictly inside.
Example case 2: All conditions are satisfied.
Example case 3: Condition $5$ is not satisfied, because the length of this string is 8. | for _ in range(int(input())):
s = input()
n = len(s)
if n < 10:
print("NO")
else:
u, d, sp, l = False, False, False, False
for i in range(n):
if s[i] >= "a" and s[i] <= "z":
l = True
if i > 0 and i < n - 1:
if s[i].isupper():
u = True
elif s[i].isdigit():
d = True
elif s[i] in {"@", "#", "%", "&", "?"}:
sp = True
if u and d and sp and l:
print("YES")
else:
print("NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR STRING STRING STRING STRING STRING ASSIGN VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well.
Chef is planning to setup a secure password for his Codechef account. For a password to be secure the following conditions should be satisfied:
1) Password must contain at least one lower case letter $[a-z]$;
2) Password must contain at least one upper case letter $[A-Z]$ strictly inside, i.e. not as the first or the last character;
3) Password must contain at least one digit $[0-9]$ strictly inside;
4) Password must contain at least one special character from the set $\{$ '@', '\#', '%', '&', '?' $\}$ strictly inside;
5) Password must be at least $10$ characters in length, but it can be longer.
Chef has generated several strings and now wants you to check whether the passwords are secure based on the above criteria. Please help Chef in doing so.
------ Input ------
First line will contain $T$, number of testcases. Then the testcases follow.
Each testcase contains of a single line of input, string $S$.
------ Output ------
For each testcase, output in a single line "YES" if the password is secure and "NO" if it is not.
------ Constraints ------
$1 ≤ |S| ≤ 20$
All the characters in $S$ are one of the following: lower case letters $[a-z]$, upper case letters $[A-Z]$, digits $[0-9]$, special characters from the set $\{$ '@', '\#', '%', '&', '?' $\}$
Sum of length of strings over all tests is atmost $10^{6}$
----- Sample Input 1 ------
3
#cookOff#P1
U@code4CHEFINA
gR3@tPWD
----- Sample Output 1 ------
NO
YES
NO
----- explanation 1 ------
Example case 1: Condition $3$ is not satisfied, because the only digit is not strictly inside.
Example case 2: All conditions are satisfied.
Example case 3: Condition $5$ is not satisfied, because the length of this string is 8. | try:
for _ in range(int(input())):
s = input()
special = {"@", "#", "%", "&", "?"}
c1 = c2 = c3 = c4 = c5 = False
flag = 0
for i in range(len(s)):
if s[i].islower():
c1 = True
if i > 0 and i < len(s) - 1 and s[i].isupper():
c2 = True
if i > 0 and i < len(s) - 1 and s[i].isdigit():
c3 = True
if i > 0 and i < len(s) - 1 and s[i] in special:
c4 = True
if len(s) >= 10:
c5 = True
if c1 and c2 and c3 and c4 and c5:
print("YES")
flag = 1
break
if flag == 0:
print("NO")
except:
pass | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING STRING STRING STRING STRING ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.