description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers. (Maintain the order of all -ve and non-negative numbers as given in the original array). Example 1: Input: N = 4 Arr[] = {-3, 3, -2, 2} Output: -3 -2 3 2 Explanation: In the given array, negative numbers are -3, -2 and non-negative numbers are 3, 2. Example 2: Input: N = 4 Arr[] = {-3, 1, 0, -2} Output: -3 -2 1 0 Explanation: In the given array, negative numbers are -3, -2 and non-negative numbers are 1, 0. Your Task: You don't need to read input or print anything. Your task is to complete the function Rearrange() which takes the array Arr[] and its size N as inputs and returns the array after rearranging with spaces between the elements of the array. Expected Time Complexity: O(N. Log(N)) Expected Auxiliary Space: O(Log(N)) Constraints: 1 ≤ N ≤ 10^{5} -10^{9} ≤ Elements of array ≤ 10^{9}
def Rearrange(a, n): m = [] p = [] for i in range(0, n): if a[i] < 0: m.append(a[i]) else: p.append(a[i]) c = m + p for i in range(0, n): a[i] = c[i]
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers. (Maintain the order of all -ve and non-negative numbers as given in the original array). Example 1: Input: N = 4 Arr[] = {-3, 3, -2, 2} Output: -3 -2 3 2 Explanation: In the given array, negative numbers are -3, -2 and non-negative numbers are 3, 2. Example 2: Input: N = 4 Arr[] = {-3, 1, 0, -2} Output: -3 -2 1 0 Explanation: In the given array, negative numbers are -3, -2 and non-negative numbers are 1, 0. Your Task: You don't need to read input or print anything. Your task is to complete the function Rearrange() which takes the array Arr[] and its size N as inputs and returns the array after rearranging with spaces between the elements of the array. Expected Time Complexity: O(N. Log(N)) Expected Auxiliary Space: O(Log(N)) Constraints: 1 ≤ N ≤ 10^{5} -10^{9} ≤ Elements of array ≤ 10^{9}
def Rearrange(a, n): pos, neg = [], [] for i in a: if i < 0: neg.append(i) else: pos.append(i) a.clear() a.extend(neg + pos)
FUNC_DEF ASSIGN VAR VAR LIST LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers. (Maintain the order of all -ve and non-negative numbers as given in the original array). Example 1: Input: N = 4 Arr[] = {-3, 3, -2, 2} Output: -3 -2 3 2 Explanation: In the given array, negative numbers are -3, -2 and non-negative numbers are 3, 2. Example 2: Input: N = 4 Arr[] = {-3, 1, 0, -2} Output: -3 -2 1 0 Explanation: In the given array, negative numbers are -3, -2 and non-negative numbers are 1, 0. Your Task: You don't need to read input or print anything. Your task is to complete the function Rearrange() which takes the array Arr[] and its size N as inputs and returns the array after rearranging with spaces between the elements of the array. Expected Time Complexity: O(N. Log(N)) Expected Auxiliary Space: O(Log(N)) Constraints: 1 ≤ N ≤ 10^{5} -10^{9} ≤ Elements of array ≤ 10^{9}
def Rearrange(a, n): d = [] e = [] for i in range(n): if a[i] < 0: d.append(a[i]) else: e.append(a[i]) a[:] = d[:] + e[:] return a
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers. (Maintain the order of all -ve and non-negative numbers as given in the original array). Example 1: Input: N = 4 Arr[] = {-3, 3, -2, 2} Output: -3 -2 3 2 Explanation: In the given array, negative numbers are -3, -2 and non-negative numbers are 3, 2. Example 2: Input: N = 4 Arr[] = {-3, 1, 0, -2} Output: -3 -2 1 0 Explanation: In the given array, negative numbers are -3, -2 and non-negative numbers are 1, 0. Your Task: You don't need to read input or print anything. Your task is to complete the function Rearrange() which takes the array Arr[] and its size N as inputs and returns the array after rearranging with spaces between the elements of the array. Expected Time Complexity: O(N. Log(N)) Expected Auxiliary Space: O(Log(N)) Constraints: 1 ≤ N ≤ 10^{5} -10^{9} ≤ Elements of array ≤ 10^{9}
def Rearrange(a, n): negin = 0 posin = 0 while posin < n: if a[posin] < 0: a.insert(negin, a.pop(posin)) posin += 1 negin += 1 else: posin += 1
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers. (Maintain the order of all -ve and non-negative numbers as given in the original array). Example 1: Input: N = 4 Arr[] = {-3, 3, -2, 2} Output: -3 -2 3 2 Explanation: In the given array, negative numbers are -3, -2 and non-negative numbers are 3, 2. Example 2: Input: N = 4 Arr[] = {-3, 1, 0, -2} Output: -3 -2 1 0 Explanation: In the given array, negative numbers are -3, -2 and non-negative numbers are 1, 0. Your Task: You don't need to read input or print anything. Your task is to complete the function Rearrange() which takes the array Arr[] and its size N as inputs and returns the array after rearranging with spaces between the elements of the array. Expected Time Complexity: O(N. Log(N)) Expected Auxiliary Space: O(Log(N)) Constraints: 1 ≤ N ≤ 10^{5} -10^{9} ≤ Elements of array ≤ 10^{9}
def Rearrange(a, n): p = [] ne = [] for i in a: if i < 0: ne += [i] else: p += [i] b = ne + p for i in range(n): a[i] = b[i]
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER VAR LIST VAR VAR LIST VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers. (Maintain the order of all -ve and non-negative numbers as given in the original array). Example 1: Input: N = 4 Arr[] = {-3, 3, -2, 2} Output: -3 -2 3 2 Explanation: In the given array, negative numbers are -3, -2 and non-negative numbers are 3, 2. Example 2: Input: N = 4 Arr[] = {-3, 1, 0, -2} Output: -3 -2 1 0 Explanation: In the given array, negative numbers are -3, -2 and non-negative numbers are 1, 0. Your Task: You don't need to read input or print anything. Your task is to complete the function Rearrange() which takes the array Arr[] and its size N as inputs and returns the array after rearranging with spaces between the elements of the array. Expected Time Complexity: O(N. Log(N)) Expected Auxiliary Space: O(Log(N)) Constraints: 1 ≤ N ≤ 10^{5} -10^{9} ≤ Elements of array ≤ 10^{9}
def Rearrange(a, n): Pos = [] Neg = [] for i in a: if i >= 0: Pos.append(i) else: Neg.append(i) a.clear() a.extend(Neg) a.extend(Pos)
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers. (Maintain the order of all -ve and non-negative numbers as given in the original array). Example 1: Input: N = 4 Arr[] = {-3, 3, -2, 2} Output: -3 -2 3 2 Explanation: In the given array, negative numbers are -3, -2 and non-negative numbers are 3, 2. Example 2: Input: N = 4 Arr[] = {-3, 1, 0, -2} Output: -3 -2 1 0 Explanation: In the given array, negative numbers are -3, -2 and non-negative numbers are 1, 0. Your Task: You don't need to read input or print anything. Your task is to complete the function Rearrange() which takes the array Arr[] and its size N as inputs and returns the array after rearranging with spaces between the elements of the array. Expected Time Complexity: O(N. Log(N)) Expected Auxiliary Space: O(Log(N)) Constraints: 1 ≤ N ≤ 10^{5} -10^{9} ≤ Elements of array ≤ 10^{9}
def Rearrange(a, n): stack = [] for i in range(n - 1, -1, -1): if a[i] >= 0: stack.append(a[i]) k = 0 for i in range(n): if a[i] < 0: a[k] = a[i] k += 1 while len(stack) > 0: a[k] = stack.pop() k += 1 return a
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER RETURN VAR
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers. (Maintain the order of all -ve and non-negative numbers as given in the original array). Example 1: Input: N = 4 Arr[] = {-3, 3, -2, 2} Output: -3 -2 3 2 Explanation: In the given array, negative numbers are -3, -2 and non-negative numbers are 3, 2. Example 2: Input: N = 4 Arr[] = {-3, 1, 0, -2} Output: -3 -2 1 0 Explanation: In the given array, negative numbers are -3, -2 and non-negative numbers are 1, 0. Your Task: You don't need to read input or print anything. Your task is to complete the function Rearrange() which takes the array Arr[] and its size N as inputs and returns the array after rearranging with spaces between the elements of the array. Expected Time Complexity: O(N. Log(N)) Expected Auxiliary Space: O(Log(N)) Constraints: 1 ≤ N ≤ 10^{5} -10^{9} ≤ Elements of array ≤ 10^{9}
def Rearrange(a, n): pos = [] neg = [] for i in a: if i < 0: neg.append(i) else: pos.append(i) a.clear() for i in range(len(neg)): a.append(neg[i]) for i in range(len(pos)): a.append(pos[i]) return a
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers. (Maintain the order of all -ve and non-negative numbers as given in the original array). Example 1: Input: N = 4 Arr[] = {-3, 3, -2, 2} Output: -3 -2 3 2 Explanation: In the given array, negative numbers are -3, -2 and non-negative numbers are 3, 2. Example 2: Input: N = 4 Arr[] = {-3, 1, 0, -2} Output: -3 -2 1 0 Explanation: In the given array, negative numbers are -3, -2 and non-negative numbers are 1, 0. Your Task: You don't need to read input or print anything. Your task is to complete the function Rearrange() which takes the array Arr[] and its size N as inputs and returns the array after rearranging with spaces between the elements of the array. Expected Time Complexity: O(N. Log(N)) Expected Auxiliary Space: O(Log(N)) Constraints: 1 ≤ N ≤ 10^{5} -10^{9} ≤ Elements of array ≤ 10^{9}
def Rearrange(a, n): ne = [] p = [] for i in a: if i < 0: ne.append(i) else: p.append(i) i = 0 m = len(ne) j = 0 while i < n and j < m: a[i] = ne[j] i += 1 j += 1 q = len(p) j = 0 while i < n and j < q: a[i] = p[j] i += 1 j += 1 return a
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers. (Maintain the order of all -ve and non-negative numbers as given in the original array). Example 1: Input: N = 4 Arr[] = {-3, 3, -2, 2} Output: -3 -2 3 2 Explanation: In the given array, negative numbers are -3, -2 and non-negative numbers are 3, 2. Example 2: Input: N = 4 Arr[] = {-3, 1, 0, -2} Output: -3 -2 1 0 Explanation: In the given array, negative numbers are -3, -2 and non-negative numbers are 1, 0. Your Task: You don't need to read input or print anything. Your task is to complete the function Rearrange() which takes the array Arr[] and its size N as inputs and returns the array after rearranging with spaces between the elements of the array. Expected Time Complexity: O(N. Log(N)) Expected Auxiliary Space: O(Log(N)) Constraints: 1 ≤ N ≤ 10^{5} -10^{9} ≤ Elements of array ≤ 10^{9}
def Rearrange(A, n): def merge(L, R): if L == R: if A[L] < 0: return L, L + 1, R + 1 else: return L, L, R + 1 m = (L + R) // 2 Lns, Lne, Lpe = merge(L, m) Rns, Rne, Rpe = merge(m + 1, R) A[Lne:Lpe] = reversed(A[Lne:Lpe]) A[Rns:Rne] = reversed(A[Rns:Rne]) A[Lne:Rne] = reversed(A[Lne:Rne]) return L, L + (Lne - Lns + Rne - Rns), R + 1 merge(0, n - 1) return A
FUNC_DEF FUNC_DEF IF VAR VAR IF VAR VAR NUMBER RETURN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER RETURN VAR
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers. (Maintain the order of all -ve and non-negative numbers as given in the original array). Example 1: Input: N = 4 Arr[] = {-3, 3, -2, 2} Output: -3 -2 3 2 Explanation: In the given array, negative numbers are -3, -2 and non-negative numbers are 3, 2. Example 2: Input: N = 4 Arr[] = {-3, 1, 0, -2} Output: -3 -2 1 0 Explanation: In the given array, negative numbers are -3, -2 and non-negative numbers are 1, 0. Your Task: You don't need to read input or print anything. Your task is to complete the function Rearrange() which takes the array Arr[] and its size N as inputs and returns the array after rearranging with spaces between the elements of the array. Expected Time Complexity: O(N. Log(N)) Expected Auxiliary Space: O(Log(N)) Constraints: 1 ≤ N ≤ 10^{5} -10^{9} ≤ Elements of array ≤ 10^{9}
def Rearrange(arr, n): pos = list() neg = list() for i in range(n): if arr[i] < 0: neg.append(arr[i]) else: pos.append(arr[i]) i = 0 while neg: arr[i] = neg.pop(0) i += 1 while pos: arr[i] = pos.pop(0) i += 1 return arr
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER RETURN VAR
Given an 2D board, count how many battleships are in it. The battleships are represented with 'X's, empty slots are represented with '.'s. You may assume the following rules: You receive a valid board, made of only battleships or empty slots. Battleships can only be placed horizontally or vertically. In other words, they can only be made of the shape 1xN (1 row, N columns) or Nx1 (N rows, 1 column), where N can be of any size. At least one horizontal or vertical cell separates between two battleships - there are no adjacent battleships. Example: X..X ...X ...X In the above board there are 2 battleships. Invalid Example: ...X XXXX ...X This is an invalid board that you will not receive - as battleships will always have a cell separating between them. Follow up:Could you do it in one-pass, using only O(1) extra memory and without modifying the value of the board?
class Solution: def countBattleships(self, board): num = 0 record = [(-1, -1)] colnum = 0 for i in board: rownum = 0 cont = 0 for j in i: if j == "X": if cont == 1: rownum += 1 continue else: ind = 0 for k in record: a, b = k if (a == colnum) & (b == rownum): ind = 1 record.append((a + 1, b)) record.remove((a, b)) break if ind == 0: num = num + 1 record.append((colnum + 1, rownum)) cont = 1 if j != "X": cont = 0 rownum += 1 colnum += 1 return num
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR STRING ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
Given an 2D board, count how many battleships are in it. The battleships are represented with 'X's, empty slots are represented with '.'s. You may assume the following rules: You receive a valid board, made of only battleships or empty slots. Battleships can only be placed horizontally or vertically. In other words, they can only be made of the shape 1xN (1 row, N columns) or Nx1 (N rows, 1 column), where N can be of any size. At least one horizontal or vertical cell separates between two battleships - there are no adjacent battleships. Example: X..X ...X ...X In the above board there are 2 battleships. Invalid Example: ...X XXXX ...X This is an invalid board that you will not receive - as battleships will always have a cell separating between them. Follow up:Could you do it in one-pass, using only O(1) extra memory and without modifying the value of the board?
class Solution: def countBattleships(self, board): ans = 0 for r, row in enumerate(board): for c, val in enumerate(row): if val == "X": ans += 1 if r and board[r - 1][c] == "X": ans -= 1 elif c and board[r][c - 1] == "X": ans -= 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR STRING VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER RETURN VAR
Given an 2D board, count how many battleships are in it. The battleships are represented with 'X's, empty slots are represented with '.'s. You may assume the following rules: You receive a valid board, made of only battleships or empty slots. Battleships can only be placed horizontally or vertically. In other words, they can only be made of the shape 1xN (1 row, N columns) or Nx1 (N rows, 1 column), where N can be of any size. At least one horizontal or vertical cell separates between two battleships - there are no adjacent battleships. Example: X..X ...X ...X In the above board there are 2 battleships. Invalid Example: ...X XXXX ...X This is an invalid board that you will not receive - as battleships will always have a cell separating between them. Follow up:Could you do it in one-pass, using only O(1) extra memory and without modifying the value of the board?
class Solution: def countBattleships(self, board): count = 0 for i in range(len(board)): for j in range(len(board[i])): if board[i][j] == "X": if i - 1 < 0 and j - 1 < 0: count += 1 elif i - 1 < 0 and board[i][j - 1] != "X": count += 1 elif j - 1 < 0 and board[i - 1][j] != "X": count += 1 elif board[i - 1][j] != "X" and board[i][j - 1] != "X": count += 1 return count
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR STRING IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR STRING VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER RETURN VAR
As Sherlock Holmes was investigating a crime, he identified n suspects. He knows for sure that exactly one of them committed the crime. To find out which one did it, the detective lines up the suspects and numbered them from 1 to n. After that, he asked each one: "Which one committed the crime?". Suspect number i answered either "The crime was committed by suspect number ai", or "Suspect number ai didn't commit the crime". Also, the suspect could say so about himself (ai = i). Sherlock Holmes understood for sure that exactly m answers were the truth and all other answers were a lie. Now help him understand this: which suspect lied and which one told the truth? Input The first line contains two integers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ n) — the total number of suspects and the number of suspects who told the truth. Next n lines contain the suspects' answers. The i-th line contains either "+ai" (without the quotes), if the suspect number i says that the crime was committed by suspect number ai, or "-ai" (without the quotes), if the suspect number i says that the suspect number ai didn't commit the crime (ai is an integer, 1 ≤ ai ≤ n). It is guaranteed that at least one suspect exists, such that if he committed the crime, then exactly m people told the truth. Output Print n lines. Line number i should contain "Truth" if suspect number i has told the truth for sure. Print "Lie" if the suspect number i lied for sure and print "Not defined" if he could lie and could tell the truth, too, depending on who committed the crime. Examples Input 1 1 +1 Output Truth Input 3 2 -1 -2 -3 Output Not defined Not defined Not defined Input 4 1 +2 -3 +4 -1 Output Lie Not defined Lie Not defined Note The first sample has the single person and he confesses to the crime, and Sherlock Holmes knows that one person is telling the truth. That means that this person is telling the truth. In the second sample there are three suspects and each one denies his guilt. Sherlock Holmes knows that only two of them are telling the truth. Any one of them can be the criminal, so we don't know for any of them, whether this person is telling the truth or not. In the third sample the second and the fourth suspect defend the first and the third one. But only one is telling the truth, thus, the first or the third one is the criminal. Both of them can be criminals, so the second and the fourth one can either be lying or telling the truth. The first and the third one are lying for sure as they are blaming the second and the fourth one.
import sys input = lambda: sys.stdin.readline().strip() print = lambda s: sys.stdout.write(str(s) + "\n") a, b = map(int, input().split()) r = [0] * a c1 = [0] * (a + 1) c2 = [0] * (a + 1) f = 0 for i in range(a): n = int(input()) r[i] = n if n > 0: c1[n] += 1 else: f += 1 c2[-n] += 1 possible = [False] * (a + 1) np = 0 for i in range(1, a + 1): if c1[i] + f - c2[i] == b: possible[i] = True np += 1 for i in range(a): if r[i] > 0: if possible[r[i]] and np == 1: print("Truth") elif not possible[r[i]]: print("Lie") else: print("Not defined") else: r[i] *= -1 if possible[r[i]] and np == 1: print("Lie") elif not possible[r[i]]: print("Truth") else: print("Not defined")
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
As Sherlock Holmes was investigating a crime, he identified n suspects. He knows for sure that exactly one of them committed the crime. To find out which one did it, the detective lines up the suspects and numbered them from 1 to n. After that, he asked each one: "Which one committed the crime?". Suspect number i answered either "The crime was committed by suspect number ai", or "Suspect number ai didn't commit the crime". Also, the suspect could say so about himself (ai = i). Sherlock Holmes understood for sure that exactly m answers were the truth and all other answers were a lie. Now help him understand this: which suspect lied and which one told the truth? Input The first line contains two integers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ n) — the total number of suspects and the number of suspects who told the truth. Next n lines contain the suspects' answers. The i-th line contains either "+ai" (without the quotes), if the suspect number i says that the crime was committed by suspect number ai, or "-ai" (without the quotes), if the suspect number i says that the suspect number ai didn't commit the crime (ai is an integer, 1 ≤ ai ≤ n). It is guaranteed that at least one suspect exists, such that if he committed the crime, then exactly m people told the truth. Output Print n lines. Line number i should contain "Truth" if suspect number i has told the truth for sure. Print "Lie" if the suspect number i lied for sure and print "Not defined" if he could lie and could tell the truth, too, depending on who committed the crime. Examples Input 1 1 +1 Output Truth Input 3 2 -1 -2 -3 Output Not defined Not defined Not defined Input 4 1 +2 -3 +4 -1 Output Lie Not defined Lie Not defined Note The first sample has the single person and he confesses to the crime, and Sherlock Holmes knows that one person is telling the truth. That means that this person is telling the truth. In the second sample there are three suspects and each one denies his guilt. Sherlock Holmes knows that only two of them are telling the truth. Any one of them can be the criminal, so we don't know for any of them, whether this person is telling the truth or not. In the third sample the second and the fourth suspect defend the first and the third one. But only one is telling the truth, thus, the first or the third one is the criminal. Both of them can be criminals, so the second and the fourth one can either be lying or telling the truth. The first and the third one are lying for sure as they are blaming the second and the fourth one.
import sys n, m = map(int, input().split(" ")) inp = [] guess = [0] * (n + 1) for i in range(n): temp = int(input()) inp.append(temp) if temp < 0: m -= 1 guess[-temp] -= 1 else: guess[temp] += 1 dic = {temp for temp in range(1, n + 1) if guess[temp] == m} if len(dic) == 0: for i in range(n): print("Not defined") elif len(dic) == 1: temp = dic.pop() for i in inp: if i == temp or i < 0 and i + temp: print("Truth") else: print("Lie") else: temp = dic.update({(-i) for i in dic}) for i in inp: if i in dic: print("Not defined") elif i < 0: print("Truth") else: print("Lie")
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
As Sherlock Holmes was investigating a crime, he identified n suspects. He knows for sure that exactly one of them committed the crime. To find out which one did it, the detective lines up the suspects and numbered them from 1 to n. After that, he asked each one: "Which one committed the crime?". Suspect number i answered either "The crime was committed by suspect number ai", or "Suspect number ai didn't commit the crime". Also, the suspect could say so about himself (ai = i). Sherlock Holmes understood for sure that exactly m answers were the truth and all other answers were a lie. Now help him understand this: which suspect lied and which one told the truth? Input The first line contains two integers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ n) — the total number of suspects and the number of suspects who told the truth. Next n lines contain the suspects' answers. The i-th line contains either "+ai" (without the quotes), if the suspect number i says that the crime was committed by suspect number ai, or "-ai" (without the quotes), if the suspect number i says that the suspect number ai didn't commit the crime (ai is an integer, 1 ≤ ai ≤ n). It is guaranteed that at least one suspect exists, such that if he committed the crime, then exactly m people told the truth. Output Print n lines. Line number i should contain "Truth" if suspect number i has told the truth for sure. Print "Lie" if the suspect number i lied for sure and print "Not defined" if he could lie and could tell the truth, too, depending on who committed the crime. Examples Input 1 1 +1 Output Truth Input 3 2 -1 -2 -3 Output Not defined Not defined Not defined Input 4 1 +2 -3 +4 -1 Output Lie Not defined Lie Not defined Note The first sample has the single person and he confesses to the crime, and Sherlock Holmes knows that one person is telling the truth. That means that this person is telling the truth. In the second sample there are three suspects and each one denies his guilt. Sherlock Holmes knows that only two of them are telling the truth. Any one of them can be the criminal, so we don't know for any of them, whether this person is telling the truth or not. In the third sample the second and the fourth suspect defend the first and the third one. But only one is telling the truth, thus, the first or the third one is the criminal. Both of them can be criminals, so the second and the fourth one can either be lying or telling the truth. The first and the third one are lying for sure as they are blaming the second and the fourth one.
n, m = map(int, input().split()) t = [int(input()) for i in range(n)] s, p = 0, [0] * (n + 1) for i in t: if i < 0: m -= 1 p[-i] -= 1 else: p[i] += 1 q = {i for i in range(1, n + 1) if p[i] == m} if len(q) == 0: print("Not defined\n" * n) elif len(q) == 1: j = q.pop() print("\n".join([("Truth" if i == j or i < 0 and i + j else "Lie") for i in t])) else: q.update({(-i) for i in q}) print( "\n".join( [("Not defined" if i in q else "Truth" if i < 0 else "Lie") for i in t] ) )
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR NUMBER BIN_OP VAR VAR STRING STRING VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR STRING VAR NUMBER STRING STRING VAR VAR
As Sherlock Holmes was investigating a crime, he identified n suspects. He knows for sure that exactly one of them committed the crime. To find out which one did it, the detective lines up the suspects and numbered them from 1 to n. After that, he asked each one: "Which one committed the crime?". Suspect number i answered either "The crime was committed by suspect number ai", or "Suspect number ai didn't commit the crime". Also, the suspect could say so about himself (ai = i). Sherlock Holmes understood for sure that exactly m answers were the truth and all other answers were a lie. Now help him understand this: which suspect lied and which one told the truth? Input The first line contains two integers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ n) — the total number of suspects and the number of suspects who told the truth. Next n lines contain the suspects' answers. The i-th line contains either "+ai" (without the quotes), if the suspect number i says that the crime was committed by suspect number ai, or "-ai" (without the quotes), if the suspect number i says that the suspect number ai didn't commit the crime (ai is an integer, 1 ≤ ai ≤ n). It is guaranteed that at least one suspect exists, such that if he committed the crime, then exactly m people told the truth. Output Print n lines. Line number i should contain "Truth" if suspect number i has told the truth for sure. Print "Lie" if the suspect number i lied for sure and print "Not defined" if he could lie and could tell the truth, too, depending on who committed the crime. Examples Input 1 1 +1 Output Truth Input 3 2 -1 -2 -3 Output Not defined Not defined Not defined Input 4 1 +2 -3 +4 -1 Output Lie Not defined Lie Not defined Note The first sample has the single person and he confesses to the crime, and Sherlock Holmes knows that one person is telling the truth. That means that this person is telling the truth. In the second sample there are three suspects and each one denies his guilt. Sherlock Holmes knows that only two of them are telling the truth. Any one of them can be the criminal, so we don't know for any of them, whether this person is telling the truth or not. In the third sample the second and the fourth suspect defend the first and the third one. But only one is telling the truth, thus, the first or the third one is the criminal. Both of them can be criminals, so the second and the fourth one can either be lying or telling the truth. The first and the third one are lying for sure as they are blaming the second and the fourth one.
n, m = list(map(int, input().split())) a = [[0, 0] for i in range(n)] b = [] e = 0 f = 0 for i in range(n): c = input() d = int(c[1:]) if c[0] == "+": a[d - 1][0] += 1 e += 1 else: a[d - 1][1] += 1 f += 1 b.append([c[0], d]) g = [(a[i][0] + f - a[i][1]) for i in range(n)] h = g.count(m) for i in range(n): d = b[i][1] if b[i][0] == "+": if g[d - 1] == m: if h > 1: print("Not defined") else: print("Truth") else: print("Lie") elif h > 1 or h == 1 and g[d - 1] != m: if g[d - 1] == m: print("Not defined") else: print("Truth") else: print("Lie")
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER STRING VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER STRING IF VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
As Sherlock Holmes was investigating a crime, he identified n suspects. He knows for sure that exactly one of them committed the crime. To find out which one did it, the detective lines up the suspects and numbered them from 1 to n. After that, he asked each one: "Which one committed the crime?". Suspect number i answered either "The crime was committed by suspect number ai", or "Suspect number ai didn't commit the crime". Also, the suspect could say so about himself (ai = i). Sherlock Holmes understood for sure that exactly m answers were the truth and all other answers were a lie. Now help him understand this: which suspect lied and which one told the truth? Input The first line contains two integers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ n) — the total number of suspects and the number of suspects who told the truth. Next n lines contain the suspects' answers. The i-th line contains either "+ai" (without the quotes), if the suspect number i says that the crime was committed by suspect number ai, or "-ai" (without the quotes), if the suspect number i says that the suspect number ai didn't commit the crime (ai is an integer, 1 ≤ ai ≤ n). It is guaranteed that at least one suspect exists, such that if he committed the crime, then exactly m people told the truth. Output Print n lines. Line number i should contain "Truth" if suspect number i has told the truth for sure. Print "Lie" if the suspect number i lied for sure and print "Not defined" if he could lie and could tell the truth, too, depending on who committed the crime. Examples Input 1 1 +1 Output Truth Input 3 2 -1 -2 -3 Output Not defined Not defined Not defined Input 4 1 +2 -3 +4 -1 Output Lie Not defined Lie Not defined Note The first sample has the single person and he confesses to the crime, and Sherlock Holmes knows that one person is telling the truth. That means that this person is telling the truth. In the second sample there are three suspects and each one denies his guilt. Sherlock Holmes knows that only two of them are telling the truth. Any one of them can be the criminal, so we don't know for any of them, whether this person is telling the truth or not. In the third sample the second and the fourth suspect defend the first and the third one. But only one is telling the truth, thus, the first or the third one is the criminal. Both of them can be criminals, so the second and the fourth one can either be lying or telling the truth. The first and the third one are lying for sure as they are blaming the second and the fourth one.
from sys import stdin, stdout n, m = map(int, stdin.readline().split()) cntf = [(0) for i in range(n + 1)] cnts = [(0) for i in range(n + 1)] challengers = [] for i in range(n): s = stdin.readline().strip() challengers.append(s) if s[0] == "+": cntf[int(s[1:])] += 1 else: cnts[int(s[1:])] += 1 first = sum(cntf) second = sum(cnts) ans = set() for i in range(1, n + 1): if cntf[i] + second - cnts[i] == m: ans.add(i) for i in range(n): s = challengers[i] if s[0] == "+": if int(s[1:]) in ans: if len(ans) > 1: stdout.write("Not defined\n") else: stdout.write("Truth\n") else: stdout.write("Lie\n") elif int(s[1:]) in ans: if len(ans) > 1: stdout.write("Not defined\n") else: stdout.write("Lie\n") else: stdout.write("Truth\n")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER STRING VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER STRING IF FUNC_CALL VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has a string $S$. He also has another string $P$, called *pattern*. He wants to find the pattern in $S$, but that might be impossible. Therefore, he is willing to reorder the characters of $S$ in such a way that $P$ occurs in the resulting string (an anagram of $S$) as a substring. Since this problem was too hard for Chef, he decided to ask you, his genius friend, for help. Can you find the lexicographically smallest anagram of $S$ that contains $P$ as substring? Note: A string $B$ is a substring of a string $A$ if $B$ can be obtained from $A$ by deleting several (possibly none or all) characters from the beginning and several (possibly none or all) characters from the end. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single string $S$. The second line contains a single string $P$. ------ Output ------ For each test case, print a single line containing one string ― the smallest anagram of $S$ that contains $P$. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ |P| ≤ |S| ≤ 10^{5}$ $S$ and $P$ contain only lowercase English letters there is at least one anagram of $S$ that contains $P$ ------ Subtasks ------ Subtask #1 (20 points): $|S| ≤ 1,000$ Subtask #2 (80 points): $|S| ≤ 10^{5}$ ----- Sample Input 1 ------ 3 akramkeeanany aka supahotboy bohoty daehabshatorawy badawy ----- Sample Output 1 ------ aaakaeekmnnry abohotypsu aabadawyehhorst
for _ in range(int(input())): s = input() p = input() c = {} d = {} for i in range(97, 124): c[chr(i)] = 0 d[chr(i)] = 0 l = sorted(list(s)) m = sorted(list(p)) for i in l: c[i] += 1 for i in m: d[i] += 1 for i in d: c[i] -= d[i] ans1 = "" ans2 = "" for i in c: if i < p[0]: ans1 += i * c[i] ans1 += p for i in c: if i >= p[0]: ans1 += i * c[i] for i in c: if i <= p[0]: ans2 += i * c[i] ans2 += p for i in c: if i > p[0]: ans2 += i * c[i] print(min(ans1, ans2))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR VAR VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR FOR VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR VAR VAR FOR VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR FOR VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has a string $S$. He also has another string $P$, called *pattern*. He wants to find the pattern in $S$, but that might be impossible. Therefore, he is willing to reorder the characters of $S$ in such a way that $P$ occurs in the resulting string (an anagram of $S$) as a substring. Since this problem was too hard for Chef, he decided to ask you, his genius friend, for help. Can you find the lexicographically smallest anagram of $S$ that contains $P$ as substring? Note: A string $B$ is a substring of a string $A$ if $B$ can be obtained from $A$ by deleting several (possibly none or all) characters from the beginning and several (possibly none or all) characters from the end. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single string $S$. The second line contains a single string $P$. ------ Output ------ For each test case, print a single line containing one string ― the smallest anagram of $S$ that contains $P$. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ |P| ≤ |S| ≤ 10^{5}$ $S$ and $P$ contain only lowercase English letters there is at least one anagram of $S$ that contains $P$ ------ Subtasks ------ Subtask #1 (20 points): $|S| ≤ 1,000$ Subtask #2 (80 points): $|S| ≤ 10^{5}$ ----- Sample Input 1 ------ 3 akramkeeanany aka supahotboy bohoty daehabshatorawy badawy ----- Sample Output 1 ------ aaakaeekmnnry abohotypsu aabadawyehhorst
for _ in range(int(input())): s = input() p = input() u = [p[0]] for e in p[1:]: if e not in u: u.append(e) s = s + p[0] s = "".join(sorted(s)) for e in p: s = s.replace(e, "", 1) pos = 0 if u[0] < u[1]: pos = s.rfind(p[0]) else: pos = s.find(p[0]) print(s[:pos] + p + s[pos + 1 :])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR STRING NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has a string $S$. He also has another string $P$, called *pattern*. He wants to find the pattern in $S$, but that might be impossible. Therefore, he is willing to reorder the characters of $S$ in such a way that $P$ occurs in the resulting string (an anagram of $S$) as a substring. Since this problem was too hard for Chef, he decided to ask you, his genius friend, for help. Can you find the lexicographically smallest anagram of $S$ that contains $P$ as substring? Note: A string $B$ is a substring of a string $A$ if $B$ can be obtained from $A$ by deleting several (possibly none or all) characters from the beginning and several (possibly none or all) characters from the end. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single string $S$. The second line contains a single string $P$. ------ Output ------ For each test case, print a single line containing one string ― the smallest anagram of $S$ that contains $P$. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ |P| ≤ |S| ≤ 10^{5}$ $S$ and $P$ contain only lowercase English letters there is at least one anagram of $S$ that contains $P$ ------ Subtasks ------ Subtask #1 (20 points): $|S| ≤ 1,000$ Subtask #2 (80 points): $|S| ≤ 10^{5}$ ----- Sample Input 1 ------ 3 akramkeeanany aka supahotboy bohoty daehabshatorawy badawy ----- Sample Output 1 ------ aaakaeekmnnry abohotypsu aabadawyehhorst
testcase = int(input()) for _ in range(testcase): string = input() pattern = input() result = "" cnt = [0] * 26 for c in string: cnt[ord(c) - ord("a")] += 1 for c in pattern: cnt[ord(c) - ord("a")] -= 1 flag = 1 for i in range(1, len(pattern)): if pattern[i] == pattern[i - 1]: continue if pattern[i] < pattern[i - 1]: flag = 0 break index = ord(pattern[0]) - ord("a") + flag for i in range(0, index): while cnt[i] > 0: result += chr(i + ord("a")) cnt[i] -= 1 result += pattern for i in range(index, 26): while cnt[i] > 0: result += chr(i + ord("a")) cnt[i] -= 1 print(result)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR NUMBER VAR WHILE VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has a string $S$. He also has another string $P$, called *pattern*. He wants to find the pattern in $S$, but that might be impossible. Therefore, he is willing to reorder the characters of $S$ in such a way that $P$ occurs in the resulting string (an anagram of $S$) as a substring. Since this problem was too hard for Chef, he decided to ask you, his genius friend, for help. Can you find the lexicographically smallest anagram of $S$ that contains $P$ as substring? Note: A string $B$ is a substring of a string $A$ if $B$ can be obtained from $A$ by deleting several (possibly none or all) characters from the beginning and several (possibly none or all) characters from the end. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single string $S$. The second line contains a single string $P$. ------ Output ------ For each test case, print a single line containing one string ― the smallest anagram of $S$ that contains $P$. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ |P| ≤ |S| ≤ 10^{5}$ $S$ and $P$ contain only lowercase English letters there is at least one anagram of $S$ that contains $P$ ------ Subtasks ------ Subtask #1 (20 points): $|S| ≤ 1,000$ Subtask #2 (80 points): $|S| ≤ 10^{5}$ ----- Sample Input 1 ------ 3 akramkeeanany aka supahotboy bohoty daehabshatorawy badawy ----- Sample Output 1 ------ aaakaeekmnnry abohotypsu aabadawyehhorst
t = int(input()) for _ in range(t): a = input() b = input() n = len(a) m = len(b) i = 0 flag = True while i + 1 < m and b[i + 1] == b[i]: i += 1 if i < m - 1 and ord(b[i]) < ord(b[i + 1]): flag = False c = [] for i in range(0, 26): c.append(0) for i in range(0, n): c[ord(a[i]) - ord("a")] += 1 for i in range(0, m): c[ord(b[i]) - ord("a")] -= 1 ans = "" for i in range(0, 26): if i + ord("a") != ord(b[0]): ans += chr(i + ord("a")) * c[i] elif flag: ans += b ans += chr(i + ord("a")) * c[i] else: ans += chr(i + ord("a")) * c[i] ans += b print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR FUNC_CALL VAR STRING FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING VAR VAR IF VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has a string $S$. He also has another string $P$, called *pattern*. He wants to find the pattern in $S$, but that might be impossible. Therefore, he is willing to reorder the characters of $S$ in such a way that $P$ occurs in the resulting string (an anagram of $S$) as a substring. Since this problem was too hard for Chef, he decided to ask you, his genius friend, for help. Can you find the lexicographically smallest anagram of $S$ that contains $P$ as substring? Note: A string $B$ is a substring of a string $A$ if $B$ can be obtained from $A$ by deleting several (possibly none or all) characters from the beginning and several (possibly none or all) characters from the end. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single string $S$. The second line contains a single string $P$. ------ Output ------ For each test case, print a single line containing one string ― the smallest anagram of $S$ that contains $P$. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ |P| ≤ |S| ≤ 10^{5}$ $S$ and $P$ contain only lowercase English letters there is at least one anagram of $S$ that contains $P$ ------ Subtasks ------ Subtask #1 (20 points): $|S| ≤ 1,000$ Subtask #2 (80 points): $|S| ≤ 10^{5}$ ----- Sample Input 1 ------ 3 akramkeeanany aka supahotboy bohoty daehabshatorawy badawy ----- Sample Output 1 ------ aaakaeekmnnry abohotypsu aabadawyehhorst
t = int(input()) for _ in range(t): s = list(input()) p = list(input()) freq = {} for c in s: freq[c] = freq.get(c, 0) + 1 for c in p: freq[c] -= 1 flag = 0 for i in range(len(p) - 1): if p[i] == p[i + 1]: continue if p[i] > p[i + 1]: flag = 1 break res = [] for key, value in sorted(freq.items(), key=lambda x: x[0]): if key < p[0] or key > p[0]: res.append(key * value) elif key == p[0]: if flag == 0: res.append(key * value) res.append("".join(p)) else: res.append("".join(p)) res.append(key * value) print("".join(res))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has a string $S$. He also has another string $P$, called *pattern*. He wants to find the pattern in $S$, but that might be impossible. Therefore, he is willing to reorder the characters of $S$ in such a way that $P$ occurs in the resulting string (an anagram of $S$) as a substring. Since this problem was too hard for Chef, he decided to ask you, his genius friend, for help. Can you find the lexicographically smallest anagram of $S$ that contains $P$ as substring? Note: A string $B$ is a substring of a string $A$ if $B$ can be obtained from $A$ by deleting several (possibly none or all) characters from the beginning and several (possibly none or all) characters from the end. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single string $S$. The second line contains a single string $P$. ------ Output ------ For each test case, print a single line containing one string ― the smallest anagram of $S$ that contains $P$. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ |P| ≤ |S| ≤ 10^{5}$ $S$ and $P$ contain only lowercase English letters there is at least one anagram of $S$ that contains $P$ ------ Subtasks ------ Subtask #1 (20 points): $|S| ≤ 1,000$ Subtask #2 (80 points): $|S| ≤ 10^{5}$ ----- Sample Input 1 ------ 3 akramkeeanany aka supahotboy bohoty daehabshatorawy badawy ----- Sample Output 1 ------ aaakaeekmnnry abohotypsu aabadawyehhorst
try: for _ in range(int(input())): n = input() t = input() a = [(0) for i in range(26)] for i in n: a[ord(i) - 97] += 1 for i in t: a[ord(i) - 97] -= 1 last = ord(t[0]) - 97 s1 = "" s2 = "" for i in range(26): if last == i: s1 += chr(97 + i) * a[i] s1 += t s2 += t s2 += chr(97 + i) * a[i] else: s1 += chr(97 + i) * a[i] s2 += chr(97 + i) * a[i] print(min(s1, s2)) except Exception: pass
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has a string $S$. He also has another string $P$, called *pattern*. He wants to find the pattern in $S$, but that might be impossible. Therefore, he is willing to reorder the characters of $S$ in such a way that $P$ occurs in the resulting string (an anagram of $S$) as a substring. Since this problem was too hard for Chef, he decided to ask you, his genius friend, for help. Can you find the lexicographically smallest anagram of $S$ that contains $P$ as substring? Note: A string $B$ is a substring of a string $A$ if $B$ can be obtained from $A$ by deleting several (possibly none or all) characters from the beginning and several (possibly none or all) characters from the end. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single string $S$. The second line contains a single string $P$. ------ Output ------ For each test case, print a single line containing one string ― the smallest anagram of $S$ that contains $P$. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ |P| ≤ |S| ≤ 10^{5}$ $S$ and $P$ contain only lowercase English letters there is at least one anagram of $S$ that contains $P$ ------ Subtasks ------ Subtask #1 (20 points): $|S| ≤ 1,000$ Subtask #2 (80 points): $|S| ≤ 10^{5}$ ----- Sample Input 1 ------ 3 akramkeeanany aka supahotboy bohoty daehabshatorawy badawy ----- Sample Output 1 ------ aaakaeekmnnry abohotypsu aabadawyehhorst
for _ in range(int(input())): a = list(input()) b = list(input()) for i in b: a.remove(i) a.sort() left = [] right = [] equal = [] k = len(a) l = 0 for i in range(1, len(b)): if ord(b[i]) > ord(b[0]): l = 0 break if ord(b[i]) < ord(b[0]): l = 1 break for i in range(len(a)): if ord(a[i]) > ord(b[0]): k = i break if ord(a[i]) == ord(b[0]): equal.append(b[0]) elif ord(a[i]) < ord(b[0]): left.append(a[i]) right = a[k:] if l: print("".join(left) + "".join(b) + "".join(equal) + "".join(right)) else: print("".join(left) + "".join(equal) + "".join(b) + "".join(right))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL STRING VAR FUNC_CALL STRING VAR FUNC_CALL STRING VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL STRING VAR FUNC_CALL STRING VAR FUNC_CALL STRING VAR FUNC_CALL STRING VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has a string $S$. He also has another string $P$, called *pattern*. He wants to find the pattern in $S$, but that might be impossible. Therefore, he is willing to reorder the characters of $S$ in such a way that $P$ occurs in the resulting string (an anagram of $S$) as a substring. Since this problem was too hard for Chef, he decided to ask you, his genius friend, for help. Can you find the lexicographically smallest anagram of $S$ that contains $P$ as substring? Note: A string $B$ is a substring of a string $A$ if $B$ can be obtained from $A$ by deleting several (possibly none or all) characters from the beginning and several (possibly none or all) characters from the end. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single string $S$. The second line contains a single string $P$. ------ Output ------ For each test case, print a single line containing one string ― the smallest anagram of $S$ that contains $P$. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ |P| ≤ |S| ≤ 10^{5}$ $S$ and $P$ contain only lowercase English letters there is at least one anagram of $S$ that contains $P$ ------ Subtasks ------ Subtask #1 (20 points): $|S| ≤ 1,000$ Subtask #2 (80 points): $|S| ≤ 10^{5}$ ----- Sample Input 1 ------ 3 akramkeeanany aka supahotboy bohoty daehabshatorawy badawy ----- Sample Output 1 ------ aaakaeekmnnry abohotypsu aabadawyehhorst
for _ in range(int(input())): s = list(str(input())) p = list(str(input())) s_26 = [0] * 26 p_26 = [0] * 26 to_add = [0] * 26 for i in s: s_26[ord(i) - 97] += 1 for i in p: p_26[ord(i) - 97] += 1 for i in range(26): to_add[i] = s_26[i] - p_26[i] ans = [] for i in range(26): if i <= ord(p[0]) - 97: ans.append(chr(i + 97) * to_add[i]) ans += p for i in range(26): if i > ord(p[0]) - 97: ans.append(chr(i + 97) * to_add[i]) ans = "".join(i for i in ans) ans1 = [] for i in range(26): if i < ord(p[0]) - 97: ans1.append(chr(i + 97) * to_add[i]) ans1 += p for i in range(26): if i >= ord(p[0]) - 97: ans1.append(chr(i + 97) * to_add[i]) ans1 = "".join(i for i in ans1) print(min(ans, ans1))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL STRING VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL STRING VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has a string $S$. He also has another string $P$, called *pattern*. He wants to find the pattern in $S$, but that might be impossible. Therefore, he is willing to reorder the characters of $S$ in such a way that $P$ occurs in the resulting string (an anagram of $S$) as a substring. Since this problem was too hard for Chef, he decided to ask you, his genius friend, for help. Can you find the lexicographically smallest anagram of $S$ that contains $P$ as substring? Note: A string $B$ is a substring of a string $A$ if $B$ can be obtained from $A$ by deleting several (possibly none or all) characters from the beginning and several (possibly none or all) characters from the end. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single string $S$. The second line contains a single string $P$. ------ Output ------ For each test case, print a single line containing one string ― the smallest anagram of $S$ that contains $P$. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ |P| ≤ |S| ≤ 10^{5}$ $S$ and $P$ contain only lowercase English letters there is at least one anagram of $S$ that contains $P$ ------ Subtasks ------ Subtask #1 (20 points): $|S| ≤ 1,000$ Subtask #2 (80 points): $|S| ≤ 10^{5}$ ----- Sample Input 1 ------ 3 akramkeeanany aka supahotboy bohoty daehabshatorawy badawy ----- Sample Output 1 ------ aaakaeekmnnry abohotypsu aabadawyehhorst
for _ in range(int(input())): s1 = input() s2 = input() l = [0] * 26 for i in s1: l[ord(i) - ord("a")] += 1 for i in s2: l[ord(i) - ord("a")] -= 1 k, s, s1 = ord(s2[0]) - ord("a"), "", "" for i in range(26): if i == k: s += chr(ord("a") + i) * l[i] s += s2 s1 += s2 s1 += chr(ord("a") + i) * l[i] else: s += chr(ord("a") + i) * l[i] s1 += chr(ord("a") + i) * l[i] print(min(s, s1))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING STRING STRING FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has a string $S$. He also has another string $P$, called *pattern*. He wants to find the pattern in $S$, but that might be impossible. Therefore, he is willing to reorder the characters of $S$ in such a way that $P$ occurs in the resulting string (an anagram of $S$) as a substring. Since this problem was too hard for Chef, he decided to ask you, his genius friend, for help. Can you find the lexicographically smallest anagram of $S$ that contains $P$ as substring? Note: A string $B$ is a substring of a string $A$ if $B$ can be obtained from $A$ by deleting several (possibly none or all) characters from the beginning and several (possibly none or all) characters from the end. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single string $S$. The second line contains a single string $P$. ------ Output ------ For each test case, print a single line containing one string ― the smallest anagram of $S$ that contains $P$. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ |P| ≤ |S| ≤ 10^{5}$ $S$ and $P$ contain only lowercase English letters there is at least one anagram of $S$ that contains $P$ ------ Subtasks ------ Subtask #1 (20 points): $|S| ≤ 1,000$ Subtask #2 (80 points): $|S| ≤ 10^{5}$ ----- Sample Input 1 ------ 3 akramkeeanany aka supahotboy bohoty daehabshatorawy badawy ----- Sample Output 1 ------ aaakaeekmnnry abohotypsu aabadawyehhorst
t = int(input()) while t != 0: s = input() p = input() x = len(s) y = len(p) a = [0] * 26 b = [0] * 26 for i in range(x): pointer = ord(s[i]) - 97 a[pointer] += 1 for i in range(y): pointer = ord(p[i]) - 97 b[pointer] += 1 for i in range(26): if i + 97 == ord(p[0]): x = p[0] c_x = a[i] - b[i] temp_p1 = p for s in range(c_x): temp_p1 += x temp_p2 = "" for s in range(c_x): temp_p2 += x temp_p2 += p if temp_p2 > temp_p1: check = 1 else: check = 0 if check == 1: for j in range(26): a[j] = a[j] - b[j] b[j] = 0 print(p, end="") times = a[i] - b[i] while times: print(chr(i + 97), end="") times -= 1 else: times = a[i] - b[i] while times: print(chr(i + 97), end="") times -= 1 for j in range(26): a[j] = a[j] - b[j] b[j] = 0 print(p, end="") else: times = a[i] - b[i] while times: print(chr(i + 97), end="") times -= 1 t -= 1 print(end="\n")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR VAR WHILE VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER STRING VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR WHILE VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER STRING VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR VAR WHILE VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has a string $S$. He also has another string $P$, called *pattern*. He wants to find the pattern in $S$, but that might be impossible. Therefore, he is willing to reorder the characters of $S$ in such a way that $P$ occurs in the resulting string (an anagram of $S$) as a substring. Since this problem was too hard for Chef, he decided to ask you, his genius friend, for help. Can you find the lexicographically smallest anagram of $S$ that contains $P$ as substring? Note: A string $B$ is a substring of a string $A$ if $B$ can be obtained from $A$ by deleting several (possibly none or all) characters from the beginning and several (possibly none or all) characters from the end. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single string $S$. The second line contains a single string $P$. ------ Output ------ For each test case, print a single line containing one string ― the smallest anagram of $S$ that contains $P$. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ |P| ≤ |S| ≤ 10^{5}$ $S$ and $P$ contain only lowercase English letters there is at least one anagram of $S$ that contains $P$ ------ Subtasks ------ Subtask #1 (20 points): $|S| ≤ 1,000$ Subtask #2 (80 points): $|S| ≤ 10^{5}$ ----- Sample Input 1 ------ 3 akramkeeanany aka supahotboy bohoty daehabshatorawy badawy ----- Sample Output 1 ------ aaakaeekmnnry abohotypsu aabadawyehhorst
for _ in range(int(input())): s = str(input()) p = str(input()) if len(s) == len(p): print(p) else: res = "" flag = 1 for i in range(1, len(p)): if ord(p[i]) == ord(p[i - 1]): continue if ord(p[i]) < ord(p[i - 1]): flag = 0 break ordP1 = ord(p[0]) - ord("a") + flag scount = [0] * 26 for c in s: scount[ord(c) - ord("a")] += 1 for c in p: scount[ord(c) - ord("a")] -= 1 for i in range(ordP1): while scount[i] > 0: res += chr(i + ord("a")) scount[i] -= 1 res += p for i in range(ordP1, 26): while scount[i] > 0: res += chr(i + ord("a")) scount[i] -= 1 print(res)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has a string $S$. He also has another string $P$, called *pattern*. He wants to find the pattern in $S$, but that might be impossible. Therefore, he is willing to reorder the characters of $S$ in such a way that $P$ occurs in the resulting string (an anagram of $S$) as a substring. Since this problem was too hard for Chef, he decided to ask you, his genius friend, for help. Can you find the lexicographically smallest anagram of $S$ that contains $P$ as substring? Note: A string $B$ is a substring of a string $A$ if $B$ can be obtained from $A$ by deleting several (possibly none or all) characters from the beginning and several (possibly none or all) characters from the end. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single string $S$. The second line contains a single string $P$. ------ Output ------ For each test case, print a single line containing one string ― the smallest anagram of $S$ that contains $P$. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ |P| ≤ |S| ≤ 10^{5}$ $S$ and $P$ contain only lowercase English letters there is at least one anagram of $S$ that contains $P$ ------ Subtasks ------ Subtask #1 (20 points): $|S| ≤ 1,000$ Subtask #2 (80 points): $|S| ≤ 10^{5}$ ----- Sample Input 1 ------ 3 akramkeeanany aka supahotboy bohoty daehabshatorawy badawy ----- Sample Output 1 ------ aaakaeekmnnry abohotypsu aabadawyehhorst
t = int(input()) for i in range(t): s = input() p = input() s1 = list(s) front = [] middle = "" last = [] for i in p: if i in s1: s1.remove(i) for i in range(len(s1)): if s1[i] < p[0]: front.append(s1[i]) elif s1[i] == p[0]: middle += s1[i] else: last.append(s1[i]) front.sort() front = "".join(map(str, front)) last.sort() last = "".join(map(str, last)) print(min(front + middle + p + last, front + p + middle + last))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has a string $S$. He also has another string $P$, called *pattern*. He wants to find the pattern in $S$, but that might be impossible. Therefore, he is willing to reorder the characters of $S$ in such a way that $P$ occurs in the resulting string (an anagram of $S$) as a substring. Since this problem was too hard for Chef, he decided to ask you, his genius friend, for help. Can you find the lexicographically smallest anagram of $S$ that contains $P$ as substring? Note: A string $B$ is a substring of a string $A$ if $B$ can be obtained from $A$ by deleting several (possibly none or all) characters from the beginning and several (possibly none or all) characters from the end. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single string $S$. The second line contains a single string $P$. ------ Output ------ For each test case, print a single line containing one string ― the smallest anagram of $S$ that contains $P$. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ |P| ≤ |S| ≤ 10^{5}$ $S$ and $P$ contain only lowercase English letters there is at least one anagram of $S$ that contains $P$ ------ Subtasks ------ Subtask #1 (20 points): $|S| ≤ 1,000$ Subtask #2 (80 points): $|S| ≤ 10^{5}$ ----- Sample Input 1 ------ 3 akramkeeanany aka supahotboy bohoty daehabshatorawy badawy ----- Sample Output 1 ------ aaakaeekmnnry abohotypsu aabadawyehhorst
for _ in range(int(input())): s = input() p = input() i = 0 d = {} for l in s: if l in d: d[l] = d[l] + 1 else: d[l] = 1 for k in p: d[k] = d[k] - 1 res = "" res1 = "" t = p[0] d = {k: v for k, v in sorted(d.items(), key=lambda item: item[0])} for k, v in d.items(): if k == t: res1 += p res1 += k * v res += k * v res += p else: res += k * v res1 += k * v print(min(res, res1))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has a string $S$. He also has another string $P$, called *pattern*. He wants to find the pattern in $S$, but that might be impossible. Therefore, he is willing to reorder the characters of $S$ in such a way that $P$ occurs in the resulting string (an anagram of $S$) as a substring. Since this problem was too hard for Chef, he decided to ask you, his genius friend, for help. Can you find the lexicographically smallest anagram of $S$ that contains $P$ as substring? Note: A string $B$ is a substring of a string $A$ if $B$ can be obtained from $A$ by deleting several (possibly none or all) characters from the beginning and several (possibly none or all) characters from the end. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single string $S$. The second line contains a single string $P$. ------ Output ------ For each test case, print a single line containing one string ― the smallest anagram of $S$ that contains $P$. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ |P| ≤ |S| ≤ 10^{5}$ $S$ and $P$ contain only lowercase English letters there is at least one anagram of $S$ that contains $P$ ------ Subtasks ------ Subtask #1 (20 points): $|S| ≤ 1,000$ Subtask #2 (80 points): $|S| ≤ 10^{5}$ ----- Sample Input 1 ------ 3 akramkeeanany aka supahotboy bohoty daehabshatorawy badawy ----- Sample Output 1 ------ aaakaeekmnnry abohotypsu aabadawyehhorst
try: t = int(input()) for _ in range(t): s1 = input() p1 = input() s = list(s1) p = list(p1) for i in range(len(p)): if p[i] in s: s.remove(p[i]) first = "" middle = "" last = "" for i in range(len(s)): if s[i] < p1[0]: first += s[i] elif s[i] == p1[0]: middle += s[i] elif s[i] > p1[0]: last += s[i] first = "".join(sorted(first)) last = "".join(sorted(last)) print(min(first + middle + p1 + last, first + p1 + middle + last)) except: pass
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has a string $S$. He also has another string $P$, called *pattern*. He wants to find the pattern in $S$, but that might be impossible. Therefore, he is willing to reorder the characters of $S$ in such a way that $P$ occurs in the resulting string (an anagram of $S$) as a substring. Since this problem was too hard for Chef, he decided to ask you, his genius friend, for help. Can you find the lexicographically smallest anagram of $S$ that contains $P$ as substring? Note: A string $B$ is a substring of a string $A$ if $B$ can be obtained from $A$ by deleting several (possibly none or all) characters from the beginning and several (possibly none or all) characters from the end. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single string $S$. The second line contains a single string $P$. ------ Output ------ For each test case, print a single line containing one string ― the smallest anagram of $S$ that contains $P$. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ |P| ≤ |S| ≤ 10^{5}$ $S$ and $P$ contain only lowercase English letters there is at least one anagram of $S$ that contains $P$ ------ Subtasks ------ Subtask #1 (20 points): $|S| ≤ 1,000$ Subtask #2 (80 points): $|S| ≤ 10^{5}$ ----- Sample Input 1 ------ 3 akramkeeanany aka supahotboy bohoty daehabshatorawy badawy ----- Sample Output 1 ------ aaakaeekmnnry abohotypsu aabadawyehhorst
for _ in range(int(input())): a = input() b = input() a_small = "" a_big = "" a_small1 = "" a_big1 = "" a = list(a) b = list(b) for i in range(len(b)): a.remove(b[i]) a = "".join(a) b = "".join(b) for i in range(len(a)): if ord(a[i]) <= ord(b[0]): a_small += a[i] elif a[i]: a_big += a[i] ans = "".join(sorted(a_small)) + b + "".join(sorted(a_big)) for i in range(len(a)): if ord(a[i]) < ord(b[0]): a_small1 += a[i] elif a[i]: a_big1 += a[i] ans1 = "".join(sorted(a_small1)) + b + "".join(sorted(a_big1)) print(min(ans, ans1))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_CALL STRING FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_CALL STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has a string $S$. He also has another string $P$, called *pattern*. He wants to find the pattern in $S$, but that might be impossible. Therefore, he is willing to reorder the characters of $S$ in such a way that $P$ occurs in the resulting string (an anagram of $S$) as a substring. Since this problem was too hard for Chef, he decided to ask you, his genius friend, for help. Can you find the lexicographically smallest anagram of $S$ that contains $P$ as substring? Note: A string $B$ is a substring of a string $A$ if $B$ can be obtained from $A$ by deleting several (possibly none or all) characters from the beginning and several (possibly none or all) characters from the end. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single string $S$. The second line contains a single string $P$. ------ Output ------ For each test case, print a single line containing one string ― the smallest anagram of $S$ that contains $P$. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ |P| ≤ |S| ≤ 10^{5}$ $S$ and $P$ contain only lowercase English letters there is at least one anagram of $S$ that contains $P$ ------ Subtasks ------ Subtask #1 (20 points): $|S| ≤ 1,000$ Subtask #2 (80 points): $|S| ≤ 10^{5}$ ----- Sample Input 1 ------ 3 akramkeeanany aka supahotboy bohoty daehabshatorawy badawy ----- Sample Output 1 ------ aaakaeekmnnry abohotypsu aabadawyehhorst
for i in range(int(input())): S = input() P = input() s = list(S) p = list(P) for i in p: s.remove(i) s.sort() if p[0] not in s: r = s[:] r.append(p[0]) r.sort() n = r.index(p[0]) u = s[:n] + p s = s[n:] print(*(u + s), sep="") else: r = s[:] r.append(p[0]) y = s.count(p[0]) r.sort() n = r.index(p[0]) u = "".join(s[: n + y]) + "".join(p) + "".join(s[y + n :]) y = s.index(p[0]) q = "".join(s[:y]) + "".join(p) + "".join(s[y:]) print(min(u, q))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL STRING VAR BIN_OP VAR VAR FUNC_CALL STRING VAR FUNC_CALL STRING VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL STRING VAR VAR FUNC_CALL STRING VAR FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has a string $S$. He also has another string $P$, called *pattern*. He wants to find the pattern in $S$, but that might be impossible. Therefore, he is willing to reorder the characters of $S$ in such a way that $P$ occurs in the resulting string (an anagram of $S$) as a substring. Since this problem was too hard for Chef, he decided to ask you, his genius friend, for help. Can you find the lexicographically smallest anagram of $S$ that contains $P$ as substring? Note: A string $B$ is a substring of a string $A$ if $B$ can be obtained from $A$ by deleting several (possibly none or all) characters from the beginning and several (possibly none or all) characters from the end. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single string $S$. The second line contains a single string $P$. ------ Output ------ For each test case, print a single line containing one string ― the smallest anagram of $S$ that contains $P$. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ |P| ≤ |S| ≤ 10^{5}$ $S$ and $P$ contain only lowercase English letters there is at least one anagram of $S$ that contains $P$ ------ Subtasks ------ Subtask #1 (20 points): $|S| ≤ 1,000$ Subtask #2 (80 points): $|S| ≤ 10^{5}$ ----- Sample Input 1 ------ 3 akramkeeanany aka supahotboy bohoty daehabshatorawy badawy ----- Sample Output 1 ------ aaakaeekmnnry abohotypsu aabadawyehhorst
tests = int(input()) for i in range(tests): s = input() p = input() arr = [0] * 26 for j in s: arr[ord(j) - 97] += 1 for j in p: arr[ord(j) - 97] -= 1 ans = "" p_added = False for j in range(26): add = chr(97 + j) * arr[j] if add + p > p + add and not p_added: ans += p p_added = True ans += add if not p_added: ans += p print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has a string $S$. He also has another string $P$, called *pattern*. He wants to find the pattern in $S$, but that might be impossible. Therefore, he is willing to reorder the characters of $S$ in such a way that $P$ occurs in the resulting string (an anagram of $S$) as a substring. Since this problem was too hard for Chef, he decided to ask you, his genius friend, for help. Can you find the lexicographically smallest anagram of $S$ that contains $P$ as substring? Note: A string $B$ is a substring of a string $A$ if $B$ can be obtained from $A$ by deleting several (possibly none or all) characters from the beginning and several (possibly none or all) characters from the end. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single string $S$. The second line contains a single string $P$. ------ Output ------ For each test case, print a single line containing one string ― the smallest anagram of $S$ that contains $P$. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ |P| ≤ |S| ≤ 10^{5}$ $S$ and $P$ contain only lowercase English letters there is at least one anagram of $S$ that contains $P$ ------ Subtasks ------ Subtask #1 (20 points): $|S| ≤ 1,000$ Subtask #2 (80 points): $|S| ≤ 10^{5}$ ----- Sample Input 1 ------ 3 akramkeeanany aka supahotboy bohoty daehabshatorawy badawy ----- Sample Output 1 ------ aaakaeekmnnry abohotypsu aabadawyehhorst
for _ in range(int(input())): s = input() p = input() list1 = [] list1[:0] = s list2 = [] list2[:0] = p for i in list2: list1.remove(i) list1.sort() str1 = "" str2 = "" s = str1.join(list1) p = str2.join(list2) temp = p[0] empty = "" third = "" second = "" for i in range(len(s)): if s[i] < temp: empty = empty + s[i] elif s[i] == temp: third = third + s[i] else: second = second + s[i] print(min(empty + third + p + second, empty + p + third + second))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has a string $S$. He also has another string $P$, called *pattern*. He wants to find the pattern in $S$, but that might be impossible. Therefore, he is willing to reorder the characters of $S$ in such a way that $P$ occurs in the resulting string (an anagram of $S$) as a substring. Since this problem was too hard for Chef, he decided to ask you, his genius friend, for help. Can you find the lexicographically smallest anagram of $S$ that contains $P$ as substring? Note: A string $B$ is a substring of a string $A$ if $B$ can be obtained from $A$ by deleting several (possibly none or all) characters from the beginning and several (possibly none or all) characters from the end. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single string $S$. The second line contains a single string $P$. ------ Output ------ For each test case, print a single line containing one string ― the smallest anagram of $S$ that contains $P$. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ |P| ≤ |S| ≤ 10^{5}$ $S$ and $P$ contain only lowercase English letters there is at least one anagram of $S$ that contains $P$ ------ Subtasks ------ Subtask #1 (20 points): $|S| ≤ 1,000$ Subtask #2 (80 points): $|S| ≤ 10^{5}$ ----- Sample Input 1 ------ 3 akramkeeanany aka supahotboy bohoty daehabshatorawy badawy ----- Sample Output 1 ------ aaakaeekmnnry abohotypsu aabadawyehhorst
t = int(input()) for j in range(t): s = input() p = input() s1 = list(s) p1 = list(p) for i in p1: if i in s1: s1.remove(i) f = "" l = "" e = "" for k in s1: if k < p[0]: f += k elif k == p[0]: e += k else: l += k f = "".join(sorted(f)) l = "".join(sorted(l)) ans = min(f + e + p + l, f + p + e + l) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR VAR IF VAR VAR NUMBER VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has a string $S$. He also has another string $P$, called *pattern*. He wants to find the pattern in $S$, but that might be impossible. Therefore, he is willing to reorder the characters of $S$ in such a way that $P$ occurs in the resulting string (an anagram of $S$) as a substring. Since this problem was too hard for Chef, he decided to ask you, his genius friend, for help. Can you find the lexicographically smallest anagram of $S$ that contains $P$ as substring? Note: A string $B$ is a substring of a string $A$ if $B$ can be obtained from $A$ by deleting several (possibly none or all) characters from the beginning and several (possibly none or all) characters from the end. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single string $S$. The second line contains a single string $P$. ------ Output ------ For each test case, print a single line containing one string ― the smallest anagram of $S$ that contains $P$. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ |P| ≤ |S| ≤ 10^{5}$ $S$ and $P$ contain only lowercase English letters there is at least one anagram of $S$ that contains $P$ ------ Subtasks ------ Subtask #1 (20 points): $|S| ≤ 1,000$ Subtask #2 (80 points): $|S| ≤ 10^{5}$ ----- Sample Input 1 ------ 3 akramkeeanany aka supahotboy bohoty daehabshatorawy badawy ----- Sample Output 1 ------ aaakaeekmnnry abohotypsu aabadawyehhorst
t = int(input()) for _ in range(t): s = input() p = input() g = list(s) left = [] right = [] mid = [] for i in p: g.remove(i) s = "".join(g) for i in s: if i > p[0]: right.append(i) elif i == p[0]: mid.append(i) else: left.append(i) left.sort() right.sort() ans = min( "".join(left) + p + "".join(mid) + "".join(right), "".join(left) + "".join(mid) + p + "".join(right), ) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING VAR FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL STRING VAR VAR FUNC_CALL STRING VAR FUNC_CALL STRING VAR BIN_OP BIN_OP BIN_OP FUNC_CALL STRING VAR FUNC_CALL STRING VAR VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has a string $S$. He also has another string $P$, called *pattern*. He wants to find the pattern in $S$, but that might be impossible. Therefore, he is willing to reorder the characters of $S$ in such a way that $P$ occurs in the resulting string (an anagram of $S$) as a substring. Since this problem was too hard for Chef, he decided to ask you, his genius friend, for help. Can you find the lexicographically smallest anagram of $S$ that contains $P$ as substring? Note: A string $B$ is a substring of a string $A$ if $B$ can be obtained from $A$ by deleting several (possibly none or all) characters from the beginning and several (possibly none or all) characters from the end. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single string $S$. The second line contains a single string $P$. ------ Output ------ For each test case, print a single line containing one string ― the smallest anagram of $S$ that contains $P$. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ |P| ≤ |S| ≤ 10^{5}$ $S$ and $P$ contain only lowercase English letters there is at least one anagram of $S$ that contains $P$ ------ Subtasks ------ Subtask #1 (20 points): $|S| ≤ 1,000$ Subtask #2 (80 points): $|S| ≤ 10^{5}$ ----- Sample Input 1 ------ 3 akramkeeanany aka supahotboy bohoty daehabshatorawy badawy ----- Sample Output 1 ------ aaakaeekmnnry abohotypsu aabadawyehhorst
t = int(input()) for tc in range(t): s = input() p = input() d = {} for i in range(len(s)): try: d[s[i]] += 1 except: d[s[i]] = 1 for i in range(len(p)): d[p[i]] -= 1 d = {k: v for k, v in sorted(d.items(), key=lambda item: item[0])} res = "" res1 = "" trigger = p[0] for k, v in d.items(): if k == trigger: res1 += p res1 += k * v res += k * v res += p else: res += k * v res1 += k * v print(min(res, res1))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has a string $S$. He also has another string $P$, called *pattern*. He wants to find the pattern in $S$, but that might be impossible. Therefore, he is willing to reorder the characters of $S$ in such a way that $P$ occurs in the resulting string (an anagram of $S$) as a substring. Since this problem was too hard for Chef, he decided to ask you, his genius friend, for help. Can you find the lexicographically smallest anagram of $S$ that contains $P$ as substring? Note: A string $B$ is a substring of a string $A$ if $B$ can be obtained from $A$ by deleting several (possibly none or all) characters from the beginning and several (possibly none or all) characters from the end. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single string $S$. The second line contains a single string $P$. ------ Output ------ For each test case, print a single line containing one string ― the smallest anagram of $S$ that contains $P$. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ |P| ≤ |S| ≤ 10^{5}$ $S$ and $P$ contain only lowercase English letters there is at least one anagram of $S$ that contains $P$ ------ Subtasks ------ Subtask #1 (20 points): $|S| ≤ 1,000$ Subtask #2 (80 points): $|S| ≤ 10^{5}$ ----- Sample Input 1 ------ 3 akramkeeanany aka supahotboy bohoty daehabshatorawy badawy ----- Sample Output 1 ------ aaakaeekmnnry abohotypsu aabadawyehhorst
t = int(input()) for _ in range(t): string = list(input()) pattern = list(input()) for i in pattern: string.remove(i) string.sort() new_st = "".join(string) pattern = "".join(pattern) p = len(pattern) l = len(new_st) i = 0 while i < l and new_st[i] < pattern[0]: i += 1 new_st1 = new_st[0:i] + pattern + new_st[i:] i = 0 while i < l and new_st[i] <= pattern[0]: i += 1 new_st2 = new_st[0:i] + pattern + new_st[i:] print(min(new_st1, new_st2))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
The Postmaster wants to write a program to answer the queries regarding letter collection in a city. A city is represented as a matrix mat of size n*m. Each cell represents a house and contains letters which are denoted by a number in the cell. The program should answer q queries which are of following types: 1 i j : To sum all the letters which are at a 1-hop distance from the cell (i,j) in any direction 2 i j : To sum all the letters which are at a 2-hop distance from the cell (i,j) in any direction The queries are given in a 2D matrix queries[][]. Example 1: Input: n = 4, m = 4 mat = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 1, 3, 4}, {1, 2, 3, 4}} q = 2 queries = {{1 0 1}, {2 0 1}} Output: 22 29 Explaination: For the first query sum is 1+5+6+7+3 = 22. For the second query sum is 9+1+3+4+8+4 = 29. Your Task: You do not need to read input or print anything. Your task is to complete the function matrixSum() which takes n, m, mat, q and queries as input parameters and returns a list of integers where the ith value is the answers for ith query. Expected Time Complexity: O(q) Expected Auxiliary Space: O(q) Constraints: 1 ≤ n, m ≤ 20 1 ≤ q ≤ 100
class Solution: def matrixSum(self, n, m, mat, q, queries): output = [] for hop, i, j in queries: output.append(0) if j - hop >= 0: for k in range(max(0, i - hop), min(n, i + hop)): output[-1] += mat[k][j - hop] if j + hop < m: for k in range(max(0, i - hop + 1), min(n, i + hop + 1)): output[-1] += mat[k][j + hop] if i - hop >= 0: for k in range(max(0, j - hop + 1), min(m, j + hop + 1)): output[-1] += mat[i - hop][k] if i + hop < n: for k in range(max(0, j - hop), min(m, j + hop)): output[-1] += mat[i + hop][k] return output
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR RETURN VAR
The Postmaster wants to write a program to answer the queries regarding letter collection in a city. A city is represented as a matrix mat of size n*m. Each cell represents a house and contains letters which are denoted by a number in the cell. The program should answer q queries which are of following types: 1 i j : To sum all the letters which are at a 1-hop distance from the cell (i,j) in any direction 2 i j : To sum all the letters which are at a 2-hop distance from the cell (i,j) in any direction The queries are given in a 2D matrix queries[][]. Example 1: Input: n = 4, m = 4 mat = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 1, 3, 4}, {1, 2, 3, 4}} q = 2 queries = {{1 0 1}, {2 0 1}} Output: 22 29 Explaination: For the first query sum is 1+5+6+7+3 = 22. For the second query sum is 9+1+3+4+8+4 = 29. Your Task: You do not need to read input or print anything. Your task is to complete the function matrixSum() which takes n, m, mat, q and queries as input parameters and returns a list of integers where the ith value is the answers for ith query. Expected Time Complexity: O(q) Expected Auxiliary Space: O(q) Constraints: 1 ≤ n, m ≤ 20 1 ≤ q ≤ 100
class Solution: def matrixSum(self, n, m, mat, q, queries): ans = [] for ele in queries: d, i, j = ele temp = 0 if d == 1: if i - 1 >= 0: temp += mat[i - 1][j] if j - 1 >= 0: temp += mat[i][j - 1] if i + 1 < n: temp += mat[i + 1][j] if j + 1 < m: temp += mat[i][j + 1] if i - 1 >= 0 and j - 1 >= 0: temp += mat[i - 1][j - 1] if i - 1 >= 0 and j + 1 < m: temp += mat[i - 1][j + 1] if i + 1 < n and j - 1 >= 0: temp += mat[i + 1][j - 1] if i + 1 < n and j + 1 < m: temp += mat[i + 1][j + 1] elif d == 2: temp = 0 if i - 2 >= 0: x, y = max(j - 2, 0), min(m, j + 3) for k in range(x, y): temp += mat[i - 2][k] if i + 2 < n: x, y = max(j - 2, 0), min(m, j + 3) for k in range(x, y): temp += mat[i + 2][k] if j - 2 >= 0: x, y = max(i - 2, 0), min(n, i + 3) for k in range(x, y): temp += mat[k][j - 2] if j + 2 < m: x, y = max(i - 2, 0), min(n, i + 3) for k in range(x, y): temp += mat[k][j + 2] if i - 2 >= 0 and j - 2 >= 0: temp -= mat[i - 2][j - 2] if i - 2 >= 0 and j + 2 < m: temp -= mat[i - 2][j + 2] if i + 2 < n and j - 2 >= 0: temp -= mat[i + 2][j - 2] if i + 2 < n and j + 2 < m: temp -= mat[i + 2][j + 2] ans.append(temp) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
The Postmaster wants to write a program to answer the queries regarding letter collection in a city. A city is represented as a matrix mat of size n*m. Each cell represents a house and contains letters which are denoted by a number in the cell. The program should answer q queries which are of following types: 1 i j : To sum all the letters which are at a 1-hop distance from the cell (i,j) in any direction 2 i j : To sum all the letters which are at a 2-hop distance from the cell (i,j) in any direction The queries are given in a 2D matrix queries[][]. Example 1: Input: n = 4, m = 4 mat = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 1, 3, 4}, {1, 2, 3, 4}} q = 2 queries = {{1 0 1}, {2 0 1}} Output: 22 29 Explaination: For the first query sum is 1+5+6+7+3 = 22. For the second query sum is 9+1+3+4+8+4 = 29. Your Task: You do not need to read input or print anything. Your task is to complete the function matrixSum() which takes n, m, mat, q and queries as input parameters and returns a list of integers where the ith value is the answers for ith query. Expected Time Complexity: O(q) Expected Auxiliary Space: O(q) Constraints: 1 ≤ n, m ≤ 20 1 ≤ q ≤ 100
class Solution: def matrixSum(self, n, m, mat, q, queries): t1 = [-1, 0, 1] t2 = [-2, -1, 0, 1, 2] res = [] for g in range(q): z, x, y = queries[g] ans1 = 0 ans2 = 0 if z == 1: for i in range(len(t1)): for j in range(len(t1)): if not (t1[i] == 0 and t1[j] == 0): if ( n > x + t1[i] and x + t1[i] >= 0 and m > y + t1[j] and y + t1[j] >= 0 ): ans1 += mat[x + t1[i]][y + t1[j]] res.append(ans1) elif z == 2: for i in range(len(t2)): for j in range(len(t2)): if abs(t2[i]) == 2 or abs(t2[j]) == 2: if ( n > x + t2[i] and x + t2[i] >= 0 and m > y + t2[j] and y + t2[j] >= 0 ): ans2 += mat[x + t2[i]][y + t2[j]] res.append(ans2) return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
The Postmaster wants to write a program to answer the queries regarding letter collection in a city. A city is represented as a matrix mat of size n*m. Each cell represents a house and contains letters which are denoted by a number in the cell. The program should answer q queries which are of following types: 1 i j : To sum all the letters which are at a 1-hop distance from the cell (i,j) in any direction 2 i j : To sum all the letters which are at a 2-hop distance from the cell (i,j) in any direction The queries are given in a 2D matrix queries[][]. Example 1: Input: n = 4, m = 4 mat = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 1, 3, 4}, {1, 2, 3, 4}} q = 2 queries = {{1 0 1}, {2 0 1}} Output: 22 29 Explaination: For the first query sum is 1+5+6+7+3 = 22. For the second query sum is 9+1+3+4+8+4 = 29. Your Task: You do not need to read input or print anything. Your task is to complete the function matrixSum() which takes n, m, mat, q and queries as input parameters and returns a list of integers where the ith value is the answers for ith query. Expected Time Complexity: O(q) Expected Auxiliary Space: O(q) Constraints: 1 ≤ n, m ≤ 20 1 ≤ q ≤ 100
class Solution: def matrixSum(self, n, m, mat, q, que): ans = [] for i in range(q): x = que[i][1] y = que[i][2] s = 0 if que[i][0] == 1: for j in range(x - 1, x + 2): for k in range(y - 1, y + 2): if k >= 0 and j >= 0 and j < n and k < m: s += mat[j][k] s -= mat[x][y] ans.append(s) if que[i][0] == 2: for j in range(x - 2, x + 3): for k in range(y - 2, y + 3): if ( k >= 0 and j >= 0 and j < n and k < m and (j == x - 2 or k == y - 2 or j == x + 2 or k == y + 2) ): s += mat[j][k] ans.append(s) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
The Postmaster wants to write a program to answer the queries regarding letter collection in a city. A city is represented as a matrix mat of size n*m. Each cell represents a house and contains letters which are denoted by a number in the cell. The program should answer q queries which are of following types: 1 i j : To sum all the letters which are at a 1-hop distance from the cell (i,j) in any direction 2 i j : To sum all the letters which are at a 2-hop distance from the cell (i,j) in any direction The queries are given in a 2D matrix queries[][]. Example 1: Input: n = 4, m = 4 mat = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 1, 3, 4}, {1, 2, 3, 4}} q = 2 queries = {{1 0 1}, {2 0 1}} Output: 22 29 Explaination: For the first query sum is 1+5+6+7+3 = 22. For the second query sum is 9+1+3+4+8+4 = 29. Your Task: You do not need to read input or print anything. Your task is to complete the function matrixSum() which takes n, m, mat, q and queries as input parameters and returns a list of integers where the ith value is the answers for ith query. Expected Time Complexity: O(q) Expected Auxiliary Space: O(q) Constraints: 1 ≤ n, m ≤ 20 1 ≤ q ≤ 100
class Solution: def matrixSum(self, n, m, mat, q, queries): res = [] for i in range(q): sum1 = 0 dist = queries[i][0] ind_i = queries[i][1] ind_j = queries[i][2] i_mod_top = ind_i - dist i_mod_bot = ind_i + dist j_mod_left = ind_j - dist j_mod_right = ind_j + dist for k in range(j_mod_left, j_mod_right + 1): if i_mod_top < 0 or i_mod_top >= n or k < 0 or k >= m: continue sum1 += mat[i_mod_top][k] i_mod_top += 1 for k in range(j_mod_left, j_mod_right + 1): if i_mod_bot < 0 or i_mod_bot >= n or k < 0 or k >= m: continue sum1 += mat[i_mod_bot][k] i_mod_bot -= 1 for k in range(i_mod_top, i_mod_bot + 1): if j_mod_left < 0 or j_mod_left >= m or k < 0 or k >= n: continue sum1 += mat[k][j_mod_left] j_mod_left += 1 for k in range(i_mod_top, i_mod_bot + 1): if j_mod_right < 0 or j_mod_right >= m or k < 0 or k >= n: continue sum1 += mat[k][j_mod_right] j_mod_right -= 1 res.append(sum1) return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
The Postmaster wants to write a program to answer the queries regarding letter collection in a city. A city is represented as a matrix mat of size n*m. Each cell represents a house and contains letters which are denoted by a number in the cell. The program should answer q queries which are of following types: 1 i j : To sum all the letters which are at a 1-hop distance from the cell (i,j) in any direction 2 i j : To sum all the letters which are at a 2-hop distance from the cell (i,j) in any direction The queries are given in a 2D matrix queries[][]. Example 1: Input: n = 4, m = 4 mat = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 1, 3, 4}, {1, 2, 3, 4}} q = 2 queries = {{1 0 1}, {2 0 1}} Output: 22 29 Explaination: For the first query sum is 1+5+6+7+3 = 22. For the second query sum is 9+1+3+4+8+4 = 29. Your Task: You do not need to read input or print anything. Your task is to complete the function matrixSum() which takes n, m, mat, q and queries as input parameters and returns a list of integers where the ith value is the answers for ith query. Expected Time Complexity: O(q) Expected Auxiliary Space: O(q) Constraints: 1 ≤ n, m ≤ 20 1 ≤ q ≤ 100
class Solution: def matrixSum(self, n, m, mat, q, queries): ans = [] for i in range(q): s = 0 t = queries[i][0] a = queries[i][1] b = queries[i][2] hop1x = [a - 1, a - 1, a - 1, a, a, a + 1, a + 1, a + 1] hop1y = [b - 1, b, b + 1, b - 1, b + 1, b - 1, b, b + 1] hop2x = [ a - 2, a - 2, a - 2, a - 2, a - 2, a - 1, a - 1, a, a, a + 1, a + 1, a + 2, a + 2, a + 2, a + 2, a + 2, ] hop2y = [ b - 2, b - 1, b, b + 1, b + 2, b - 2, b + 2, b - 2, b + 2, b - 2, b + 2, b - 2, b - 1, b, b + 1, b + 2, ] if t == 1: for j in range(8): if hop1x[j] in list(range(0, n)) and hop1y[j] in list(range(0, m)): s += mat[hop1x[j]][hop1y[j]] elif t == 2: for j in range(16): if hop2x[j] in range(0, n) and hop2y[j] in range(0, m): s += mat[hop2x[j]][hop2y[j]] ans.append(s) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR FUNC_CALL VAR NUMBER VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
The Postmaster wants to write a program to answer the queries regarding letter collection in a city. A city is represented as a matrix mat of size n*m. Each cell represents a house and contains letters which are denoted by a number in the cell. The program should answer q queries which are of following types: 1 i j : To sum all the letters which are at a 1-hop distance from the cell (i,j) in any direction 2 i j : To sum all the letters which are at a 2-hop distance from the cell (i,j) in any direction The queries are given in a 2D matrix queries[][]. Example 1: Input: n = 4, m = 4 mat = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 1, 3, 4}, {1, 2, 3, 4}} q = 2 queries = {{1 0 1}, {2 0 1}} Output: 22 29 Explaination: For the first query sum is 1+5+6+7+3 = 22. For the second query sum is 9+1+3+4+8+4 = 29. Your Task: You do not need to read input or print anything. Your task is to complete the function matrixSum() which takes n, m, mat, q and queries as input parameters and returns a list of integers where the ith value is the answers for ith query. Expected Time Complexity: O(q) Expected Auxiliary Space: O(q) Constraints: 1 ≤ n, m ≤ 20 1 ≤ q ≤ 100
class Solution: def matrixSum(self, n, m, mat, q, queries): ans = [] for t, x, y in queries: temp = 0 if t == 1: for i in range(-1, 2): for j in range(-1, 2): if i == 0 and j == 0: continue nx = x + i ny = y + j if nx < 0 or nx >= n or ny < 0 or ny >= m: continue temp += mat[nx][ny] ans.append(temp) else: for i in range(-2, 3): for j in range(-2, 3): if max(abs(i), abs(j)) != 2: continue nx = x + i ny = y + j if nx < 0 or nx >= n or ny < 0 or ny >= m: continue temp += mat[nx][ny] ans.append(temp) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
The Postmaster wants to write a program to answer the queries regarding letter collection in a city. A city is represented as a matrix mat of size n*m. Each cell represents a house and contains letters which are denoted by a number in the cell. The program should answer q queries which are of following types: 1 i j : To sum all the letters which are at a 1-hop distance from the cell (i,j) in any direction 2 i j : To sum all the letters which are at a 2-hop distance from the cell (i,j) in any direction The queries are given in a 2D matrix queries[][]. Example 1: Input: n = 4, m = 4 mat = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 1, 3, 4}, {1, 2, 3, 4}} q = 2 queries = {{1 0 1}, {2 0 1}} Output: 22 29 Explaination: For the first query sum is 1+5+6+7+3 = 22. For the second query sum is 9+1+3+4+8+4 = 29. Your Task: You do not need to read input or print anything. Your task is to complete the function matrixSum() which takes n, m, mat, q and queries as input parameters and returns a list of integers where the ith value is the answers for ith query. Expected Time Complexity: O(q) Expected Auxiliary Space: O(q) Constraints: 1 ≤ n, m ≤ 20 1 ≤ q ≤ 100
class Solution: def matrixSum(self, n, m, mat, q, queries): s = [] for i in range(q): p = queries[i][0] r = queries[i][1] c = queries[i][2] ans = 0 if p == 1: row = r - 1 col = c - 1 if row >= 0: ans = ans + mat[row][c] if col >= 0: ans = ans + mat[r][col] if row >= 0 and col >= 0: ans = ans + mat[row][col] row = r + 1 col = c + 1 if row < n: ans = ans + mat[row][c] if col < m: ans = ans + mat[r][col] if row < n and col < m: ans = ans + mat[row][col] row = r + 1 col = c - 1 if row < n and col >= 0: ans = ans + mat[row][col] row = r - 1 col = c + 1 if row >= 0 and col < m: ans = ans + mat[row][col] s.append(ans) else: row = r - 2 col = c - 2 if row >= 0: ans = ans + mat[row][c] if c - 1 >= 0: ans = ans + mat[row][c - 1] if c + 1 < m: ans = ans + mat[row][c + 1] if col >= 0: ans = ans + mat[r][col] if r - 1 >= 0: ans = ans + mat[r - 1][col] if r + 1 < n: ans = ans + mat[r + 1][col] if row >= 0 and col >= 0: ans = ans + mat[row][col] row = r + 2 col = c + 2 if row < n: ans = ans + mat[row][c] if c - 1 >= 0: ans = ans + mat[row][c - 1] if c + 1 < m: ans = ans + mat[row][c + 1] if col < m: ans = ans + mat[r][col] if r - 1 >= 0: ans = ans + mat[r - 1][col] if r + 1 < n: ans = ans + mat[r + 1][col] if row < n and col < m: ans = ans + mat[row][col] row = r + 2 col = c - 2 if row < n and col >= 0: ans = ans + mat[row][col] row = r - 2 col = c + 2 if row >= 0 and col < m: ans = ans + mat[row][col] s.append(ans) return s
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
You are given an array x of n positive numbers. You start at point (0,0) and moves x[0] metres to the north, then x[1] metres to the west, x[2] metres to the south, x[3] metres to the east and so on. In other words, after each move your direction changes counter-clockwise. Write a one-pass algorithm with O(1) extra space to determine, if your path crosses itself, or not. Example 1: Given x = [2, 1, 1, 2], ????? ? ? ???????> ? Return true (self crossing) Example 2: Given x = [1, 2, 3, 4], ???????? ? ? ? ? ?????????????> Return false (not self crossing) Example 3: Given x = [1, 1, 1, 1], ????? ? ? ?????> Return true (self crossing) Credits:Special thanks to @dietpepsi for adding this problem and creating all test cases.
class Solution: def isSelfCrossing(self, x): b = c = d = e = 0 for a in x: if d >= b > 0 and (a >= c or a >= c - e >= 0 and f >= d - b): return True b, c, d, e, f = a, b, c, d, e return False
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER
You are given an array x of n positive numbers. You start at point (0,0) and moves x[0] metres to the north, then x[1] metres to the west, x[2] metres to the south, x[3] metres to the east and so on. In other words, after each move your direction changes counter-clockwise. Write a one-pass algorithm with O(1) extra space to determine, if your path crosses itself, or not. Example 1: Given x = [2, 1, 1, 2], ????? ? ? ???????> ? Return true (self crossing) Example 2: Given x = [1, 2, 3, 4], ???????? ? ? ? ? ?????????????> Return false (not self crossing) Example 3: Given x = [1, 1, 1, 1], ????? ? ? ?????> Return true (self crossing) Credits:Special thanks to @dietpepsi for adding this problem and creating all test cases.
class Solution: def isSelfCrossing(self, x): l = len(x) iscross = False if l < 4: return False for i in range(3, l): if x[i - 3] >= x[i - 1] and x[i - 2] <= x[i]: return True if i >= 4 and x[i - 4] + x[i] >= x[i - 2] and x[i - 3] == x[i - 1]: return True if ( i >= 5 and x[i - 5] + x[i - 1] >= x[i - 3] and x[i - 4] + x[i] >= x[i - 2] and x[i - 2] >= x[i - 4] and x[i - 2] > x[i - 4] and x[i - 3] > x[i - 5] and x[i - 1] < x[i - 3] ): return True iscross = False return iscross
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER IF VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER RETURN VAR
You are given an array x of n positive numbers. You start at point (0,0) and moves x[0] metres to the north, then x[1] metres to the west, x[2] metres to the south, x[3] metres to the east and so on. In other words, after each move your direction changes counter-clockwise. Write a one-pass algorithm with O(1) extra space to determine, if your path crosses itself, or not. Example 1: Given x = [2, 1, 1, 2], ????? ? ? ???????> ? Return true (self crossing) Example 2: Given x = [1, 2, 3, 4], ???????? ? ? ? ? ?????????????> Return false (not self crossing) Example 3: Given x = [1, 1, 1, 1], ????? ? ? ?????> Return true (self crossing) Credits:Special thanks to @dietpepsi for adding this problem and creating all test cases.
class Solution: def isSelfCrossing(self, x): n = len(x) if n < 4: return False i = 3 while i < n: if x[i] >= x[i - 2] and x[i - 3] >= x[i - 1]: return True if i >= 4: if x[i - 1] == x[i - 3] and x[i] + x[i - 4] >= x[i - 2]: return True if i >= 5: if ( x[i] >= x[i - 2] - x[i - 4] >= 0 and x[i - 5] >= x[i - 3] - x[i - 1] >= 0 ): return True i += 1 return False
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR NUMBER IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER VAR NUMBER RETURN NUMBER
You are given an array x of n positive numbers. You start at point (0,0) and moves x[0] metres to the north, then x[1] metres to the west, x[2] metres to the south, x[3] metres to the east and so on. In other words, after each move your direction changes counter-clockwise. Write a one-pass algorithm with O(1) extra space to determine, if your path crosses itself, or not. Example 1: Given x = [2, 1, 1, 2], ????? ? ? ???????> ? Return true (self crossing) Example 2: Given x = [1, 2, 3, 4], ???????? ? ? ? ? ?????????????> Return false (not self crossing) Example 3: Given x = [1, 1, 1, 1], ????? ? ? ?????> Return true (self crossing) Credits:Special thanks to @dietpepsi for adding this problem and creating all test cases.
class Solution: def isSelfCrossing(self, x): if not x or len(x) < 4: return False i = 3 while i < len(x): if x[i] >= x[i - 2] and x[i - 1] <= x[i - 3]: print("case 1") return True elif i >= 4 and x[i - 1] == x[i - 3] and x[i] + x[i - 4] >= x[i - 2]: print("case 2") return True elif ( i >= 5 and x[i - 4] < x[i - 2] <= x[i] + x[i - 4] and x[i - 1] <= x[i - 3] <= x[i] + x[i - 5] ): print("case 3") return True i += 1 return False
CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN NUMBER VAR NUMBER RETURN NUMBER
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(head1, head2): temp1 = head1 temp2 = head2 sortedList = Node(-1) if temp1.data <= temp2.data: sortedList.data = temp1.data temp1 = temp1.next else: sortedList.data = temp2.data temp2 = temp2.next sortedList.next = None resHead = sortedList while temp1 != None and temp2 != None: newNode = Node(-1) if temp1.data <= temp2.data: sortedList.next = newNode newNode.data = temp1.data temp1 = temp1.next else: sortedList.next = newNode newNode.data = temp2.data temp2 = temp2.next sortedList = sortedList.next while temp1 != None: newNode = Node(-1) sortedList.next = newNode newNode.data = temp1.data temp1 = temp1.next sortedList = sortedList.next while temp2 != None: newNode = Node(-1) sortedList.next = newNode newNode.data = temp2.data temp2 = temp2.next sortedList = sortedList.next return resHead
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR VAR WHILE VAR NONE VAR NONE ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(head1, head2): dummy = Node cur = dummy while True: if head1.data <= head2.data: cur.next = head1 head1 = head1.next else: cur.next = head2 head2 = head2.next cur = cur.next if head1 is None and head2: cur.next = head2 break if head2 is None and head1: cur.next = head1 break return dummy.next
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE VAR ASSIGN VAR VAR IF VAR NONE VAR ASSIGN VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(head1, head2): dummy = Node(None) curr = dummy p1, p2 = head1, head2 while p1 and p2: if p1.data <= p2.data: curr.next = p1 p1 = p1.next else: curr.next = p2 p2 = p2.next curr = curr.next if p1: curr.next = p1 if p2: curr.next = p2 return dummy.next
FUNC_DEF ASSIGN VAR FUNC_CALL VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(head1, head2): head = None tail = None curr1 = head1 curr2 = head2 while curr1 != None and curr2 != None: if curr1.data <= curr2.data: if head == None: head = curr1 tail = curr1 curr1 = curr1.next else: tail.next = curr1 curr1 = curr1.next tail = tail.next tail.next = None elif head == None: head = curr2 tail = curr2 curr2 = curr2.next else: tail.next = curr2 curr2 = curr2.next tail = tail.next tail.next = None while curr1 != None: tail.next = curr1 curr1 = curr1.next tail = tail.next tail.next = None while curr2 != None: tail.next = curr2 curr2 = curr2.next tail = tail.next tail.next = None return head
FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE VAR NONE IF VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(headA, headB): dummyNode = Node(0) tail = dummyNode while True: if headA is None: tail.next = headB break if headB is None: tail.next = headA break if headA.data <= headB.data: tail.next = headA headA = headA.next else: tail.next = headB headB = headB.next tail = tail.next return dummyNode.next
FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE NUMBER IF VAR NONE ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def findBetween(a1, a2, b): start_b = b while b is not None and a1.data <= b.data <= a2.data: if b.next is None or b.next.data > a2.data: break b = b.next return start_b, b def sortedMerge(head1, head2): temp_node = Node(-1) temp_node.next = head1 a1, a2, b = temp_node, temp_node.next, head2 next_b = b while a2 is not None and next_b is not None: start_b, b = findBetween(a1, a2, next_b) if a1.data <= start_b.data <= a2.data: next_b = b.next a1.next = start_b b.next = a2 a1 = a2 if a2.next is None and next_b is not None: a2.next = next_b break else: a2 = a2.next return temp_node.next
FUNC_DEF ASSIGN VAR VAR WHILE VAR NONE VAR VAR VAR IF VAR NONE VAR VAR ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR WHILE VAR NONE VAR NONE ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(left, right): if not left: return right if not right: return left ans = Node(-1) temp = ans while left and right: if left.data < right.data: temp.next = left temp = left left = left.next else: temp.next = right temp = right right = right.next if left: temp.next = left if right: temp.next = right return ans.next
FUNC_DEF IF VAR RETURN VAR IF VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(head1, head2): a = [] b = [] h1 = head1 h2 = head2 while h1: a.append(h1.data) h1 = h1.next while h2: b.append(h2.data) h2 = h2.next r = a + b r.sort() head = None for i in range(len(r) - 1, -1, -1): ne = Node(r[i]) ne.next = head head = ne return head
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(head1, head2): if head1 == None: return head2 if head2 == None: return head1 if head1.data > head2.data: temp = head2.next head2.next = head1 head1 = head2 head2 = temp temp = head1 prev = temp while temp != None and head2 != None: if temp.data > head2.data: prev.next = head2 temp2 = head2.next head2.next = temp prev = prev.next head2 = temp2 else: prev = temp temp = temp.next while head2 != None: prev.next = head2 head2 = head2.next prev = prev.next return head1
FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE VAR NONE IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(head1, head2): temp1 = head1 temp2 = head2 a = [] b = [] while temp1: a += [temp1.data] temp1 = temp1.next while temp2: b += [temp2.data] temp2 = temp2.next a.extend(b) a.sort() head = Node(a[0]) T = head for i in range(1, len(a)): x = Node(a[i]) head.next = x head = head.next return T
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR VAR LIST VAR ASSIGN VAR VAR WHILE VAR VAR LIST VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(h1, h2): ans = Node(-1) head_ans = ans while h1 or h2: if h1 != None and h2 != None: if h1.data < h2.data: tmp = Node(h1.data) ans.next = tmp ans = ans.next h1 = h1.next else: tmp = Node(h2.data) ans.next = tmp ans = ans.next h2 = h2.next elif h1 == None: ans.next = h2 break elif h2 == None: ans.next = h1 break return head_ans.next
FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR NONE VAR NONE IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def solve(temp1, temp2): while temp2 is not None and temp1.next is not None: curr = temp1.next if temp2.data >= temp1.data and temp2.data <= curr.data: temp1.next = temp2 temp2 = temp2.next temp1.next.next = curr temp1 = temp1.next else: temp1 = curr curr = curr.next if temp2 is not None: temp1.next = temp2 def sortedMerge(head1, head2): if head1 is None: return head2 if head2 is None: return head1 val1 = head1.data val2 = head2.data temp1 = head1 temp2 = head2 if val1 <= val2: solve(temp1, temp2) return head1 else: solve(temp2, temp1) return head2
FUNC_DEF WHILE VAR NONE VAR NONE ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(head1, head2): a = head1 b = head2 u = [] v = [] while a: u.append(a.data) a = a.next while b: v.append(b.data) b = b.next u = u + v u = sorted(u) head = Node(u[0]) t = head for i in range(1, len(u)): x = Node(u[i]) head.next = x head = head.next return t
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def findsmaller(curr, t1): if curr.data < t1.data: return curr return t1 def sortedMerge(head1, head2): t1 = head1 t2 = head2 if t1.data > t2.data: curr = head2 while t1 != None and curr.next != None: temp = curr.next curr.next = findsmaller(curr.next, t1) if curr.next != t1: curr = curr.next else: curr = t1 head1 = t1.next t1.next = temp t1 = head1 if curr.next == None: if curr.data < t1.data: curr.next = head1 return head2 elif t1 == None: return head2 else: curr = head1 while t2 != None and curr.next != None: temp = curr.next curr.next = findsmaller(curr.next, t2) if curr.next != t2: curr = curr.next else: curr = t2 head2 = t2.next t2.next = temp t2 = head2 if curr.next == None: if curr.data < t2.data: curr.next = head2 return head1 elif t2 == None: return head1
FUNC_DEF IF VAR VAR RETURN VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR WHILE VAR NONE VAR NONE ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE IF VAR VAR ASSIGN VAR VAR RETURN VAR IF VAR NONE RETURN VAR ASSIGN VAR VAR WHILE VAR NONE VAR NONE ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE IF VAR VAR ASSIGN VAR VAR RETURN VAR IF VAR NONE RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(start1, start2): temp1 = temp2 = None if start1.data < start2.data: temp1 = start1 temp2 = start2 else: temp1 = start2 temp2 = start1 start = temp1 prev = None node = None while temp1 and temp2: if temp1.data > temp2.data: node = temp2 temp2 = temp2.next prev.next = node node.next = temp1 prev = node else: prev = temp1 temp1 = temp1.next if temp2: prev.next = temp2 return start
FUNC_DEF ASSIGN VAR VAR NONE IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(head1, head2): x = [] y = [] temp1 = head1 temp2 = head2 while head1 != None: x.append(head1.data) head1 = head1.next while head2 != None: y.append(head2.data) head2 = head2.next z = x + y z.sort() for i in z: print(i, end=" ")
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(head1, head2): a = [] while head1: a.append(head1.data) head1 = head1.next while head2: a.append(head2.data) head2 = head2.next a.sort(reverse=True) p = None for i in a: temp = Node(i) if p is None: p = temp else: temp.next = p p = temp return p
FUNC_DEF ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NONE FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def mergeUtil(h1, h2): if h1.next == None: h1.next = h2 return h1 curr1 = h1 next1 = h1.next curr2 = h2 next2 = h2.next while next1 != None and curr2 != None: if curr2.data <= next1.data and curr2.data >= curr1.data: next2 = curr2.next curr1.next = curr2 curr2.next = next1 curr1 = curr2 next1 = curr1.next curr2 = next2 elif next1.next != None: curr1 = next1 next1 = curr1.next else: next1.next = curr2 return h1 return h1 def sortedMerge(head1, head2): if head1 is None: return head2 if head2 is None: return head1 if head1.data <= head2.data: return mergeUtil(head1, head2) else: return mergeUtil(head2, head1)
FUNC_DEF IF VAR NONE ASSIGN VAR VAR RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE VAR NONE IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(head1, head2): if head1.data <= head2.data: final_head = head1 temp = head1 i = temp.next j = head2 else: final_head = head2 temp = head2 i = head1 j = temp.next while i is not None and j is not None: if i.data <= j.data: temp.next = i temp = temp.next i = i.next else: temp.next = j temp = temp.next j = j.next while i is not None: temp.next = i temp = temp.next i = i.next while j is not None: temp.next = j temp = temp.next j = j.next return final_head
FUNC_DEF IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE VAR NONE IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(h1, h2): if h1.data > h2.data: h1, h2 = h2, h1 res = h1 while h1 and h2: temp = Node(0) while h1 and h1.data <= h2.data: temp = h1 h1 = h1.next temp.next = h2 h1, h2 = h2, h1 return res
FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(head1, head2): curr1, curr2 = head1, head2 if curr2.data < curr1.data: merged_head = curr2 curr2 = curr2.next else: merged_head = curr1 curr1 = curr1.next merged_tail = merged_head while curr1 != None and curr2 != None: node = Node(10000000) if curr2.data <= curr1.data: node = curr2 curr2 = curr2.next else: node = curr1 curr1 = curr1.next merged_tail.next = node merged_tail = node if curr1 == None: merged_tail.next = curr2 else: merged_tail.next = curr1 return merged_head
FUNC_DEF ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE VAR NONE ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(head1, head2): if head1 == None: return head2 if head2 == None: return head1 if head1.data < head2.data: head = tail = head1 head1 = head1.next else: head = tail = head2 head2 = head2.next while head1 and head2: if head1.data < head2.data: tail.next = head1 tail = head1 head1 = head1.next else: tail.next = head2 tail = head2 head2 = head2.next if head1 == None: tail.next = head2 else: tail.next = head1 return head
FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
class Node: def __init__(self, data): self.data = data self.next = None def sortedMerge(head1, head2): r = Node(0) head = r p = head1 q = head2 while p and q: if p.data < q.data: r.next = p p = p.next else: r.next = q q = q.next r = r.next while p: r.next = p p = p.next r = r.next while q: r.next = q q = q.next r = r.next return head.next
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(head1, head2): if head1 == None: return head2 if head2 == None: return head1 temp1 = head1 temp2 = head2 ans = None while temp1 != None and temp2 != None: if temp1.data <= temp2.data: temp = temp1 temp1 = temp1.next else: temp = temp2 temp2 = temp2.next if ans == None: ans = temp cur = temp else: cur.next = temp cur = cur.next if temp1 != None: cur.next = temp1 if temp2 != None: cur.next = temp2 return ans
FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE WHILE VAR NONE VAR NONE IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(head1, head2): z = Node(-1) start = z while head1 and head2: if head1.data <= head2.data: start.next = head1 head1 = head1.next start = start.next else: start.next = head2 head2 = head2.next start = start.next while head1: start.next = head1 head1 = head1.next start = start.next while head2: start.next = head2 head2 = head2.next start = start.next return z.next
FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(head1, head2): if head1.data < head2.data: res = head1 curr = head1 head1 = head1.next else: res = head2 curr = head2 head2 = head2.next while True: if head1 == None: curr.next = head2 return res elif head2 == None: curr.next = head1 return res if head1.data < head2.data: curr.next = head1 head1 = head1.next curr = curr.next else: curr.next = head2 head2 = head2.next curr = curr.next
FUNC_DEF IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE NUMBER IF VAR NONE ASSIGN VAR VAR RETURN VAR IF VAR NONE ASSIGN VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(head1, head2): if not head1: return head2 if not head2: return head1 temp1, temp2 = head1, head2 temp = None while temp1 and temp2: if temp1.data <= temp2.data: if temp: temp.next = temp1 temp = temp1 temp1 = temp1.next else: if temp: temp.next = temp2 temp = temp2 temp2 = temp2.next if temp1: temp.next = temp1 if temp2: temp.next = temp2 if head1.data <= head2.data: return head1 return head2
FUNC_DEF IF VAR RETURN VAR IF VAR RETURN VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NONE WHILE VAR VAR IF VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR IF VAR VAR RETURN VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(head1, head2): if head1 is None: return head2 if head2 is None: return head1 dummy = Node(None) tail = dummy while head1 and head2: if head1.data < head2.data: tail.next = head1 head1 = head1.next else: tail.next = head2 head2 = head2.next tail = tail.next if head1: tail.next = head1 else: tail.next = head2 return dummy.next def sortedMerge(head1, head2): if head1 is None: return head2 if head2 is None: return head1 if head1.data > head2.data: head1, head2 = head2, head1 res = head1 while head1 and head2: temp = None while head1 and head1.data <= head2.data: temp = head1 head1 = head1.next temp.next = head2 head1, head2 = head2, head1 return res
FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR NONE ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR NONE WHILE VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(head1, head2): if head1 is None: return head2 if head2 is None: return head1 ptr1 = head1 ptr2 = head2 dummy = Node(0) tail = dummy while ptr1 is not None and ptr2 is not None: if ptr1.data <= ptr2.data: tail.next = ptr1 ptr1 = ptr1.next else: tail.next = ptr2 ptr2 = ptr2.next tail = tail.next while ptr1 is not None: tail.next = ptr1 ptr1 = ptr1.next tail = tail.next while ptr2 is not None: tail.next = ptr2 ptr2 = ptr2.next tail = tail.next return dummy.next
FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR NONE VAR NONE IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def solve(head1, head2): if head1.next == None: head1.next = head2 return head1 res = Node(-1) dummy = res while head1 and head2: if head1.data < head2.data: res.next = head1 head1 = head1.next else: res.next = head2 head2 = head2.next res = res.next while head1: res.next = head1 head1 = head1.next res = res.next while head2: res.next = head2 head2 = head2.next res = res.next return dummy.next def sortedMerge(head1, head2): if head1 == None: return head2 if head2 == None: return head1 if head1.data <= head2.data: return solve(head1, head2) else: return solve(head2, head1)
FUNC_DEF IF VAR NONE ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(head1, head2): res = LinkedList() for i in range(n + m): if head2 == None or head1 != None and head1.data <= head2.data: res.append(head1.data) head1 = head1.next else: res.append(head2.data) head2 = head2.next return res.head
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NONE VAR NONE VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(head1, head2): if not head1 and not head2: return head1 if not head1: return head2 if not head2: return head1 ans = None head = None while head1 and head2: if head1.data > head2.data: if head: head.next = head2 head = head.next else: head = head2 ans = head head2 = head2.next else: if head: head.next = head1 head = head.next else: head = head1 ans = head head1 = head1.next if head1: head.next = head1 if head2: head.next = head2 return ans
FUNC_DEF IF VAR VAR RETURN VAR IF VAR RETURN VAR IF VAR RETURN VAR ASSIGN VAR NONE ASSIGN VAR NONE WHILE VAR VAR IF VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(head1, head2): if not head1: return head2 if not head2: return head1 newnew = Node(0) currr = newnew cur = head1 cur2 = head2 while cur and cur2: if cur.data <= cur2.data: currr.next = cur cur = cur.next else: currr.next = cur2 cur2 = cur2.next currr = currr.next if cur: currr.next = cur cur = cur.next if cur2: currr.next = cur2 cur2 = cur2.next return newnew.next
FUNC_DEF IF VAR RETURN VAR IF VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(h1, h2): if h1 == None: return h2 if h2 == None: return h1 if h1.data > h2.data: h1, h2 = h2, h1 res = h1 while h1 and h2: temp = None while h1 and h1.data <= h2.data: temp = h1 h1 = h1.next temp.next = h2 h1, h2 = h2, h1 return res
FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR NONE WHILE VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(head1, head2): ans = [] res = [] cur = head1 prev = head2 while cur: ans.append(cur.data) cur = cur.next while prev: res.append(prev.data) prev = prev.next ans.extend(res) ans.sort() l = Node(0) p = l for i in ans: k = Node(i) l.next = k l = l.next return p.next
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(head1, head2): l1 = head1 l2 = head2 if l1.data > l2.data: l1, l2 = l2, l1 res = l1 while l1 and l2: temp = Node(-1) temp = None while l1 and l1.data <= l2.data: temp = l1 l1 = l1.next temp.next = l2 l1, l2 = l2, l1 return res
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NONE WHILE VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
class Node: def __init__(self, data): self.data = data self.next = None def sortedMerge(head1, head2): dummyNode = Node(-1) auxA = head1 auxB = head2 tail = dummyNode breakLoop = False while not breakLoop: if auxA == None: tail.next = auxB breakLoop = True elif auxB == None: tail.next = auxA breakLoop = True elif auxA.data <= auxB.data: tail.next = auxA auxA = auxA.next else: tail.next = auxB auxB = auxB.next tail = tail.next return dummyNode.next
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NONE ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(head1, head2): if head1 is None: return head2 if head2 is None: return head1 p1 = head1 p2 = head2 dummy = Node(0) tail = dummy while p1 is not None and p2 is not None: if p1.data <= p2.data: tail.next = p1 p1 = p1.next else: tail.next = p2 p2 = p2.next tail = tail.next if p1 is not None: tail.next = p1 if p2 is not None: tail.next = p2 return dummy.next
FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR NONE VAR NONE IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(head1, head2): if head1 is None and head2 is None: return None if head1 is None and head2 is not None: return head2 if head2 is None and head1 is not None: return head1 head = Node(0) curr = head while head1 and head2: if head1.data == head2.data or head1.data < head2.data: curr.next = head1 head1 = head1.next curr = curr.next curr.next = None elif head2.data < head1.data: curr.next = head2 head2 = head2.next curr = curr.next curr.next = None if head1: curr.next = head1 if head2: curr.next = head2 return head.next
FUNC_DEF IF VAR NONE VAR NONE RETURN NONE IF VAR NONE VAR NONE RETURN VAR IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE IF VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None self.tail = None def append(self, new_value): new_node = Node(new_value) if self.head == None: self.head = new_node self.tail = new_node else: self.tail.next = new_node self.tail = new_node def sortedMerge(head1, head2): final = LinkedList() while head1 and head2: if head1.data < head2.data: final.append(head1.data) head1 = head1.next else: final.append(head2.data) head2 = head2.next while head1: final.tail.next = head1 final.tail = final.tail.next head1 = head1.next while head2: final.tail.next = head2 final.tail = final.tail.next head2 = head2.next return final.head
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(head1, head2): temp1 = head1 temp2 = head2 head = None curr = None while temp1 is not None and temp2 is not None: if temp1.data < temp2.data: if head is None: head = temp1 else: curr.next = temp1 curr = temp1 temp1 = temp1.next else: if head is None: head = temp2 else: curr.next = temp2 curr = temp2 temp2 = temp2.next while temp1 is not None: if head is None: head = temp1 else: curr.next = temp1 curr = temp1 temp1 = temp1.next while temp2 is not None: if head is None: head = temp2 else: curr.next = temp2 curr = temp2 temp2 = temp2.next return head
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE WHILE VAR NONE VAR NONE IF VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(head1, head2): a = head1 p, q = [], [] while a is not None: p.append(a.data) a = a.next b = head2 while b is not None: q.append(b.data) b = b.next list1 = sorted(p + q) c = None head3 = None for i in list1: m = Node(i) if head3 is None: head3 = m c = head3 else: c.next = m c = c.next return head3
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR LIST LIST WHILE VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(head1, head2): newhead = None temp = newhead while head1 and head2: if head1.data > head2.data: if newhead == None: newhead = head2 temp = newhead else: temp.next = head2 temp = temp.next head2 = head2.next else: if newhead == None: newhead = head1 temp = newhead else: temp.next = head1 temp = temp.next head1 = head1.next while head1: if newhead == None: newhead = head1 temp = newhead else: temp.next = head1 temp = temp.next head1 = head1.next while head2: if newhead == None: newhead = head2 temp = newhead else: temp.next = head2 temp = temp.next head2 = head2.next return newhead
FUNC_DEF ASSIGN VAR NONE ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(head1, head2): NewList = LinkedList() node1 = head1 node2 = head2 while node1 != None and node2 != None: if node1.data <= node2.data: NewList.append(node1.data) node1 = node1.next else: NewList.append(node2.data) node2 = node2.next while node1 != None: NewList.append(node1.data) node1 = node1.next while node2 != None: NewList.append(node2.data) node2 = node2.next return NewList.head
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE VAR NONE IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(head1, head2): l = [] while head1 != None: l.append(head1.data) head1 = head1.next while head2 != None: l.append(head2.data) head2 = head2.next l.sort() head = Node(l[0]) x = head for i in range(1, len(l)): y = Node(l[i]) x.next = y x = y return head
FUNC_DEF ASSIGN VAR LIST WHILE VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def solve(temp, temp1, temp2): while temp1 is not None and temp2 is not None: if temp1.data <= temp2.data: newNode = Node(temp1.data) temp.next = newNode temp = temp.next temp1 = temp1.next else: newNode = Node(temp2.data) temp.next = newNode temp = temp.next temp2 = temp2.next while temp1 is not None: newNode = Node(temp1.data) temp.next = newNode temp = temp.next temp1 = temp1.next while temp2 is not None: newNode = Node(temp2.data) temp.next = newNode temp = temp.next temp2 = temp2.next def sortedMerge(head1, head2): if head1 is None: return head2 if head2 is None: return head1 val1 = head1.data val2 = head2.data temp = Node(-1) head = temp temp1 = head1 temp2 = head2 if val1 <= val2: temp.data = val1 temp1 = temp1.next solve(temp, temp1, temp2) else: temp.data = val2 temp2 = temp2.next solve(temp, temp1, temp2) return head
FUNC_DEF WHILE VAR NONE VAR NONE IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(head1, head2): tmp = temp = None while head1 and head2: if head1.data <= head2.data: if not tmp: temp = head1 tmp = head1 else: tmp.next = head1 tmp = tmp.next head1 = head1.next else: if not tmp: temp = head2 tmp = head2 else: tmp.next = head2 tmp = tmp.next head2 = head2.next while head1: tmp.next = head1 tmp = tmp.next head1 = head1.next while head2: tmp.next = head2 tmp = tmp.next head2 = head2.next return temp
FUNC_DEF ASSIGN VAR VAR NONE WHILE VAR VAR IF VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR