description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell. Example 1: Input: N = 1 dictionary = {"CAT"} R = 3, C = 3 board = {{C,A,P},{A,N,D},{T,I,E}} Output: CAT Explanation: C A P A N D T I E Words we got is denoted using same color. Example 2: Input: N = 4 dictionary = {"GEEKS","FOR","QUIZ","GO"} R = 3, C = 3 board = {{G,I,Z},{U,E,K},{Q,S,E}} Output: GEEKS QUIZ Explanation: G I Z U E K Q S E Words we got is denoted using same color. Your task: You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary). Expected Time Complexity: O(4^(N^2)) Expected Auxiliary Space: O(N^2) Constraints: 1 ≤ N ≤ 15 1 ≤ R, C ≤ 50 1 ≤ length of Word ≤ 60 All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z'
class Solution: def wordBoggle(self, board, dictionary): def f(i, j, n, m, ind, word, board, vis): if i < 0 or i >= n or j < 0 or j >= m or vis[i][j]: return False if board[i][j] == word[ind]: if ind == len(word) - 1: return True vis[i][j] = 1 if ( f(i + 1, j, n, m, ind + 1, word, board, vis) or f(i - 1, j, n, m, ind + 1, word, board, vis) or f(i, j + 1, n, m, ind + 1, word, board, vis) or f(i, j - 1, n, m, ind + 1, word, board, vis) or f(i + 1, j + 1, n, m, ind + 1, word, board, vis) or f(i + 1, j - 1, n, m, ind + 1, word, board, vis) or f(i - 1, j + 1, n, m, ind + 1, word, board, vis) or f(i - 1, j - 1, n, m, ind + 1, word, board, vis) ): return True vis[i][j] = 0 return False n = len(board) m = len(board[0]) ans = set() for ele in dictionary: flag = 0 for i in range(n): if flag: break for j in range(m): if board[i][j] == ele[0]: vis = [([0] * m) for _ in range(n)] if f(i, j, n, m, 0, ele, board, vis): ans.add(ele) flag = 1 break return list(ans)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER RETURN FUNC_CALL VAR VAR
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell. Example 1: Input: N = 1 dictionary = {"CAT"} R = 3, C = 3 board = {{C,A,P},{A,N,D},{T,I,E}} Output: CAT Explanation: C A P A N D T I E Words we got is denoted using same color. Example 2: Input: N = 4 dictionary = {"GEEKS","FOR","QUIZ","GO"} R = 3, C = 3 board = {{G,I,Z},{U,E,K},{Q,S,E}} Output: GEEKS QUIZ Explanation: G I Z U E K Q S E Words we got is denoted using same color. Your task: You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary). Expected Time Complexity: O(4^(N^2)) Expected Auxiliary Space: O(N^2) Constraints: 1 ≤ N ≤ 15 1 ≤ R, C ≤ 50 1 ≤ length of Word ≤ 60 All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z'
class Solution: def wordBoggle(self, g, d): r = len(g) c = len(g[0]) dx = [-1, -1, -1, 0, 0, 1, 1, 1] dy = [-1, 0, 1, -1, 1, -1, 0, 1] def isSafe(x, y): return x >= 0 and y >= 0 and x < r and y < c def bfs(x, y, k, s): q = deque() n = len(s) q.append((x, y, k)) vis = [[(False) for _ in range(c)] for _ in range(r)] vis[x][y] = True while q: i, j, k = q.popleft() k += 1 if k >= n: return True for z in range(8): x = i + dx[z] y = j + dy[z] if ( isSafe(x, y) and vis[x][y] == False and k < n and g[x][y] == s[k] ): q.append((x, y, k)) vis[x][y] = True return False def dfs(x, y, level, s): n = len(s) if level == n: return True if not isSafe(x, y): return False if g[x][y] == s[level]: t = g[x][y] g[x][y] = "#" res = ( dfs(x - 1, y, level + 1, s) or dfs(x + 1, y, level + 1, s) or dfs(x, y - 1, level + 1, s) or dfs(x, y + 1, level + 1, s) or dfs(x - 1, y + 1, level + 1, s) or dfs(x + 1, y + 1, level + 1, s) or dfs(x + 1, y - 1, level + 1, s) or dfs(x - 1, y - 1, level + 1, s) ) g[x][y] = t return res else: return False ans = [] d1 = {} for k in d: d1[k] = False for i in range(r): for j in range(c): for k in d: if d1[k] == False and g[i][j] == k[0]: if dfs(i, j, 0, k): ans.append(k) d1[k] = True ans.sort() return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF RETURN VAR NUMBER VAR NUMBER VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN VAR RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
from itertools import permutations class Solution: def find_permutation(self, S): l = [] for i in permutations(S): if "".join(i) not in l: l.append("".join(i)) return sorted(l)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN FUNC_CALL VAR VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
from itertools import permutations class Solution: def find_permutation(self, S): data = list(set(permutations(S))) for i in range(len(data)): data[i] = "".join(data[i]) return sorted(data)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL STRING VAR VAR RETURN FUNC_CALL VAR VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
from itertools import permutations class Solution: def find_permutation(self, S): lst = [] all_pm = permutations(S) for pm in all_pm: lst.append("".join(pm)) return sorted(list(set(lst)))
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
res = [] def pl(s): global res r = "" for i in s: r += i res += [r] def swap(l, a, b): t = l[a] l[a] = l[b] l[b] = t def perm(s, l, r): if l == r: pl(s) else: for i in range(l, r + 1): swap(s, l, i) perm(s, l + 1, r) swap(s, l, i) class Solution: def find_permutation(self, s): r = [] global res perm(list(s), 0, len(s) - 1) res.sort() for i in res: if i not in r: r += [i] res = [] return r
ASSIGN VAR LIST FUNC_DEF ASSIGN VAR STRING FOR VAR VAR VAR VAR VAR LIST VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR LIST VAR ASSIGN VAR LIST RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
class Solution: def find_permutation(self, S): n = len(S) def solve(ind, ds, vis): ds.append(S[ind]) vis[ind] = 1 if len(ds) == n: perm.append("".join(ds)) for c in range(len(vis)): if vis[c] == 0: solve(c, ds, vis) vis[ind] = 0 ds.pop() vis = [0] * n ds = [] perm = [] for i in range(n): solve(i, ds, vis) perm = list(set(perm)) perm.sort() return perm
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
class Solution: def find_permutation(self, S): S = list(S) S.sort() ans = [] def solve(temp, s): if len(temp) == len(S): if "".join(temp) not in ans: ans.append("".join(temp)) return for i in range(len(s)): temp.append(s[i]) solve(temp, s[:i] + s[i + 1 :]) temp.pop() solve([], S) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
class Solution: def find_permutation(self, S): k = [] l = 0 r = len(S) S = list(S) def find(S, l, r, k): if l == r: k.append("".join(S)) else: for i in range(l, r): S[l], S[i] = S[i], S[l] find(S, l + 1, r, k) S[i], S[l] = S[l], S[i] find(S, l, r, k) k = set(k) k = list(k) k.sort() return k
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
class Solution: def find_permutation(self, S): if len(S) == 1: return [S[0]] ch = S[0] ss = S[1:] rr = self.find_permutation(ss) mr = [] for ele in rr: for i in range(len(ele) + 1): mr.append(ele[0:i] + ch + ele[i:]) my_list = list(set(mr)) sorted_list = sorted(my_list, key=lambda x: x[:2]) return sorted_list
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN LIST VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
from itertools import permutations class Solution: def find_permutation(self, s): p = permutations(s) l = [] for i in p: r = "" for j in i: r += j l.append(r) l.sort() c = [] m = [] for i in l: if i not in c: c.append(i) return c
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR STRING FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
from itertools import permutations class Solution: def find_permutation(self, S): res = permutations(S) ans = set() for i in res: temp = "" for j in i: temp += j ans.add(temp) return sorted(ans)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR STRING FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
class Solution: def find_permutation(self, S): op = "" arr = [] vector = [False] * len(S) self.solve(arr, op, vector, S) arr.sort() return arr def solve(self, arr, op, vector, s): if len(op) == len(s): if op not in arr: arr.append(op) return for i in range(len(s)): if vector[i] == False: vector[i] = True op += s[i] self.solve(arr, op, vector, s) op = op.replace(op[-1], "") vector[i] = False
CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER STRING ASSIGN VAR VAR NUMBER
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
import itertools class Solution: def find_permutation(self, S): return sorted(set(["".join(each) for each in itertools.permutations(S)])) ans = [] S = list(S) if len(S) == 1: return S for i in range(len(S)): temp = S[i] remaining = S[:i] + S[i + 1 :] perms = self.find_permutation(remaining) for each in perms: ans.append(temp + each) return sorted(set(ans))
IMPORT CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
from itertools import permutations class Solution: def find_permutation(self, S): a = ["".join(i) for i in list(permutations(S))] b = [] for i in a: if i not in b: b.append(i) b = sorted(b) return b
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL STRING VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
class Solution: def find_permutation(self, s): list1 = [] def dfs(s, temp, list1): if len(s) == 0: if temp not in list1: list1.append(temp) return for i in range(len(s)): dfs(s[:i] + s[i + 1 :], temp + s[i], list1) dfs(s, "", list1) return sorted(list1)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING VAR RETURN FUNC_CALL VAR VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
class Solution: def permutation(self, result, arr, start): if start == len(arr) - 1: s = "".join(arr) result.append(s) return for i in range(start, len(S)): arr[start], arr[i] = arr[i], arr[start] self.permutation(result, arr, start + 1) arr[start], arr[i] = arr[i], arr[start] return def find_permutation(self, S): result = [] arr = list(map(str, S)) self.permutation(result, arr, 0) set_result = set(result) f_result = list(set_result) f_result.sort() return f_result
CLASS_DEF FUNC_DEF IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
class Solution: def find_permutation(self, S): def seq(S, ans, i): if len(S) == i: ans.append("".join(S)) for j in range(i, len(S)): S[i], S[j] = S[j], S[i] seq(S, ans, i + 1) S[i], S[j] = S[j], S[i] ans = [] S = list(S) seq(S, ans, 0) ans = list(set(ans)) ans.sort() return ans
CLASS_DEF FUNC_DEF FUNC_DEF IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
class Solution: def toString(self, List): return "".join(List) def backtracking(self, arr, l, r, vec): if l == r: if self.toString(arr) not in vec: vec.append(self.toString(arr)) else: for i in range(l, r): arr[l], arr[i] = arr[i], arr[l] self.backtracking(arr, l + 1, r, vec) arr[l], arr[i] = arr[i], arr[l] return True def find_permutation(self, S): n = len(S) arr = list(S) vec = [] self.backtracking(arr, 0, n, vec) return sorted(vec)
CLASS_DEF FUNC_DEF RETURN FUNC_CALL STRING VAR FUNC_DEF IF VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
import itertools class Solution: def find_permutation(self, S): permutations = itertools.permutations(S) return sorted(list(dict.fromkeys(list("".join(i) for i in list(permutations)))))
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR VAR FUNC_CALL VAR VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
import itertools class Solution: def find_permutation(self, S): l = [] perms = set(itertools.permutations(S)) sorted_perms = sorted(perms) for perm in sorted_perms: s = "".join(perm) l.append(s) return l
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
from itertools import permutations class Solution: def find_permutation(self, S): s = permutations(S) ans = [] for i in s: l = "" for j in i: l = l + j ans.append(l) g = set(ans) h = list(g) h.sort() return h
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
class Solution: def solve(self, s, ans, res): if len(s) == 0: if ans not in res: res.append(ans) return n = len(s) for i in range(n): ch = s[i] left = s[0:i] right = s[i + 1 :] rest = left + right self.solve(rest, ans + ch, res) def find_permutation(self, S): res = [] ans = "" self.solve(S, ans, res) res.sort() return res
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
class Solution: def find_permutation(self, s): result = [] list_s = sorted(list(s)) permutation(list_s, 0, result) return sorted(result) def permutation(s, index, result): if index == len(s): result.append("".join(s[:])) return for i in range(index, len(s)): if i != index and s[i] == s[i - 1]: continue s[i], s[index] = s[index], s[i] permutation(s, index + 1, result) s[i], s[index] = s[index], s[i]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR RETURN FUNC_CALL VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
class Solution: def toString(self, List): return "".join(List) def permute(self, a, l, r, words): if l == r: words.add(self.toString(a)) return else: for i in range(l, r): a[l], a[i] = a[i], a[l] self.permute(a, l + 1, r, words) a[l], a[i] = a[i], a[l] def find_permutation(self, S): words = set() words2 = [] n = len(S) a = list(S) self.permute(a, 0, n, words) for x in words: words2.append(x) words2.sort() return words2
CLASS_DEF FUNC_DEF RETURN FUNC_CALL STRING VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
from itertools import permutations class Solution: def find_permutation(self, S): lst = [] for i in permutations(S): lst.append("".join(i)) return sorted(set(lst))
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
import itertools class Solution: def find_permutation(self, S): a = set() p = itertools.permutations(S, len(S)) for i in p: a.add("".join(i)) a = sorted(a) return a
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
from itertools import permutations class Solution: def find_permutation(self, S): res = [] def permute(a, l, r): if l == r: k = "".join(a) if k not in res: res.append(k) else: for i in range(l, r): a[l], a[i] = a[i], a[l] permute(a, l + 1, r) a[l], a[i] = a[i], a[l] return res p = [i for i in S] result = permute(p, 0, len(S)) return sorted(result)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
from itertools import permutations class Solution: def find_permutation(self, s): ans = permutations(s) ans = list(ans) l = [] for i in ans: k = "".join(i) l = l + [k] l = set(l) l = list(l) l.sort() return l
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR BIN_OP VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
from itertools import permutations class Solution: def find_permutation(self, S): l = [] li = permutations(S) for i in li: l.append("".join(i)) l = set(l) l = list(l) l.sort() return l
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
import itertools class Solution: def find_permutation(self, S): ans = [] x = list(itertools.permutations(S, len(S))) for i in x: temp = "" for j in range(0, len(i)): temp += i[j] ans.append(temp) final_ans = set(ans) sorted_ans = list(final_ans) sorted_ans.sort() return sorted_ans
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
class Solution: def solve(self, arr, ind, n, res): if ind == n: temp = "".join(arr) if temp not in res: res.append(temp) for i in range(ind, n): arr[i], arr[ind] = arr[ind], arr[i] self.solve(arr, ind + 1, n, res) arr[i], arr[ind] = arr[ind], arr[i] def find_permutation(self, S): n = len(S) arr = list(S) res = [] self.solve(arr, 0, n, res) res.sort() return res
CLASS_DEF FUNC_DEF IF VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
import itertools class Solution: def find_permutation(self, S): S = sorted(S) unq_perm = [] permutations = list(itertools.permutations(S)) permutations = ["".join(i) for i in permutations] for i in permutations: if i not in unq_perm: unq_perm.append(i) return unq_perm
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING VAR VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
class Solution: def find_permutation(self, s): res = [] def permutation(s, i): if i == len(s): if s not in res: res.append(s) elif s not in res: for j in range(0, len(s)): w = [w for w in s] w[j], w[i] = w[i], w[j] word = "".join(w) permutation(word, i + 1) permutation(s, 0) res.sort() return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
class Solution: def find_permutation(self, S): S = list(S) res = set() perm = [] used = [(False) for i in range(len(S))] def backtrack(): if len(perm) == len(S): res.add("".join(perm)) return for i in range(len(used)): if used[i]: continue used[i] = True perm.append(S[i]) backtrack() used[i] = False perm.pop() backtrack() return sorted(list(res))
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
class Solution: def find_permutation(self, S): l = [] a = [] f = [0] * len(S) def x(s, l, a, f): if len(l) == len(s): y = "".join(l) if y not in a: a.append(y) return for i in range(len(s)): if not f[i]: f[i] = 1 l.append(s[i]) x(s, l, a, f) f[i] = 0 l.pop() a.sort() return a return x(S, l, a, f)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
class Solution: def find_permutation(self, s): d = {} for i in s: if i in d: d[i] += 1 else: d[i] = 1 res = [] l = len(s) def sol(ind, str, d): if ind == l: res.append(str) return for i in d: if d[i] > 0: str += i d[i] -= 1 sol(ind + 1, str, d) str = str[:-1] d[i] += 1 sol(0, "", d) res = sorted(res) return res
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
from itertools import permutations class Solution: def find_permutation(self, S): P = permutations(S) Output = [] for i in P: Ans = "".join(i) Output.append(Ans) Output = list(set(Output)) Output.sort() return Output
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
from itertools import permutations class Solution: def find_permutation(self, S): n = len(S) res = permutations(S) r = [] for i in res: k = "".join(i) if k not in r: r.append(k) return sorted(r)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
class Solution: def find_permutation(self, S): result = [] def generate_permutations(current_permutation, used): if len(current_permutation) == len(S): result.append(current_permutation) return for i in range(len(S)): if used[i]: continue if i > 0 and S[i] == S[i - 1] and not used[i - 1]: continue used[i] = True generate_permutations(current_permutation + S[i], used) used[i] = False S = "".join(sorted(S)) generate_permutations("", [False] * len(S)) return result
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING BIN_OP LIST NUMBER FUNC_CALL VAR VAR RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
from itertools import permutations class Solution: def find_permutation(self, S): b = [] from itertools import permutations perms = permutations(S) perms = sorted(perms) dic = {} for i in perms: x = "".join(i) if x not in dic: b.append(x) dic[x] = 1 return b
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
from itertools import permutations class Solution: def find_permutation(self, S): p = list(permutations(S)) l = [] for i in p: s = "" for j in i: s += j if s not in l: l.append(s) return sorted(l)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR STRING FOR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
class Solution: def find_permutation(self, S): self.ans = [] S = list(S) def recursive(index): if index == len(S) - 1: temp = S[:] self.ans.append("".join(temp)) return uset = set() for i in range(index, len(S)): if S[i] not in uset: uset.add(S[i]) S[i], S[index] = S[index], S[i] recursive(index + 1) S[i], S[index] = S[index], S[i] recursive(0) return sorted(self.ans)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN FUNC_CALL VAR VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
class Solution: def find_permutation(self, S): i = 0 res = [] arr = [i for i in S] self.func(i, res, arr) f_ans = list(set(res)) f_ans.sort() return f_ans def func(self, i, res, arr): if i == len(arr) - 1: res.append("".join(arr)) return for j in range(i, len(arr)): arr[i], arr[j] = arr[j], arr[i] self.func(i + 1, res, arr) arr[i], arr[j] = arr[j], arr[i]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR FUNC_DEF IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
class Solution: def permutation_rec(self, rest_letters): if len(rest_letters) == 1: return rest_letters[0] new_ends = [] for i in range(len(rest_letters)): letter = rest_letters[i] new_rest_letters = rest_letters.copy() new_rest_letters.remove(letter) ends = self.permutation_rec(new_rest_letters) for end in ends: new_ends.append(letter + end) return new_ends def find_permutation(self, S): letters = [] for i in range(len(S)): letters.append(S[i]) permutations = self.permutation_rec(letters) unique_perm = [] for perm in permutations: if perm not in unique_perm: unique_perm.append(perm) unique_perm.sort() return unique_perm
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
class Solution: def find_permutation(self, S): def Permutaion(ip, op): if len(ip) == 0: if op not in self.ans: self.ans.append(op) return for i in range(len(ip)): req_ip = ip[:i] + ip[i + 1 :] Permutaion(req_ip, op + ip[i]) self.ans = [] Permutaion(S, "") return sorted(self.ans)
CLASS_DEF FUNC_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR STRING RETURN FUNC_CALL VAR VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
class Solution: def find_permutation(self, S): def permute(arr, ds, ans, start): if len(arr) - 1 == start: ans.append("".join(list(arr))) return for i in range(len(arr)): arr[start], arr[i] = arr[i], arr[start] permute(arr, ds, ans, start + 1) arr[i], arr[start] = arr[start], arr[i] return ans = [] ds = [] mp = {} arr = [] for i in range(len(S)): arr.append(S[i]) permute(arr, ds, ans, 0) ans = list(set(ans)) ans = sorted(ans) return ans
CLASS_DEF FUNC_DEF FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
class Solution: def helper(self, s, l, h, res): if l == h: res.append("".join(s)) return for i in range(l, h): s[i], s[l] = s[l], s[i] self.helper(s, l + 1, h, res) s[l], s[i] = s[i], s[l] def find_permutation(self, S): res = [] self.helper(list(S), 0, len(S), res) res = list(set(res)) res.sort() return res
CLASS_DEF FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
from itertools import permutations class Solution: def find_permutation(self, S): premList = permutations(S) l = set() for prem in list(premList): l.add("".join(prem)) l = list(l) l.sort() return l
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
class Solution: def recur(self, S, index, res, path): if index == len(S): res.append(S[:]) return else: for i in range(index, len(S)): S[i], S[index] = S[index], S[i] self.recur(S, index + 1, res, path) S[i], S[index] = S[index], S[i] def find_permutation(self, S): res = [] path = [] L = list(S) self.recur(L, 0, res, path) ans = [] for i in res: ans.append("".join(i)) final = list(set(ans)) final.sort() return final
CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
class Solution: def find_permutation(self, S): def backtrack(first=0, curr=[]): if first == len(S): result.add("".join(curr)) return for i in range(first, len(S)): S[i], S[first] = S[first], S[i] backtrack(first + 1, curr + [S[first]]) S[i], S[first] = S[first], S[i] result = set() S = [char for char in S] backtrack() return sorted(result)
CLASS_DEF FUNC_DEF FUNC_DEF NUMBER LIST IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
class Solution: def ansd(self, i, S, ans): con = len(S) if i >= con: if S not in ans: ans.append(S) return ans else: st = i while st < con: os = S if st != i: temp1 = os[i] temp2 = os[st] swaped = os[:i] + temp2 + os[i + 1 : st] + temp1 + os[st + 1 :] self.ansd(i + 1, swaped, ans) else: self.ansd(i + 1, S, ans) st = st + 1 return ans def find_permutation(self, S): k = [] ans = self.ansd(0, S, k) ans.sort() return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
class Solution: def find_permutation(self, S): result = set() def helper(string, slate): if not string: result.add(slate) return for i in range(len(string)): helper(string[:i] + string[i + 1 :], slate + string[i]) helper(S, "") return list(sorted(result))
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING RETURN FUNC_CALL VAR FUNC_CALL VAR VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
from itertools import permutations class Solution: def find_permutation(self, S): from itertools import permutations result = set(permutations(S)) result = sorted(["".join(p) for p in result]) return result
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
class Solution: def find_permutation(self, string): chars = list(string) chars.sort() permutations = [] visited = [False] * len(chars) self.backtrack(chars, visited, [], permutations) return permutations def backtrack(self, chars, visited, current, permutations): if len(current) == len(chars): permutations.append("".join(current)) return for i in range(len(chars)): if visited[i] or i > 0 and chars[i] == chars[i - 1] and not visited[i - 1]: continue visited[i] = True current.append(chars[i]) self.backtrack(chars, visited, current, permutations) current.pop() visited[i] = False
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR LIST VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
class Solution: def find_permutation(self, S): perm_set = set() def generate_permutations(s, l, r): if l == r: perm_set.add(s) else: for i in range(l, r + 1): s_list = list(s) s_list[l], s_list[i] = s_list[i], s_list[l] s_new = "".join(s_list) generate_permutations(s_new, l + 1, r) s_list[l], s_list[i] = s_list[i], s_list[l] generate_permutations(S, 0, len(S) - 1) perm_list = sorted(list(perm_set)) return perm_list
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
class Solution: def find_permutation(self, S): if len(S) == 0: arr = [] arr.append("") return arr output = [] for i in range(len(S)): smallOut = self.find_permutation(S[0:i] + S[i + 1 :]) for j in smallOut: output.append(S[i] + j) output2 = [] for i in output: if i in output2: pass else: output2.append(i) return sorted(output2)
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR STRING RETURN VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
class Solution: def find_permutation(self, s): n = len(s) perms = set() def permute(start, s_list): if start == n: perms.add("".join(s_list)) return for i in range(start, n): s_list[start], s_list[i] = s_list[i], s_list[start] permute(start + 1, s_list) s_list[start], s_list[i] = s_list[i], s_list[start] s_list = list(s) permute(0, s_list) result = list(perms) result.sort() return result
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
from itertools import permutations class Solution: def find_permutation(self, S): perms = ["".join(p) for p in permutations(S)] for i in perms: if perms.count(i) > 1: perms.pop() return sorted(set(perms))
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL STRING VAR VAR FUNC_CALL VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
class Solution: def find_permutation(self, S): def backtrack(start): if start == len(S) - 1: permutations.add("".join(S)) return for i in range(start, len(S)): S[start], S[i] = S[i], S[start] backtrack(start + 1) S[start], S[i] = S[i], S[start] S = list(S) permutations = set() backtrack(0) permutations = sorted(list(permutations)) return permutations
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
from itertools import permutations class Solution: def find_permutation(self, S): ans = list(permutations(S)) ans.sort() res = [] for i in ans: str = "".join(i) if str not in res: res.append(str) return res
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
import itertools as it class Solution: def find_permutation(self, S): import itertools as it x = list(it.permutations(S)) a = [] for i in x: if "".join(i) not in a: a.append("".join(i)) s = sorted(a) return s
IMPORT CLASS_DEF FUNC_DEF IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
class Solution: def permute(self, s, temp, ans): if len(s) == 0: ans.append(temp) return for i, char in enumerate(s): if i > 0 and char == s[i - 1]: continue temp += char self.permute(s[:i] + s[i + 1 :], temp, ans) temp = temp[:-1] def find_permutation(self, s): ans = [] self.permute(sorted(s), "", ans) return ans
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING VAR RETURN VAR
Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order. Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations. Your Task: You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order. Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n! * n) Constraints: 1 <= length of string <= 5
class Solution: def __init__(self): self.ans = [] def find(self, s, i): if i == len(s): self.ans.append(s) else: for j in range(i, len(s), 1): l = [] for k in s: l.append(k) temp = l[j] l[j] = l[i] l[i] = temp self.find("".join(l), i + 1) def find_permutation(self, S): self.find(S, 0) return sorted(list(set(self.ans)))
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR BIN_OP VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given an array $a$ consisting of $500000$ integers (numbered from $1$ to $500000$). Initially all elements of $a$ are zero. You have to process two types of queries to this array: $1$ $x$ $y$ — increase $a_x$ by $y$; $2$ $x$ $y$ — compute $\sum\limits_{i \in R(x, y)} a_i$, where $R(x, y)$ is the set of all integers from $1$ to $500000$ which have remainder $y$ modulo $x$. Can you process all the queries? -----Input----- The first line contains one integer $q$ ($1 \le q \le 500000$) — the number of queries. Then $q$ lines follow, each describing a query. The $i$-th line contains three integers $t_i$, $x_i$ and $y_i$ ($1 \le t_i \le 2$). If $t_i = 1$, then it is a query of the first type, $1 \le x_i \le 500000$, and $-1000 \le y_i \le 1000$. If $t_i = 2$, then it it a query of the second type, $1 \le x_i \le 500000$, and $0 \le y_i < x_i$. It is guaranteed that there will be at least one query of type $2$. -----Output----- For each query of type $2$ print one integer — the answer to it. -----Example----- Input 5 1 3 4 2 3 0 2 4 3 1 4 -4 2 1 0 Output 4 4 0
import sys readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines ns = lambda: readline().rstrip() ni = lambda: int(readline().rstrip()) nm = lambda: map(int, readline().split()) nl = lambda: list(map(int, readline().split())) prn = lambda x: print(*x, sep="\n") def solve(): q = ni() n = 5 * 10**5 a = [0] * (n + 1) m = 720 rem = [([0] * m) for _ in range(m)] for _ in range(q): t, x, y = nm() if t == 1: a[x] += y for p in range(1, m): rem[p][x % p] += y elif x < m: print(rem[x][y]) else: print(sum(a[i] for i in range(y, n + 1, x))) solve()
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR IF VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR
You are given an array $a$ consisting of $500000$ integers (numbered from $1$ to $500000$). Initially all elements of $a$ are zero. You have to process two types of queries to this array: $1$ $x$ $y$ — increase $a_x$ by $y$; $2$ $x$ $y$ — compute $\sum\limits_{i \in R(x, y)} a_i$, where $R(x, y)$ is the set of all integers from $1$ to $500000$ which have remainder $y$ modulo $x$. Can you process all the queries? -----Input----- The first line contains one integer $q$ ($1 \le q \le 500000$) — the number of queries. Then $q$ lines follow, each describing a query. The $i$-th line contains three integers $t_i$, $x_i$ and $y_i$ ($1 \le t_i \le 2$). If $t_i = 1$, then it is a query of the first type, $1 \le x_i \le 500000$, and $-1000 \le y_i \le 1000$. If $t_i = 2$, then it it a query of the second type, $1 \le x_i \le 500000$, and $0 \le y_i < x_i$. It is guaranteed that there will be at least one query of type $2$. -----Output----- For each query of type $2$ print one integer — the answer to it. -----Example----- Input 5 1 3 4 2 3 0 2 4 3 1 4 -4 2 1 0 Output 4 4 0
import sys readline = sys.stdin.buffer.readline ni = lambda: int(readline().rstrip()) nm = lambda: map(int, readline().split()) def solve(): q = ni() n = 5 * 10**5 a = [0] * (n + 1) m = 710 rem = [([0] * m) for _ in range(m)] for _ in range(q): t, x, y = nm() if t == 1: a[x] += y for p in range(1, m): rem[p][x % p] += y elif x < m: print(rem[x][y]) else: print(sum(a[y::x])) solve()
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR IF VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
You are given an array $a$ consisting of $500000$ integers (numbered from $1$ to $500000$). Initially all elements of $a$ are zero. You have to process two types of queries to this array: $1$ $x$ $y$ — increase $a_x$ by $y$; $2$ $x$ $y$ — compute $\sum\limits_{i \in R(x, y)} a_i$, where $R(x, y)$ is the set of all integers from $1$ to $500000$ which have remainder $y$ modulo $x$. Can you process all the queries? -----Input----- The first line contains one integer $q$ ($1 \le q \le 500000$) — the number of queries. Then $q$ lines follow, each describing a query. The $i$-th line contains three integers $t_i$, $x_i$ and $y_i$ ($1 \le t_i \le 2$). If $t_i = 1$, then it is a query of the first type, $1 \le x_i \le 500000$, and $-1000 \le y_i \le 1000$. If $t_i = 2$, then it it a query of the second type, $1 \le x_i \le 500000$, and $0 \le y_i < x_i$. It is guaranteed that there will be at least one query of type $2$. -----Output----- For each query of type $2$ print one integer — the answer to it. -----Example----- Input 5 1 3 4 2 3 0 2 4 3 1 4 -4 2 1 0 Output 4 4 0
import sys n = 500001 sqrt = int(0.75 * n**0.5) data = [0] * n ans = [[]] for i in range(1, sqrt): ans.append([0] * i) j = int(sys.stdin.readline()) qus = sys.stdin.readlines() for qu in qus: q = [int(i) for i in qu.split()] if q[0] == 1: x = q[1] y = q[2] data[x] += y for i in range(1, sqrt): ans[i][x % i] += y else: if q[1] < sqrt: sys.stdout.write(str(ans[q[1]][q[2]])) else: sm = 0 for i in range(q[2], n, q[1]): sm += data[i] sys.stdout.write(str(sm)) sys.stdout.write("\n")
IMPORT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR IF VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Given an incomplete Sudoku configuration in terms of a 9 x 9 2-D square matrix (grid[][]), the task to find a solved Sudoku. For simplicity, you may assume that there will be only one unique solution. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. Sample Sudoku for you to get the logic for its solution: Example 1: Input: grid[][] = [[3 0 6 5 0 8 4 0 0], [5 2 0 0 0 0 0 0 0], [0 8 7 0 0 0 0 3 1 ], [0 0 3 0 1 0 0 8 0], [9 0 0 8 6 3 0 0 5], [0 5 0 0 9 0 6 0 0], [1 3 0 0 0 0 2 5 0], [0 0 0 0 0 0 0 7 4], [0 0 5 2 0 6 3 0 0]] Output: 3 1 6 5 7 8 4 9 2 5 2 9 1 3 4 7 6 8 4 8 7 6 2 9 5 3 1 2 6 3 4 1 5 9 8 7 9 7 4 8 6 3 1 2 5 8 5 1 7 9 2 6 4 3 1 3 8 9 4 7 2 5 6 6 9 2 3 5 1 8 7 4 7 4 5 2 8 6 3 1 9 Your Task: You need to complete the two functions: SolveSudoku(): Takes a grid as its argument and returns true if a solution is possible and false if it is not. printGrid(): Takes a grid as its argument and prints the 81 numbers of the solved Sudoku in a single line with space separation. NOTE: Do not give a new line character after printing the grid. It has already been taken care of in the Driver Code. Expected Time Complexity: O(9^{N*N}). Expected Auxiliary Space: O(N*N). Constraints: 0 ≤ grid[i][j] ≤ 9
def valid(grid, r, c, no): if no in grid[r]: return False if any(grid[i][c] == no for i in range(9)): return False br, bc = 3 * (r // 3), 3 * (c // 3) for i in range(br, br + 3): for j in range(bc, bc + 3): if grid[i][j] == no: return False return True def func(pos, board): r, c = pos while board[r][c] != 0: c += 1 if c == 9: c = 0 r += 1 if r == 9: return True for i in range(1, 10): if valid(board, r, c, i): board[r][c] = i if func((r, c), board): return True board[r][c] = 0 return False class Solution: def SolveSudoku(self, grid): return func((0, 0), grid) def printGrid(self, arr): for i in arr: for j in i: print(j, end=" ")
FUNC_DEF IF VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER RETURN NUMBER ASSIGN VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR VAR WHILE VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR NUMBER NUMBER VAR FUNC_DEF FOR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Given an incomplete Sudoku configuration in terms of a 9 x 9 2-D square matrix (grid[][]), the task to find a solved Sudoku. For simplicity, you may assume that there will be only one unique solution. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. Sample Sudoku for you to get the logic for its solution: Example 1: Input: grid[][] = [[3 0 6 5 0 8 4 0 0], [5 2 0 0 0 0 0 0 0], [0 8 7 0 0 0 0 3 1 ], [0 0 3 0 1 0 0 8 0], [9 0 0 8 6 3 0 0 5], [0 5 0 0 9 0 6 0 0], [1 3 0 0 0 0 2 5 0], [0 0 0 0 0 0 0 7 4], [0 0 5 2 0 6 3 0 0]] Output: 3 1 6 5 7 8 4 9 2 5 2 9 1 3 4 7 6 8 4 8 7 6 2 9 5 3 1 2 6 3 4 1 5 9 8 7 9 7 4 8 6 3 1 2 5 8 5 1 7 9 2 6 4 3 1 3 8 9 4 7 2 5 6 6 9 2 3 5 1 8 7 4 7 4 5 2 8 6 3 1 9 Your Task: You need to complete the two functions: SolveSudoku(): Takes a grid as its argument and returns true if a solution is possible and false if it is not. printGrid(): Takes a grid as its argument and prints the 81 numbers of the solved Sudoku in a single line with space separation. NOTE: Do not give a new line character after printing the grid. It has already been taken care of in the Driver Code. Expected Time Complexity: O(9^{N*N}). Expected Auxiliary Space: O(N*N). Constraints: 0 ≤ grid[i][j] ≤ 9
class Solution: def SolveSudoku(self, grid): def issafe(grid, row, col, val): for i in range(9): if grid[row][i] == val: return False if grid[i][col] == val: return False if grid[3 * (row // 3) + i // 3][3 * (col // 3) + i % 3] == val: return False return True for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] == 0: for k in range(1, len(grid) + 1): if issafe(grid, i, j, k): grid[i][j] = k if self.SolveSudoku(grid): return True grid[i][j] = 0 return False return True def printGrid(self, arr): for i in range(len(arr)): for j in range(len(arr)): print(arr[i][j], end=" ")
CLASS_DEF FUNC_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING
Given an incomplete Sudoku configuration in terms of a 9 x 9 2-D square matrix (grid[][]), the task to find a solved Sudoku. For simplicity, you may assume that there will be only one unique solution. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. Sample Sudoku for you to get the logic for its solution: Example 1: Input: grid[][] = [[3 0 6 5 0 8 4 0 0], [5 2 0 0 0 0 0 0 0], [0 8 7 0 0 0 0 3 1 ], [0 0 3 0 1 0 0 8 0], [9 0 0 8 6 3 0 0 5], [0 5 0 0 9 0 6 0 0], [1 3 0 0 0 0 2 5 0], [0 0 0 0 0 0 0 7 4], [0 0 5 2 0 6 3 0 0]] Output: 3 1 6 5 7 8 4 9 2 5 2 9 1 3 4 7 6 8 4 8 7 6 2 9 5 3 1 2 6 3 4 1 5 9 8 7 9 7 4 8 6 3 1 2 5 8 5 1 7 9 2 6 4 3 1 3 8 9 4 7 2 5 6 6 9 2 3 5 1 8 7 4 7 4 5 2 8 6 3 1 9 Your Task: You need to complete the two functions: SolveSudoku(): Takes a grid as its argument and returns true if a solution is possible and false if it is not. printGrid(): Takes a grid as its argument and prints the 81 numbers of the solved Sudoku in a single line with space separation. NOTE: Do not give a new line character after printing the grid. It has already been taken care of in the Driver Code. Expected Time Complexity: O(9^{N*N}). Expected Auxiliary Space: O(N*N). Constraints: 0 ≤ grid[i][j] ≤ 9
class Solution: ans = 0 def issafe(self, grid, x, coord): for i in range(9): if grid[i][coord[1]] == x: return 0 for i in range(9): if grid[coord[0]][i] == x: return 0 l = coord[0] // 3 r = coord[1] // 3 for i in range(3 * l, 3 * (l + 1)): for j in range(3 * r, 3 * (r + 1)): if grid[i][j] == x: return 0 return 1 def gen(self, grid, space, i): if i == len(space): ans = 1 return 1 for k in range(1, 10): if self.issafe(grid, k, space[i]): grid[space[i][0]][space[i][1]] = k if self.gen(grid, space, i + 1): return 1 grid[space[i][0]][space[i][1]] = 0 def SolveSudoku(self, grid): space = [] for i in range(9): for j in range(9): if grid[i][j] == 0: space.append([i, j]) return self.gen(grid, space, 0) def printGrid(self, arr): for i in range(9): for j in range(9): print(arr[i][j], end=" ")
CLASS_DEF ASSIGN VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR RETURN FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR STRING
Given an incomplete Sudoku configuration in terms of a 9 x 9 2-D square matrix (grid[][]), the task to find a solved Sudoku. For simplicity, you may assume that there will be only one unique solution. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. Sample Sudoku for you to get the logic for its solution: Example 1: Input: grid[][] = [[3 0 6 5 0 8 4 0 0], [5 2 0 0 0 0 0 0 0], [0 8 7 0 0 0 0 3 1 ], [0 0 3 0 1 0 0 8 0], [9 0 0 8 6 3 0 0 5], [0 5 0 0 9 0 6 0 0], [1 3 0 0 0 0 2 5 0], [0 0 0 0 0 0 0 7 4], [0 0 5 2 0 6 3 0 0]] Output: 3 1 6 5 7 8 4 9 2 5 2 9 1 3 4 7 6 8 4 8 7 6 2 9 5 3 1 2 6 3 4 1 5 9 8 7 9 7 4 8 6 3 1 2 5 8 5 1 7 9 2 6 4 3 1 3 8 9 4 7 2 5 6 6 9 2 3 5 1 8 7 4 7 4 5 2 8 6 3 1 9 Your Task: You need to complete the two functions: SolveSudoku(): Takes a grid as its argument and returns true if a solution is possible and false if it is not. printGrid(): Takes a grid as its argument and prints the 81 numbers of the solved Sudoku in a single line with space separation. NOTE: Do not give a new line character after printing the grid. It has already been taken care of in the Driver Code. Expected Time Complexity: O(9^{N*N}). Expected Auxiliary Space: O(N*N). Constraints: 0 ≤ grid[i][j] ≤ 9
class Solution: def solving(self, grid, row, col): if row == 8 and col == 9: return True if col == 9: col = 0 row += 1 if grid[row][col] > 0: return self.solving(grid, row, col + 1) for i in range(1, 10, 1): if self.isvalidnumber(grid, i, row, col): grid[row][col] = i if self.solving(grid, row, col + 1): return True grid[row][col] = 0 return False def isvalidnumber(self, grid, n, row, col): for i in range(9): if grid[row][i] == n: return False for i in range(9): if grid[i][col] == n: return False startRow = row - row % 3 startCol = col - col % 3 for i in range(3): for j in range(3): if grid[i + startRow][j + startCol] == n: return False return True def SolveSudoku(self, grid): self.solving(grid, 0, 0) return True def printGrid(self, arr): for i in range(9): for j in range(9): print(grid[i][j], end=" ")
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR STRING
Given an incomplete Sudoku configuration in terms of a 9 x 9 2-D square matrix (grid[][]), the task to find a solved Sudoku. For simplicity, you may assume that there will be only one unique solution. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. Sample Sudoku for you to get the logic for its solution: Example 1: Input: grid[][] = [[3 0 6 5 0 8 4 0 0], [5 2 0 0 0 0 0 0 0], [0 8 7 0 0 0 0 3 1 ], [0 0 3 0 1 0 0 8 0], [9 0 0 8 6 3 0 0 5], [0 5 0 0 9 0 6 0 0], [1 3 0 0 0 0 2 5 0], [0 0 0 0 0 0 0 7 4], [0 0 5 2 0 6 3 0 0]] Output: 3 1 6 5 7 8 4 9 2 5 2 9 1 3 4 7 6 8 4 8 7 6 2 9 5 3 1 2 6 3 4 1 5 9 8 7 9 7 4 8 6 3 1 2 5 8 5 1 7 9 2 6 4 3 1 3 8 9 4 7 2 5 6 6 9 2 3 5 1 8 7 4 7 4 5 2 8 6 3 1 9 Your Task: You need to complete the two functions: SolveSudoku(): Takes a grid as its argument and returns true if a solution is possible and false if it is not. printGrid(): Takes a grid as its argument and prints the 81 numbers of the solved Sudoku in a single line with space separation. NOTE: Do not give a new line character after printing the grid. It has already been taken care of in the Driver Code. Expected Time Complexity: O(9^{N*N}). Expected Auxiliary Space: O(N*N). Constraints: 0 ≤ grid[i][j] ≤ 9
class Solution: def SolveSudoku(self, grid): def fun1(r, c): for i in range(1, toggle + 1): if ( a[r][i] == 0 and b[c][i] == 0 and cbox[hash1.index([r - r % 3, c - c % 3])][i] == 0 ): grid[r][c] = i a[r][i] = 1 b[c][i] = 1 cbox[hash1.index([r - r % 3, c - c % 3])][i] = 1 if r == toggl1 and c == toggl1: return 1 if c == toggl1: z1 = fun(r + 1, 0) if z1 == 1: return 1 grid[r][c] = 0 a[r][i] = 0 b[c][i] = 0 cbox[hash1.index([r - r % 3, c - c % 3])][i] = 0 else: z1 = fun(r, c + 1) if z1 == 1: return 1 grid[r][c] = 0 a[r][i] = 0 b[c][i] = 0 cbox[hash1.index([r - r % 3, c - c % 3])][i] = 0 elif i == toggle: return def fun(k1, k2): for i in range(k1, len(grid)): for j in range(k2, len(grid)): if grid[i][j] == 0: return fun1(i, j) elif i == toggl1 and j == toggl1: return 1 if j == toggl1: k2 = 0 a = [] b = [] cbox = [] ans = [] for i in range(9): a.append([0] * 10) for i in range(9): b.append([0] * 10) for i in range(9): cbox.append([0] * 10) toggle = 9 toggl1 = 8 hash1 = [[0, 0], [0, 3], [0, 6], [3, 0], [3, 3], [3, 6], [6, 0], [6, 3], [6, 6]] for i in range(toggle): for j in range(toggle): a[i][grid[i][j]] = 1 b[j][grid[i][j]] = 1 box = hash1.index([i - i % 3, j - j % 3]) cbox[box][grid[i][j]] = 1 fun(0, 0) s = "" for i in range(len(grid)): for j in range(len(grid)): s += str(grid[i][j]) + " " if grid[i][j] == 0: print("cam") return 0 return 1 def printGrid(self, arr): s = "" for i in range(len(arr)): for j in range(len(arr)): print(arr[i][j], end=" ")
CLASS_DEF FUNC_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR FUNC_CALL VAR LIST BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR RETURN FUNC_DEF FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR STRING IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING
Given an incomplete Sudoku configuration in terms of a 9 x 9 2-D square matrix (grid[][]), the task to find a solved Sudoku. For simplicity, you may assume that there will be only one unique solution. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. Sample Sudoku for you to get the logic for its solution: Example 1: Input: grid[][] = [[3 0 6 5 0 8 4 0 0], [5 2 0 0 0 0 0 0 0], [0 8 7 0 0 0 0 3 1 ], [0 0 3 0 1 0 0 8 0], [9 0 0 8 6 3 0 0 5], [0 5 0 0 9 0 6 0 0], [1 3 0 0 0 0 2 5 0], [0 0 0 0 0 0 0 7 4], [0 0 5 2 0 6 3 0 0]] Output: 3 1 6 5 7 8 4 9 2 5 2 9 1 3 4 7 6 8 4 8 7 6 2 9 5 3 1 2 6 3 4 1 5 9 8 7 9 7 4 8 6 3 1 2 5 8 5 1 7 9 2 6 4 3 1 3 8 9 4 7 2 5 6 6 9 2 3 5 1 8 7 4 7 4 5 2 8 6 3 1 9 Your Task: You need to complete the two functions: SolveSudoku(): Takes a grid as its argument and returns true if a solution is possible and false if it is not. printGrid(): Takes a grid as its argument and prints the 81 numbers of the solved Sudoku in a single line with space separation. NOTE: Do not give a new line character after printing the grid. It has already been taken care of in the Driver Code. Expected Time Complexity: O(9^{N*N}). Expected Auxiliary Space: O(N*N). Constraints: 0 ≤ grid[i][j] ≤ 9
class Solution: def SolveSudoku(self, grid): def helper(grid): for i in range(9): for j in range(9): if grid[i][j] == 0: for inx in range(1, 10): if self.isvalid(i, j, grid, inx): grid[i][j] = inx ans = helper(grid) if ans: return True else: grid[i][j] = 0 return False return True return helper(grid) def isvalid(self, row, col, grid, inx): for i in range(9): if grid[row][i] == inx: return False if grid[i][col] == inx: return False if grid[3 * (row // 3) + i // 3][3 * (col // 3) + i % 3] == inx: return False return True def printGrid(self, arr): for ele in arr: for i in range(9): print(ele[i], end=" ") return
CLASS_DEF FUNC_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING RETURN
Given an incomplete Sudoku configuration in terms of a 9 x 9 2-D square matrix (grid[][]), the task to find a solved Sudoku. For simplicity, you may assume that there will be only one unique solution. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. Sample Sudoku for you to get the logic for its solution: Example 1: Input: grid[][] = [[3 0 6 5 0 8 4 0 0], [5 2 0 0 0 0 0 0 0], [0 8 7 0 0 0 0 3 1 ], [0 0 3 0 1 0 0 8 0], [9 0 0 8 6 3 0 0 5], [0 5 0 0 9 0 6 0 0], [1 3 0 0 0 0 2 5 0], [0 0 0 0 0 0 0 7 4], [0 0 5 2 0 6 3 0 0]] Output: 3 1 6 5 7 8 4 9 2 5 2 9 1 3 4 7 6 8 4 8 7 6 2 9 5 3 1 2 6 3 4 1 5 9 8 7 9 7 4 8 6 3 1 2 5 8 5 1 7 9 2 6 4 3 1 3 8 9 4 7 2 5 6 6 9 2 3 5 1 8 7 4 7 4 5 2 8 6 3 1 9 Your Task: You need to complete the two functions: SolveSudoku(): Takes a grid as its argument and returns true if a solution is possible and false if it is not. printGrid(): Takes a grid as its argument and prints the 81 numbers of the solved Sudoku in a single line with space separation. NOTE: Do not give a new line character after printing the grid. It has already been taken care of in the Driver Code. Expected Time Complexity: O(9^{N*N}). Expected Auxiliary Space: O(N*N). Constraints: 0 ≤ grid[i][j] ≤ 9
class Solution: def issafe(self, arr, number, row, col): for i in range(len(arr)): if arr[row][i] == number: return False if arr[i][col] == number: return False sc = col - col % 3 sr = row - row % 3 for i in range(3): for j in range(3): if arr[sr + i][sc + j] == number: return False return True def helper(self, grid, row, col): if row == 8 and col == 9: return True nrow = 0 ncol = 0 if col == len(grid): row = row + 1 col = 0 if grid[row][col] != 0: if self.helper(grid, row, col + 1): return True else: for i in range(1, 10): if self.issafe(grid, i, row, col): grid[row][col] = i if self.helper(grid, row, col + 1): return True grid[row][col] = 0 return False def SolveSudoku(self, grid): if self.helper(grid, 0, 0): return True else: return False def printGrid(self, arr): for i in range(len(arr)): for j in range(len(arr[0])): print(grid[i][j], end=" ")
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR STRING
Given an incomplete Sudoku configuration in terms of a 9 x 9 2-D square matrix (grid[][]), the task to find a solved Sudoku. For simplicity, you may assume that there will be only one unique solution. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. Sample Sudoku for you to get the logic for its solution: Example 1: Input: grid[][] = [[3 0 6 5 0 8 4 0 0], [5 2 0 0 0 0 0 0 0], [0 8 7 0 0 0 0 3 1 ], [0 0 3 0 1 0 0 8 0], [9 0 0 8 6 3 0 0 5], [0 5 0 0 9 0 6 0 0], [1 3 0 0 0 0 2 5 0], [0 0 0 0 0 0 0 7 4], [0 0 5 2 0 6 3 0 0]] Output: 3 1 6 5 7 8 4 9 2 5 2 9 1 3 4 7 6 8 4 8 7 6 2 9 5 3 1 2 6 3 4 1 5 9 8 7 9 7 4 8 6 3 1 2 5 8 5 1 7 9 2 6 4 3 1 3 8 9 4 7 2 5 6 6 9 2 3 5 1 8 7 4 7 4 5 2 8 6 3 1 9 Your Task: You need to complete the two functions: SolveSudoku(): Takes a grid as its argument and returns true if a solution is possible and false if it is not. printGrid(): Takes a grid as its argument and prints the 81 numbers of the solved Sudoku in a single line with space separation. NOTE: Do not give a new line character after printing the grid. It has already been taken care of in the Driver Code. Expected Time Complexity: O(9^{N*N}). Expected Auxiliary Space: O(N*N). Constraints: 0 ≤ grid[i][j] ≤ 9
class Solution: def SolveSudoku(self, grid): ans = self.solve(grid) return ans def printGrid(self, arr): for i in range(9): for j in range(9): print(arr[i][j], end=" ") def isSafe(self, row, col, grid, val): for i in range(9): if grid[row][i] == val: return False if grid[i][col] == val: return False if grid[3 * (row // 3) + i // 3][3 * (col // 3) + i % 3] == val: return False return True def solve(self, grid): for row in range(9): for col in range(9): if grid[row][col] == 0: for val in range(1, 10): if self.isSafe(row, col, grid, val): grid[row][col] = val is_SolutionExist = self.solve(grid) if is_SolutionExist: return True else: grid[row][col] = 0 return False return True
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER
Given an incomplete Sudoku configuration in terms of a 9 x 9 2-D square matrix (grid[][]), the task to find a solved Sudoku. For simplicity, you may assume that there will be only one unique solution. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. Sample Sudoku for you to get the logic for its solution: Example 1: Input: grid[][] = [[3 0 6 5 0 8 4 0 0], [5 2 0 0 0 0 0 0 0], [0 8 7 0 0 0 0 3 1 ], [0 0 3 0 1 0 0 8 0], [9 0 0 8 6 3 0 0 5], [0 5 0 0 9 0 6 0 0], [1 3 0 0 0 0 2 5 0], [0 0 0 0 0 0 0 7 4], [0 0 5 2 0 6 3 0 0]] Output: 3 1 6 5 7 8 4 9 2 5 2 9 1 3 4 7 6 8 4 8 7 6 2 9 5 3 1 2 6 3 4 1 5 9 8 7 9 7 4 8 6 3 1 2 5 8 5 1 7 9 2 6 4 3 1 3 8 9 4 7 2 5 6 6 9 2 3 5 1 8 7 4 7 4 5 2 8 6 3 1 9 Your Task: You need to complete the two functions: SolveSudoku(): Takes a grid as its argument and returns true if a solution is possible and false if it is not. printGrid(): Takes a grid as its argument and prints the 81 numbers of the solved Sudoku in a single line with space separation. NOTE: Do not give a new line character after printing the grid. It has already been taken care of in the Driver Code. Expected Time Complexity: O(9^{N*N}). Expected Auxiliary Space: O(N*N). Constraints: 0 ≤ grid[i][j] ≤ 9
class Solution: def isValid(self, board, ch, row, col): for i in range(9): if board[row][i] == ch: return False for i in range(9): if board[i][col] == ch: return False for i in range(9): if board[3 * (row // 3) + i // 3][3 * (col // 3) + i % 3] == ch: return False return True def helper(self, board): for i in range(9): for j in range(9): if board[i][j] == 0: for k in range(1, 10): if self.isValid(board, k, i, j): board[i][j] = k if self.helper(board) == True: return True board[i][j] = 0 return False return True def SolveSudoku(self, grid): self.helper(grid) return True def printGrid(self, arr): for i in arr: for j in i: print(j, end=" ")
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR RETURN NUMBER FUNC_DEF FOR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Given an incomplete Sudoku configuration in terms of a 9 x 9 2-D square matrix (grid[][]), the task to find a solved Sudoku. For simplicity, you may assume that there will be only one unique solution. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. Sample Sudoku for you to get the logic for its solution: Example 1: Input: grid[][] = [[3 0 6 5 0 8 4 0 0], [5 2 0 0 0 0 0 0 0], [0 8 7 0 0 0 0 3 1 ], [0 0 3 0 1 0 0 8 0], [9 0 0 8 6 3 0 0 5], [0 5 0 0 9 0 6 0 0], [1 3 0 0 0 0 2 5 0], [0 0 0 0 0 0 0 7 4], [0 0 5 2 0 6 3 0 0]] Output: 3 1 6 5 7 8 4 9 2 5 2 9 1 3 4 7 6 8 4 8 7 6 2 9 5 3 1 2 6 3 4 1 5 9 8 7 9 7 4 8 6 3 1 2 5 8 5 1 7 9 2 6 4 3 1 3 8 9 4 7 2 5 6 6 9 2 3 5 1 8 7 4 7 4 5 2 8 6 3 1 9 Your Task: You need to complete the two functions: SolveSudoku(): Takes a grid as its argument and returns true if a solution is possible and false if it is not. printGrid(): Takes a grid as its argument and prints the 81 numbers of the solved Sudoku in a single line with space separation. NOTE: Do not give a new line character after printing the grid. It has already been taken care of in the Driver Code. Expected Time Complexity: O(9^{N*N}). Expected Auxiliary Space: O(N*N). Constraints: 0 ≤ grid[i][j] ≤ 9
class Solution: def SolveSudoku(self, grid): N = 9 def isSafe(grid, row, col, num): for x in range(9): if grid[row][x] == num: return False for x in range(9): if grid[x][col] == num: return False startRow = row - row % 3 startCol = col - col % 3 for i in range(3): for j in range(3): if grid[i + startRow][j + startCol] == num: return False return True def solvesudoku(grid, row, col): if row == N - 1 and col == N: return True if col == N: row += 1 col = 0 if grid[row][col] > 0: return solvesudoku(grid, row, col + 1) for num in range(1, N + 1, 1): if isSafe(grid, row, col, num): grid[row][col] = num if solvesudoku(grid, row, col + 1) == True: return True grid[row][col] = 0 return False return solvesudoku(grid, 0, 0) def printGrid(self, grid): for i in range(9): for j in range(9): print(grid[i][j], end=" ")
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR NUMBER NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR STRING
Given an incomplete Sudoku configuration in terms of a 9 x 9 2-D square matrix (grid[][]), the task to find a solved Sudoku. For simplicity, you may assume that there will be only one unique solution. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. Sample Sudoku for you to get the logic for its solution: Example 1: Input: grid[][] = [[3 0 6 5 0 8 4 0 0], [5 2 0 0 0 0 0 0 0], [0 8 7 0 0 0 0 3 1 ], [0 0 3 0 1 0 0 8 0], [9 0 0 8 6 3 0 0 5], [0 5 0 0 9 0 6 0 0], [1 3 0 0 0 0 2 5 0], [0 0 0 0 0 0 0 7 4], [0 0 5 2 0 6 3 0 0]] Output: 3 1 6 5 7 8 4 9 2 5 2 9 1 3 4 7 6 8 4 8 7 6 2 9 5 3 1 2 6 3 4 1 5 9 8 7 9 7 4 8 6 3 1 2 5 8 5 1 7 9 2 6 4 3 1 3 8 9 4 7 2 5 6 6 9 2 3 5 1 8 7 4 7 4 5 2 8 6 3 1 9 Your Task: You need to complete the two functions: SolveSudoku(): Takes a grid as its argument and returns true if a solution is possible and false if it is not. printGrid(): Takes a grid as its argument and prints the 81 numbers of the solved Sudoku in a single line with space separation. NOTE: Do not give a new line character after printing the grid. It has already been taken care of in the Driver Code. Expected Time Complexity: O(9^{N*N}). Expected Auxiliary Space: O(N*N). Constraints: 0 ≤ grid[i][j] ≤ 9
class Solution: def isValid(self, temp, board, row, col): for i in range(9): if board[i][col] == temp: return False if board[row][i] == temp: return False if board[3 * (row // 3) + i // 3][3 * (col // 3) + i % 3] == temp: return False return True def solve(self, board): for i in range(9): for j in range(9): if board[i][j] == 0: for temp in [1, 2, 3, 4, 5, 6, 7, 8, 9]: if self.isValid(temp, board, i, j): board[i][j] = temp if self.solve(board) == True: return True else: board[i][j] = 0 return False return True def SolveSudoku(self, grid): if self.solve(grid): return 1 return 0 def printGrid(self, arr): for i in range(9): for j in range(9): print(arr[i][j], end=" ")
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR STRING
Given an incomplete Sudoku configuration in terms of a 9 x 9 2-D square matrix (grid[][]), the task to find a solved Sudoku. For simplicity, you may assume that there will be only one unique solution. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. Sample Sudoku for you to get the logic for its solution: Example 1: Input: grid[][] = [[3 0 6 5 0 8 4 0 0], [5 2 0 0 0 0 0 0 0], [0 8 7 0 0 0 0 3 1 ], [0 0 3 0 1 0 0 8 0], [9 0 0 8 6 3 0 0 5], [0 5 0 0 9 0 6 0 0], [1 3 0 0 0 0 2 5 0], [0 0 0 0 0 0 0 7 4], [0 0 5 2 0 6 3 0 0]] Output: 3 1 6 5 7 8 4 9 2 5 2 9 1 3 4 7 6 8 4 8 7 6 2 9 5 3 1 2 6 3 4 1 5 9 8 7 9 7 4 8 6 3 1 2 5 8 5 1 7 9 2 6 4 3 1 3 8 9 4 7 2 5 6 6 9 2 3 5 1 8 7 4 7 4 5 2 8 6 3 1 9 Your Task: You need to complete the two functions: SolveSudoku(): Takes a grid as its argument and returns true if a solution is possible and false if it is not. printGrid(): Takes a grid as its argument and prints the 81 numbers of the solved Sudoku in a single line with space separation. NOTE: Do not give a new line character after printing the grid. It has already been taken care of in the Driver Code. Expected Time Complexity: O(9^{N*N}). Expected Auxiliary Space: O(N*N). Constraints: 0 ≤ grid[i][j] ≤ 9
class Solution: def isvalid(self, grid, row, col, val): for i in range(9): if grid[row][i] == val: return False if grid[i][col] == val: return False if grid[3 * (row // 3) + i // 3][3 * (col // 3) + i % 3] == val: return False return True def SolveSudoku(self, grid): for i in range(9): for j in range(9): if grid[i][j] == 0: for c in range(1, 10): if self.isvalid(grid, i, j, c): grid[i][j] = c if self.SolveSudoku(grid): return True else: grid[i][j] = 0 return False return True def SolveSudoku1(self, grid): grow = len(grid) gcol = len(grid[0]) def issafe(i, j, val, grid): for index in range(grow): if grid[index][j] == val: return False if grid[i][index] == val: return False duprow = 3 * (i // 3) + index // 3 dupcol = 3 * (j // 3) + index // 3 if grid[duprow][dupcol] == val: return False return True def bts(grid): for i in range(grow): for j in range(gcol): if grid[i][j] == 0: for val in range(1, grow + 1): if issafe(i, j, val, grid): grid[i][j] = val if bts(grid): return True grid[i][j] = 0 return False return True return bts(grid) def printGrid(self, arr): for i in range(len(arr)): for j in range(len(arr[0])): print(arr[i][j], end=" ")
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR STRING
Given an incomplete Sudoku configuration in terms of a 9 x 9 2-D square matrix (grid[][]), the task to find a solved Sudoku. For simplicity, you may assume that there will be only one unique solution. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. Sample Sudoku for you to get the logic for its solution: Example 1: Input: grid[][] = [[3 0 6 5 0 8 4 0 0], [5 2 0 0 0 0 0 0 0], [0 8 7 0 0 0 0 3 1 ], [0 0 3 0 1 0 0 8 0], [9 0 0 8 6 3 0 0 5], [0 5 0 0 9 0 6 0 0], [1 3 0 0 0 0 2 5 0], [0 0 0 0 0 0 0 7 4], [0 0 5 2 0 6 3 0 0]] Output: 3 1 6 5 7 8 4 9 2 5 2 9 1 3 4 7 6 8 4 8 7 6 2 9 5 3 1 2 6 3 4 1 5 9 8 7 9 7 4 8 6 3 1 2 5 8 5 1 7 9 2 6 4 3 1 3 8 9 4 7 2 5 6 6 9 2 3 5 1 8 7 4 7 4 5 2 8 6 3 1 9 Your Task: You need to complete the two functions: SolveSudoku(): Takes a grid as its argument and returns true if a solution is possible and false if it is not. printGrid(): Takes a grid as its argument and prints the 81 numbers of the solved Sudoku in a single line with space separation. NOTE: Do not give a new line character after printing the grid. It has already been taken care of in the Driver Code. Expected Time Complexity: O(9^{N*N}). Expected Auxiliary Space: O(N*N). Constraints: 0 ≤ grid[i][j] ≤ 9
class Solution: def SolveSudoku(self, grid): n = len(grid[0]) for row in range(n): for col in range(n): if grid[row][col] == 0: for val in range(1, 10): if self.isSafe(grid, row, col, val, n): grid[row][col] = val next_sol = self.SolveSudoku(grid) if next_sol: return True else: grid[row][col] = 0 return False return True def isSafe(self, grid, row, col, val, n): for i in range(n): if grid[row][i] == val or grid[i][col] == val: return False if grid[3 * (row // 3) + i // 3][3 * (col // 3) + i % 3] == val: return False return True def printGrid(self, arr): for i in range(0, 9): for j in range(0, 9): print(arr[i][j], end=" ")
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER IF VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR STRING
Given an incomplete Sudoku configuration in terms of a 9 x 9 2-D square matrix (grid[][]), the task to find a solved Sudoku. For simplicity, you may assume that there will be only one unique solution. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. Sample Sudoku for you to get the logic for its solution: Example 1: Input: grid[][] = [[3 0 6 5 0 8 4 0 0], [5 2 0 0 0 0 0 0 0], [0 8 7 0 0 0 0 3 1 ], [0 0 3 0 1 0 0 8 0], [9 0 0 8 6 3 0 0 5], [0 5 0 0 9 0 6 0 0], [1 3 0 0 0 0 2 5 0], [0 0 0 0 0 0 0 7 4], [0 0 5 2 0 6 3 0 0]] Output: 3 1 6 5 7 8 4 9 2 5 2 9 1 3 4 7 6 8 4 8 7 6 2 9 5 3 1 2 6 3 4 1 5 9 8 7 9 7 4 8 6 3 1 2 5 8 5 1 7 9 2 6 4 3 1 3 8 9 4 7 2 5 6 6 9 2 3 5 1 8 7 4 7 4 5 2 8 6 3 1 9 Your Task: You need to complete the two functions: SolveSudoku(): Takes a grid as its argument and returns true if a solution is possible and false if it is not. printGrid(): Takes a grid as its argument and prints the 81 numbers of the solved Sudoku in a single line with space separation. NOTE: Do not give a new line character after printing the grid. It has already been taken care of in the Driver Code. Expected Time Complexity: O(9^{N*N}). Expected Auxiliary Space: O(N*N). Constraints: 0 ≤ grid[i][j] ≤ 9
class Solution: def SolveSudoku(self, grid): return self.solveUtil(grid, 0, 0) def solveUtil(self, grid, ii, jj): found = False for i in range(9): for j in range(9): if grid[i][j] == 0: found = True break if found: break if not found: return True for k in range(1, 10): if self.isSafe(grid, i, j, k): grid[i][j] = k if self.solveUtil(grid, i, j): return True grid[i][j] = 0 return False def isSafe(self, grid, i, j, k): for z in range(9): if grid[i][z] == k or grid[z][j] == k: return False istart = i - i % 3 jstart = j - j % 3 for ii in range(istart, istart + 3): for jj in range(jstart, jstart + 3): if grid[ii][jj] == k: return False return True def printGrid(self, arr): for i in range(9): for j in range(9): print(arr[i][j], end=" ")
CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR IF VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR STRING
Given an incomplete Sudoku configuration in terms of a 9 x 9 2-D square matrix (grid[][]), the task to find a solved Sudoku. For simplicity, you may assume that there will be only one unique solution. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. Sample Sudoku for you to get the logic for its solution: Example 1: Input: grid[][] = [[3 0 6 5 0 8 4 0 0], [5 2 0 0 0 0 0 0 0], [0 8 7 0 0 0 0 3 1 ], [0 0 3 0 1 0 0 8 0], [9 0 0 8 6 3 0 0 5], [0 5 0 0 9 0 6 0 0], [1 3 0 0 0 0 2 5 0], [0 0 0 0 0 0 0 7 4], [0 0 5 2 0 6 3 0 0]] Output: 3 1 6 5 7 8 4 9 2 5 2 9 1 3 4 7 6 8 4 8 7 6 2 9 5 3 1 2 6 3 4 1 5 9 8 7 9 7 4 8 6 3 1 2 5 8 5 1 7 9 2 6 4 3 1 3 8 9 4 7 2 5 6 6 9 2 3 5 1 8 7 4 7 4 5 2 8 6 3 1 9 Your Task: You need to complete the two functions: SolveSudoku(): Takes a grid as its argument and returns true if a solution is possible and false if it is not. printGrid(): Takes a grid as its argument and prints the 81 numbers of the solved Sudoku in a single line with space separation. NOTE: Do not give a new line character after printing the grid. It has already been taken care of in the Driver Code. Expected Time Complexity: O(9^{N*N}). Expected Auxiliary Space: O(N*N). Constraints: 0 ≤ grid[i][j] ≤ 9
class Solution: def SolveSudoku(self, grid): def valid(i, j, n, r): for x in range(9): if grid[i][x] == r or grid[x][j] == r: return False sx = i // 3 * 3 sy = j // 3 * 3 for x in range(sx, sx + 3): for y in range(sy, sy + 3): if grid[x][y] == r: return False return True def solve(i, j, n): if i == n: return True if j == n: return solve(i + 1, 0, n) if grid[i][j] != 0: return solve(i, j + 1, n) for r in range(1, 10): if valid(i, j, n, r): grid[i][j] = r success = solve(i, j + 1, n) if success: return True grid[i][j] = 0 return False return solve(0, 0, 9) def printGrid(self, arr): for i in arr: for j in i: print(j, end=" ")
CLASS_DEF FUNC_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR IF VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR NUMBER NUMBER NUMBER FUNC_DEF FOR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Given an incomplete Sudoku configuration in terms of a 9 x 9 2-D square matrix (grid[][]), the task to find a solved Sudoku. For simplicity, you may assume that there will be only one unique solution. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. Sample Sudoku for you to get the logic for its solution: Example 1: Input: grid[][] = [[3 0 6 5 0 8 4 0 0], [5 2 0 0 0 0 0 0 0], [0 8 7 0 0 0 0 3 1 ], [0 0 3 0 1 0 0 8 0], [9 0 0 8 6 3 0 0 5], [0 5 0 0 9 0 6 0 0], [1 3 0 0 0 0 2 5 0], [0 0 0 0 0 0 0 7 4], [0 0 5 2 0 6 3 0 0]] Output: 3 1 6 5 7 8 4 9 2 5 2 9 1 3 4 7 6 8 4 8 7 6 2 9 5 3 1 2 6 3 4 1 5 9 8 7 9 7 4 8 6 3 1 2 5 8 5 1 7 9 2 6 4 3 1 3 8 9 4 7 2 5 6 6 9 2 3 5 1 8 7 4 7 4 5 2 8 6 3 1 9 Your Task: You need to complete the two functions: SolveSudoku(): Takes a grid as its argument and returns true if a solution is possible and false if it is not. printGrid(): Takes a grid as its argument and prints the 81 numbers of the solved Sudoku in a single line with space separation. NOTE: Do not give a new line character after printing the grid. It has already been taken care of in the Driver Code. Expected Time Complexity: O(9^{N*N}). Expected Auxiliary Space: O(N*N). Constraints: 0 ≤ grid[i][j] ≤ 9
class Solution: def SolveSudoku(self, grid): def check(grid, row, col, k): for i in range(9): if grid[row][i] == k: return False if grid[i][col] == k: return False startRow = row - row % 3 startCol = col - col % 3 for i in range(3): for j in range(3): if grid[i + startRow][j + startCol] == k: return False return True def solve(): for i in range(9): for j in range(9): if grid[i][j] == 0: for k in range(1, 10): if check(grid, i, j, k): grid[i][j] = k if solve(): return True else: grid[i][j] = 0 return False return True solve() return True def printGrid(self, arr): for i in range(9): for j in range(9): print(arr[i][j], end=" ")
CLASS_DEF FUNC_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR STRING
Given an incomplete Sudoku configuration in terms of a 9 x 9 2-D square matrix (grid[][]), the task to find a solved Sudoku. For simplicity, you may assume that there will be only one unique solution. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. Sample Sudoku for you to get the logic for its solution: Example 1: Input: grid[][] = [[3 0 6 5 0 8 4 0 0], [5 2 0 0 0 0 0 0 0], [0 8 7 0 0 0 0 3 1 ], [0 0 3 0 1 0 0 8 0], [9 0 0 8 6 3 0 0 5], [0 5 0 0 9 0 6 0 0], [1 3 0 0 0 0 2 5 0], [0 0 0 0 0 0 0 7 4], [0 0 5 2 0 6 3 0 0]] Output: 3 1 6 5 7 8 4 9 2 5 2 9 1 3 4 7 6 8 4 8 7 6 2 9 5 3 1 2 6 3 4 1 5 9 8 7 9 7 4 8 6 3 1 2 5 8 5 1 7 9 2 6 4 3 1 3 8 9 4 7 2 5 6 6 9 2 3 5 1 8 7 4 7 4 5 2 8 6 3 1 9 Your Task: You need to complete the two functions: SolveSudoku(): Takes a grid as its argument and returns true if a solution is possible and false if it is not. printGrid(): Takes a grid as its argument and prints the 81 numbers of the solved Sudoku in a single line with space separation. NOTE: Do not give a new line character after printing the grid. It has already been taken care of in the Driver Code. Expected Time Complexity: O(9^{N*N}). Expected Auxiliary Space: O(N*N). Constraints: 0 ≤ grid[i][j] ≤ 9
class Solution: def SolveSudoku(self, grid): def isSafe(i, j, num, grid): for k in range(0, 9): if grid[i][k] == num or grid[k][j] == num: return False for x in range(i // 3 * 3, i // 3 * 3 + 3): for y in range(j // 3 * 3, j // 3 * 3 + 3): if grid[x][y] == num: return False return True def sol(i, j, grid): for x in range(i, 9): for y in range(j, 9): if grid[x][y] == 0: for k in range(1, 10): if isSafe(x, y, k, grid): grid[x][y] = k if sol(x, y, grid): return True grid[x][y] = 0 return False j = 0 return True return sol(0, 0, grid) def printGrid(self, arr): for i in arr: for j in i: print(j, end=" ")
CLASS_DEF FUNC_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR NUMBER NUMBER VAR FUNC_DEF FOR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Given an incomplete Sudoku configuration in terms of a 9 x 9 2-D square matrix (grid[][]), the task to find a solved Sudoku. For simplicity, you may assume that there will be only one unique solution. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. Sample Sudoku for you to get the logic for its solution: Example 1: Input: grid[][] = [[3 0 6 5 0 8 4 0 0], [5 2 0 0 0 0 0 0 0], [0 8 7 0 0 0 0 3 1 ], [0 0 3 0 1 0 0 8 0], [9 0 0 8 6 3 0 0 5], [0 5 0 0 9 0 6 0 0], [1 3 0 0 0 0 2 5 0], [0 0 0 0 0 0 0 7 4], [0 0 5 2 0 6 3 0 0]] Output: 3 1 6 5 7 8 4 9 2 5 2 9 1 3 4 7 6 8 4 8 7 6 2 9 5 3 1 2 6 3 4 1 5 9 8 7 9 7 4 8 6 3 1 2 5 8 5 1 7 9 2 6 4 3 1 3 8 9 4 7 2 5 6 6 9 2 3 5 1 8 7 4 7 4 5 2 8 6 3 1 9 Your Task: You need to complete the two functions: SolveSudoku(): Takes a grid as its argument and returns true if a solution is possible and false if it is not. printGrid(): Takes a grid as its argument and prints the 81 numbers of the solved Sudoku in a single line with space separation. NOTE: Do not give a new line character after printing the grid. It has already been taken care of in the Driver Code. Expected Time Complexity: O(9^{N*N}). Expected Auxiliary Space: O(N*N). Constraints: 0 ≤ grid[i][j] ≤ 9
class Solution: def SolveSudoku(self, grid): empty_cell = self.find_empty_cell(grid) if empty_cell == -1: return True for k in range(1, 10): i, j = empty_cell if self.is_safe(grid, i, j, k): grid[i][j] = k if self.SolveSudoku(grid): return True grid[i][j] = 0 return False def find_empty_cell(self, grid): for i in range(9): for j in range(9): if grid[i][j] == 0: return [i, j] return -1 def is_safe(self, grid, i, j, k): for z in range(9): if grid[i][z] == k or grid[z][j] == k: return False istart = i - i % 3 jstart = j - j % 3 for ii in range(istart, istart + 3): for jj in range(jstart, jstart + 3): if grid[ii][jj] == k: return False return True def printGrid(self, arr): for i in range(9): for j in range(9): print(arr[i][j], end=" ")
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER RETURN LIST VAR VAR RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR STRING
Given an incomplete Sudoku configuration in terms of a 9 x 9 2-D square matrix (grid[][]), the task to find a solved Sudoku. For simplicity, you may assume that there will be only one unique solution. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. Sample Sudoku for you to get the logic for its solution: Example 1: Input: grid[][] = [[3 0 6 5 0 8 4 0 0], [5 2 0 0 0 0 0 0 0], [0 8 7 0 0 0 0 3 1 ], [0 0 3 0 1 0 0 8 0], [9 0 0 8 6 3 0 0 5], [0 5 0 0 9 0 6 0 0], [1 3 0 0 0 0 2 5 0], [0 0 0 0 0 0 0 7 4], [0 0 5 2 0 6 3 0 0]] Output: 3 1 6 5 7 8 4 9 2 5 2 9 1 3 4 7 6 8 4 8 7 6 2 9 5 3 1 2 6 3 4 1 5 9 8 7 9 7 4 8 6 3 1 2 5 8 5 1 7 9 2 6 4 3 1 3 8 9 4 7 2 5 6 6 9 2 3 5 1 8 7 4 7 4 5 2 8 6 3 1 9 Your Task: You need to complete the two functions: SolveSudoku(): Takes a grid as its argument and returns true if a solution is possible and false if it is not. printGrid(): Takes a grid as its argument and prints the 81 numbers of the solved Sudoku in a single line with space separation. NOTE: Do not give a new line character after printing the grid. It has already been taken care of in the Driver Code. Expected Time Complexity: O(9^{N*N}). Expected Auxiliary Space: O(N*N). Constraints: 0 ≤ grid[i][j] ≤ 9
class Solution: def printGrid(self, arr): for i in range(9): for j in range(9): print(arr[i][j], end=" ") def used_in_row(self, arr, row, num): for i in range(9): if arr[row][i] == num: return True return False def used_in_col(self, arr, col, num): for i in range(9): if arr[i][col] == num: return True return False def used_in_box(self, arr, row, col, num): for i in range(3): for j in range(3): if arr[i + row][j + col] == num: return True return False def check_location_is_safe(self, arr, row, col, num): return ( not self.used_in_row(arr, row, num) and not self.used_in_col(arr, col, num) and not self.used_in_box(arr, row - row % 3, col - col % 3, num) ) def find_empty_location(self, arr, l): for row in range(9): for col in range(9): if arr[row][col] == 0: l[0] = row l[1] = col return True return False def SolveSudoku(self, grid): l = [0, 0] if not self.find_empty_location(grid, l): return True row = l[0] col = l[1] for num in range(1, 10): if self.check_location_is_safe(grid, row, col, num): grid[row][col] = num if self.SolveSudoku(grid): return True grid[row][col] = 0 return False
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER
Given an incomplete Sudoku configuration in terms of a 9 x 9 2-D square matrix (grid[][]), the task to find a solved Sudoku. For simplicity, you may assume that there will be only one unique solution. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. Sample Sudoku for you to get the logic for its solution: Example 1: Input: grid[][] = [[3 0 6 5 0 8 4 0 0], [5 2 0 0 0 0 0 0 0], [0 8 7 0 0 0 0 3 1 ], [0 0 3 0 1 0 0 8 0], [9 0 0 8 6 3 0 0 5], [0 5 0 0 9 0 6 0 0], [1 3 0 0 0 0 2 5 0], [0 0 0 0 0 0 0 7 4], [0 0 5 2 0 6 3 0 0]] Output: 3 1 6 5 7 8 4 9 2 5 2 9 1 3 4 7 6 8 4 8 7 6 2 9 5 3 1 2 6 3 4 1 5 9 8 7 9 7 4 8 6 3 1 2 5 8 5 1 7 9 2 6 4 3 1 3 8 9 4 7 2 5 6 6 9 2 3 5 1 8 7 4 7 4 5 2 8 6 3 1 9 Your Task: You need to complete the two functions: SolveSudoku(): Takes a grid as its argument and returns true if a solution is possible and false if it is not. printGrid(): Takes a grid as its argument and prints the 81 numbers of the solved Sudoku in a single line with space separation. NOTE: Do not give a new line character after printing the grid. It has already been taken care of in the Driver Code. Expected Time Complexity: O(9^{N*N}). Expected Auxiliary Space: O(N*N). Constraints: 0 ≤ grid[i][j] ≤ 9
def canPlace(grid, i, j, num): for r in range(9): if grid[r][j] == num: return False if grid[i][r] == num: return False r3 = int(i / 3) * 3 c3 = int(j / 3) * 3 for r in range(3): for c in range(3): if grid[r3 + r][c3 + c] == num: return False return True def solveMe(grid, i, j): if i == 9: return True if j == 9: return solveMe(grid, i + 1, 0) if grid[i][j] != 0: return solveMe(grid, i, j + 1) for num in range(1, 10): if canPlace(grid, i, j, num): grid[i][j] = num if solveMe(grid, i, j + 1): return True grid[i][j] = 0 return False class Solution: def SolveSudoku(self, grid): return solveMe(grid, 0, 0) def printGrid(self, arr): for i in range(9): for j in range(9): print(grid[i][j], end=" ")
FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR STRING
Given an incomplete Sudoku configuration in terms of a 9 x 9 2-D square matrix (grid[][]), the task to find a solved Sudoku. For simplicity, you may assume that there will be only one unique solution. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. Sample Sudoku for you to get the logic for its solution: Example 1: Input: grid[][] = [[3 0 6 5 0 8 4 0 0], [5 2 0 0 0 0 0 0 0], [0 8 7 0 0 0 0 3 1 ], [0 0 3 0 1 0 0 8 0], [9 0 0 8 6 3 0 0 5], [0 5 0 0 9 0 6 0 0], [1 3 0 0 0 0 2 5 0], [0 0 0 0 0 0 0 7 4], [0 0 5 2 0 6 3 0 0]] Output: 3 1 6 5 7 8 4 9 2 5 2 9 1 3 4 7 6 8 4 8 7 6 2 9 5 3 1 2 6 3 4 1 5 9 8 7 9 7 4 8 6 3 1 2 5 8 5 1 7 9 2 6 4 3 1 3 8 9 4 7 2 5 6 6 9 2 3 5 1 8 7 4 7 4 5 2 8 6 3 1 9 Your Task: You need to complete the two functions: SolveSudoku(): Takes a grid as its argument and returns true if a solution is possible and false if it is not. printGrid(): Takes a grid as its argument and prints the 81 numbers of the solved Sudoku in a single line with space separation. NOTE: Do not give a new line character after printing the grid. It has already been taken care of in the Driver Code. Expected Time Complexity: O(9^{N*N}). Expected Auxiliary Space: O(N*N). Constraints: 0 ≤ grid[i][j] ≤ 9
class Solution: def SolveSudoku(self, grid): n = len(grid) m = len(grid[0]) for i in range(n): for j in range(m): if grid[i][j] == 0: for c in range(1, 10): if self.checkifokay(i, j, c, n, grid): grid[i][j] = c if self.SolveSudoku(grid): return True grid[i][j] = 0 return False return True def checkifokay(self, row, col, c, n, grid): for k in range(0, 9): if grid[row][k] == c: return False if grid[k][col] == c: return False x = 3 * (row // 3) + k // 3 y = 3 * (col // 3) + k % 3 if grid[x][y] == c: return False return True def printGrid(self, arr): for i in range(len(arr)): for j in range(len(arr[i])): print(arr[i][j], end=" ")
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING
Given an incomplete Sudoku configuration in terms of a 9 x 9 2-D square matrix (grid[][]), the task to find a solved Sudoku. For simplicity, you may assume that there will be only one unique solution. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. Sample Sudoku for you to get the logic for its solution: Example 1: Input: grid[][] = [[3 0 6 5 0 8 4 0 0], [5 2 0 0 0 0 0 0 0], [0 8 7 0 0 0 0 3 1 ], [0 0 3 0 1 0 0 8 0], [9 0 0 8 6 3 0 0 5], [0 5 0 0 9 0 6 0 0], [1 3 0 0 0 0 2 5 0], [0 0 0 0 0 0 0 7 4], [0 0 5 2 0 6 3 0 0]] Output: 3 1 6 5 7 8 4 9 2 5 2 9 1 3 4 7 6 8 4 8 7 6 2 9 5 3 1 2 6 3 4 1 5 9 8 7 9 7 4 8 6 3 1 2 5 8 5 1 7 9 2 6 4 3 1 3 8 9 4 7 2 5 6 6 9 2 3 5 1 8 7 4 7 4 5 2 8 6 3 1 9 Your Task: You need to complete the two functions: SolveSudoku(): Takes a grid as its argument and returns true if a solution is possible and false if it is not. printGrid(): Takes a grid as its argument and prints the 81 numbers of the solved Sudoku in a single line with space separation. NOTE: Do not give a new line character after printing the grid. It has already been taken care of in the Driver Code. Expected Time Complexity: O(9^{N*N}). Expected Auxiliary Space: O(N*N). Constraints: 0 ≤ grid[i][j] ≤ 9
class Solution: def SolveSudoku(self, grid): def checkvalid(row, col, val): for i in range(9): if val == grid[row][i]: return False if val == grid[i][col]: return False r = row // 3 c = col // 3 for i in range(3 * r, 3 * r + 3): for j in range(3 * c, 3 * c + 3): if grid[i][j] == val: return False return True def findsol(i, j): if i == 8 and j >= 9: return True if i < 9 and j >= 9: return findsol(i + 1, 0) if i >= 9 or j >= 9: return False if grid[i][j] != 0: return findsol(i, j + 1) else: for x in range(1, 10): if checkvalid(i, j, x): grid[i][j] = x if findsol(i, j + 1) == True: return True grid[i][j] = 0 return False return findsol(0, 0) def printGrid(self, arr): for i in arr: for j in i: print(j, end=" ")
CLASS_DEF FUNC_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR NUMBER NUMBER FUNC_DEF FOR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Given an incomplete Sudoku configuration in terms of a 9 x 9 2-D square matrix (grid[][]), the task to find a solved Sudoku. For simplicity, you may assume that there will be only one unique solution. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. Sample Sudoku for you to get the logic for its solution: Example 1: Input: grid[][] = [[3 0 6 5 0 8 4 0 0], [5 2 0 0 0 0 0 0 0], [0 8 7 0 0 0 0 3 1 ], [0 0 3 0 1 0 0 8 0], [9 0 0 8 6 3 0 0 5], [0 5 0 0 9 0 6 0 0], [1 3 0 0 0 0 2 5 0], [0 0 0 0 0 0 0 7 4], [0 0 5 2 0 6 3 0 0]] Output: 3 1 6 5 7 8 4 9 2 5 2 9 1 3 4 7 6 8 4 8 7 6 2 9 5 3 1 2 6 3 4 1 5 9 8 7 9 7 4 8 6 3 1 2 5 8 5 1 7 9 2 6 4 3 1 3 8 9 4 7 2 5 6 6 9 2 3 5 1 8 7 4 7 4 5 2 8 6 3 1 9 Your Task: You need to complete the two functions: SolveSudoku(): Takes a grid as its argument and returns true if a solution is possible and false if it is not. printGrid(): Takes a grid as its argument and prints the 81 numbers of the solved Sudoku in a single line with space separation. NOTE: Do not give a new line character after printing the grid. It has already been taken care of in the Driver Code. Expected Time Complexity: O(9^{N*N}). Expected Auxiliary Space: O(N*N). Constraints: 0 ≤ grid[i][j] ≤ 9
class Solution: def SolveSudoku(self, grid): ans = [] new = [([0] * 9) for i in range(9)] def isSafe(grid, row, col, i): for k in range(0, 9): if grid[row][k] == i: return False for k in range(0, 9): if grid[k][col] == i: return False rowe = row // 3 cole = col // 3 rowe, cole = rowe * 3, cole * 3 for a in range(rowe, rowe + 3): for b in range(cole, cole + 3): if grid[a][b] == i: return False return True def istrue(grid): for i in range(9): for j in range(9): if grid[i][j] == 0: return False return True def backtrack(grid, row, col): if row > 8: if istrue(grid): ans.append(True) for i in range(9): for j in range(9): new[i][j] = grid[i][j] return if grid[row][col] == 0: for i in range(1, 10): if isSafe(grid, row, col, i): grid[row][col] = i if col == 8: backtrack(grid, row + 1, 0) else: backtrack(grid, row, col + 1) grid[row][col] = 0 elif col == 8: backtrack(grid, row + 1, 0) else: backtrack(grid, row, col + 1) backtrack(grid, 0, 0) for i in range(9): for j in range(9): grid[i][j] = new[i][j] if len(ans): return True return False def printGrid(self, arr): for i in range(9): for j in range(9): print(arr[i][j], end=" ")
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR RETURN IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR STRING
Given an incomplete Sudoku configuration in terms of a 9 x 9 2-D square matrix (grid[][]), the task to find a solved Sudoku. For simplicity, you may assume that there will be only one unique solution. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. Sample Sudoku for you to get the logic for its solution: Example 1: Input: grid[][] = [[3 0 6 5 0 8 4 0 0], [5 2 0 0 0 0 0 0 0], [0 8 7 0 0 0 0 3 1 ], [0 0 3 0 1 0 0 8 0], [9 0 0 8 6 3 0 0 5], [0 5 0 0 9 0 6 0 0], [1 3 0 0 0 0 2 5 0], [0 0 0 0 0 0 0 7 4], [0 0 5 2 0 6 3 0 0]] Output: 3 1 6 5 7 8 4 9 2 5 2 9 1 3 4 7 6 8 4 8 7 6 2 9 5 3 1 2 6 3 4 1 5 9 8 7 9 7 4 8 6 3 1 2 5 8 5 1 7 9 2 6 4 3 1 3 8 9 4 7 2 5 6 6 9 2 3 5 1 8 7 4 7 4 5 2 8 6 3 1 9 Your Task: You need to complete the two functions: SolveSudoku(): Takes a grid as its argument and returns true if a solution is possible and false if it is not. printGrid(): Takes a grid as its argument and prints the 81 numbers of the solved Sudoku in a single line with space separation. NOTE: Do not give a new line character after printing the grid. It has already been taken care of in the Driver Code. Expected Time Complexity: O(9^{N*N}). Expected Auxiliary Space: O(N*N). Constraints: 0 ≤ grid[i][j] ≤ 9
class Solution: def __init__(self): self.ds = [] def SolveSudoku(self, grid): rows, cols = len(grid), len(grid[0]) def isvalid(num, grid, row, col): for i in range(rows): if grid[i][col] == num: return False if grid[row][i] == num: return False if grid[3 * (row // 3) + i // 3][3 * (col // 3) + i % 3] == num: return False return True def rec(r, c): for i in range(rows): for j in range(cols): if grid[i][j] == 0: for num in range(1, 10): if isvalid(num, grid, i, j): grid[i][j] = num if rec(i, j): return True else: grid[i][j] = 0 return False return True return rec(0, 0) def printGrid(self, arr): for i in range(9): for j in range(9): print(arr[i][j], end=" ")
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER RETURN FUNC_CALL VAR NUMBER NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR STRING
Given an incomplete Sudoku configuration in terms of a 9 x 9 2-D square matrix (grid[][]), the task to find a solved Sudoku. For simplicity, you may assume that there will be only one unique solution. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. Sample Sudoku for you to get the logic for its solution: Example 1: Input: grid[][] = [[3 0 6 5 0 8 4 0 0], [5 2 0 0 0 0 0 0 0], [0 8 7 0 0 0 0 3 1 ], [0 0 3 0 1 0 0 8 0], [9 0 0 8 6 3 0 0 5], [0 5 0 0 9 0 6 0 0], [1 3 0 0 0 0 2 5 0], [0 0 0 0 0 0 0 7 4], [0 0 5 2 0 6 3 0 0]] Output: 3 1 6 5 7 8 4 9 2 5 2 9 1 3 4 7 6 8 4 8 7 6 2 9 5 3 1 2 6 3 4 1 5 9 8 7 9 7 4 8 6 3 1 2 5 8 5 1 7 9 2 6 4 3 1 3 8 9 4 7 2 5 6 6 9 2 3 5 1 8 7 4 7 4 5 2 8 6 3 1 9 Your Task: You need to complete the two functions: SolveSudoku(): Takes a grid as its argument and returns true if a solution is possible and false if it is not. printGrid(): Takes a grid as its argument and prints the 81 numbers of the solved Sudoku in a single line with space separation. NOTE: Do not give a new line character after printing the grid. It has already been taken care of in the Driver Code. Expected Time Complexity: O(9^{N*N}). Expected Auxiliary Space: O(N*N). Constraints: 0 ≤ grid[i][j] ≤ 9
class Solution: def SolveSudoku(self, grid): tablePossibles = [] sol = [] def ad(lista): tmp = [] for i in range(0, 9): vec = [] for j in range(0, 9): vec.append(lista[i][j]) tmp.append(vec) sol.append(tmp) def solve(grid): find = find_empty() if not find: for i in range(0, 9): for j in range(0, 9): if grid[i][j] == 0: return False ad(grid) return True else: row, col = find nums = [] for k in tablePossibles[row][col]: nums.append(k) if len(nums) == 0: return False for i in nums: grid[row][col] = i temps = [] for e in range(0, 9): if i in tablePossibles[row][e]: temps.append([row, e]) tablePossibles[row][e].remove(i) for e in range(0, 9): if i in tablePossibles[e][col]: temps.append([e, col]) tablePossibles[e][col].remove(i) box_x = col // 3 box_y = row // 3 for m in range(box_y * 3, box_y * 3 + 3): for j in range(box_x * 3, box_x * 3 + 3): if i in tablePossibles[m][j]: temps.append([m, j]) tablePossibles[m][j].remove(i) if solve(grid): return True for j in temps: tablePossibles[j[0]][j[1]].insert(0, i) grid[row][col] = 0 return False def getPossibles(grid, tablePossibles): for i in range(0, 9): vec = [] for j in range(0, 9): if grid[i][j] == 0: vec.append(getNumPossibles(grid, i, j)) else: vec.append([]) tablePossibles.append(vec) def getNumPossibles(grid, y, x): nums = [1, 2, 3, 4, 5, 6, 7, 8, 9] for i in range(0, 9): el = grid[y][i] if el != 0 and el in nums: nums.remove(el) for i in range(0, 9): el = grid[i][x] if el != 0 and el in nums: nums.remove(el) box_x = x // 3 box_y = y // 3 for i in range(box_y * 3, box_y * 3 + 3): for j in range(box_x * 3, box_x * 3 + 3): el = grid[i][j] if el != 0 and el in nums: nums.remove(el) return nums def find_empty(): min = [10, 0, 0] for i in range(0, 9): for j in range(0, 9): leng = len(tablePossibles[i][j]) if leng == 0 or grid[i][j] != 0: continue if leng < min[0] and leng > 0: min = [leng, i, j] if min[0] == 10: return None return min[1], min[2] getPossibles(grid, tablePossibles) solve(grid) return True def printGrid(self, arr): for i in range(9): for j in range(9): print(arr[i][j], end=" ")
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR IF VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR RETURN NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR VAR VAR IF VAR NUMBER NUMBER RETURN NONE RETURN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR STRING
Given an incomplete Sudoku configuration in terms of a 9 x 9 2-D square matrix (grid[][]), the task to find a solved Sudoku. For simplicity, you may assume that there will be only one unique solution. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. Sample Sudoku for you to get the logic for its solution: Example 1: Input: grid[][] = [[3 0 6 5 0 8 4 0 0], [5 2 0 0 0 0 0 0 0], [0 8 7 0 0 0 0 3 1 ], [0 0 3 0 1 0 0 8 0], [9 0 0 8 6 3 0 0 5], [0 5 0 0 9 0 6 0 0], [1 3 0 0 0 0 2 5 0], [0 0 0 0 0 0 0 7 4], [0 0 5 2 0 6 3 0 0]] Output: 3 1 6 5 7 8 4 9 2 5 2 9 1 3 4 7 6 8 4 8 7 6 2 9 5 3 1 2 6 3 4 1 5 9 8 7 9 7 4 8 6 3 1 2 5 8 5 1 7 9 2 6 4 3 1 3 8 9 4 7 2 5 6 6 9 2 3 5 1 8 7 4 7 4 5 2 8 6 3 1 9 Your Task: You need to complete the two functions: SolveSudoku(): Takes a grid as its argument and returns true if a solution is possible and false if it is not. printGrid(): Takes a grid as its argument and prints the 81 numbers of the solved Sudoku in a single line with space separation. NOTE: Do not give a new line character after printing the grid. It has already been taken care of in the Driver Code. Expected Time Complexity: O(9^{N*N}). Expected Auxiliary Space: O(N*N). Constraints: 0 ≤ grid[i][j] ≤ 9
class Solution: def SolveSudoku(self, grid): def function1(i, j, grid, k): m = i n = j s = "F" while m < 9: if grid[m][j] == k: return False m = m + 1 m = i while m >= 0: if grid[m][j] == k: return False m = m - 1 while n < 9: if grid[i][n] == k: return False n = n + 1 n = j while n >= 0: if grid[i][n] == k: return False n = n - 1 if i < 3: v = 0 b = 3 elif i < 6: v = 3 b = 6 elif i < 9: v = 6 b = 9 if j < 3: s = 0 l = 3 elif j < 6: s = 3 l = 6 elif j < 9: s = 6 l = 9 m = i n = j for r in range(v, b): for v in range(s, l): if grid[r][v] == k: return False return True def function(grid): for i in range(9): for j in range(9): s = "F" if grid[i][j] == 0: for k in range(1, 10): if function1(i, j, grid, k): grid[i][j] = k if function(grid): return True else: grid[i][j] = 0 return False return True return function(grid) def printGrid(self, arr): for i in range(9): for j in range(9): print(arr[i][j], end=" ")
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR STRING WHILE VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR STRING IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR STRING
Given an incomplete Sudoku configuration in terms of a 9 x 9 2-D square matrix (grid[][]), the task to find a solved Sudoku. For simplicity, you may assume that there will be only one unique solution. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. Sample Sudoku for you to get the logic for its solution: Example 1: Input: grid[][] = [[3 0 6 5 0 8 4 0 0], [5 2 0 0 0 0 0 0 0], [0 8 7 0 0 0 0 3 1 ], [0 0 3 0 1 0 0 8 0], [9 0 0 8 6 3 0 0 5], [0 5 0 0 9 0 6 0 0], [1 3 0 0 0 0 2 5 0], [0 0 0 0 0 0 0 7 4], [0 0 5 2 0 6 3 0 0]] Output: 3 1 6 5 7 8 4 9 2 5 2 9 1 3 4 7 6 8 4 8 7 6 2 9 5 3 1 2 6 3 4 1 5 9 8 7 9 7 4 8 6 3 1 2 5 8 5 1 7 9 2 6 4 3 1 3 8 9 4 7 2 5 6 6 9 2 3 5 1 8 7 4 7 4 5 2 8 6 3 1 9 Your Task: You need to complete the two functions: SolveSudoku(): Takes a grid as its argument and returns true if a solution is possible and false if it is not. printGrid(): Takes a grid as its argument and prints the 81 numbers of the solved Sudoku in a single line with space separation. NOTE: Do not give a new line character after printing the grid. It has already been taken care of in the Driver Code. Expected Time Complexity: O(9^{N*N}). Expected Auxiliary Space: O(N*N). Constraints: 0 ≤ grid[i][j] ≤ 9
class Solution: def SolveSudoku(self, grid): return solve_sudoku(grid) def printGrid(self, arr): print_sudoku(arr) def solve_sudoku_helper(grid: [[]], row: int, col: int): if row == 9: return True if col == 9: return solve_sudoku_helper(grid, row + 1, 0) if grid[row][col] != 0: return solve_sudoku_helper(grid, row, col + 1) for i in range(1, 10): if is_safe(grid, row, col, i): grid[row][col] = i if solve_sudoku_helper(grid, row, col + 1): return True grid[row][col] = 0 def is_safe(grid, row: int, col: int, number: int): for i in range(0, 9): if grid[row][i] == number: return False for i in range(0, 9): if grid[i][col] == number: return False start_row = row - row % 3 start_col = col - col % 3 for i in range(start_row, start_row + 3): for j in range(start_col, start_col + 3): if grid[i][j] == number: return False return True def solve_sudoku(grid: [[]]): return solve_sudoku_helper(grid, 0, 0) def print_sudoku(grid: [[]]): solve_sudoku(grid) for i in range(0, 9): for j in range(0, 9): print(grid[i][j], end=" ")
CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR FUNC_DEF LIST LIST VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_DEF VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF LIST LIST RETURN FUNC_CALL VAR VAR NUMBER NUMBER FUNC_DEF LIST LIST EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR STRING
Given an incomplete Sudoku configuration in terms of a 9 x 9 2-D square matrix (grid[][]), the task to find a solved Sudoku. For simplicity, you may assume that there will be only one unique solution. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. Sample Sudoku for you to get the logic for its solution: Example 1: Input: grid[][] = [[3 0 6 5 0 8 4 0 0], [5 2 0 0 0 0 0 0 0], [0 8 7 0 0 0 0 3 1 ], [0 0 3 0 1 0 0 8 0], [9 0 0 8 6 3 0 0 5], [0 5 0 0 9 0 6 0 0], [1 3 0 0 0 0 2 5 0], [0 0 0 0 0 0 0 7 4], [0 0 5 2 0 6 3 0 0]] Output: 3 1 6 5 7 8 4 9 2 5 2 9 1 3 4 7 6 8 4 8 7 6 2 9 5 3 1 2 6 3 4 1 5 9 8 7 9 7 4 8 6 3 1 2 5 8 5 1 7 9 2 6 4 3 1 3 8 9 4 7 2 5 6 6 9 2 3 5 1 8 7 4 7 4 5 2 8 6 3 1 9 Your Task: You need to complete the two functions: SolveSudoku(): Takes a grid as its argument and returns true if a solution is possible and false if it is not. printGrid(): Takes a grid as its argument and prints the 81 numbers of the solved Sudoku in a single line with space separation. NOTE: Do not give a new line character after printing the grid. It has already been taken care of in the Driver Code. Expected Time Complexity: O(9^{N*N}). Expected Auxiliary Space: O(N*N). Constraints: 0 ≤ grid[i][j] ≤ 9
import sys sys.setrecursionlimit(10**6) class Solution: def is_placable(self, i, j, k): if self.grid[i][j] == 0: for ii in range(9): if self.grid[i][ii] == k: return False for ii in range(9): if self.grid[ii][j] == k: return False nj = int(j / 3) * 3 ni = int(i / 3) * 3 for ii in range(3): for jj in range(3): if self.grid[ni + ii][nj + jj] == k: return False return True else: return False def solve(self): for i in range(9): for j in range(9): if self.grid[i][j] == 0: for x in range(9): if self.is_placable(i, j, x + 1): self.grid[i][j] = x + 1 if self.solve(): return True self.grid[i][j] = 0 return False return True def SolveSudoku(self, grid): self.grid = grid return self.solve() def printGrid(self, arr): for i in range(len(arr)): for j in range(len(arr)): print(arr[i][j], end=" ")
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR RETURN FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING
Given an incomplete Sudoku configuration in terms of a 9 x 9 2-D square matrix (grid[][]), the task to find a solved Sudoku. For simplicity, you may assume that there will be only one unique solution. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. Sample Sudoku for you to get the logic for its solution: Example 1: Input: grid[][] = [[3 0 6 5 0 8 4 0 0], [5 2 0 0 0 0 0 0 0], [0 8 7 0 0 0 0 3 1 ], [0 0 3 0 1 0 0 8 0], [9 0 0 8 6 3 0 0 5], [0 5 0 0 9 0 6 0 0], [1 3 0 0 0 0 2 5 0], [0 0 0 0 0 0 0 7 4], [0 0 5 2 0 6 3 0 0]] Output: 3 1 6 5 7 8 4 9 2 5 2 9 1 3 4 7 6 8 4 8 7 6 2 9 5 3 1 2 6 3 4 1 5 9 8 7 9 7 4 8 6 3 1 2 5 8 5 1 7 9 2 6 4 3 1 3 8 9 4 7 2 5 6 6 9 2 3 5 1 8 7 4 7 4 5 2 8 6 3 1 9 Your Task: You need to complete the two functions: SolveSudoku(): Takes a grid as its argument and returns true if a solution is possible and false if it is not. printGrid(): Takes a grid as its argument and prints the 81 numbers of the solved Sudoku in a single line with space separation. NOTE: Do not give a new line character after printing the grid. It has already been taken care of in the Driver Code. Expected Time Complexity: O(9^{N*N}). Expected Auxiliary Space: O(N*N). Constraints: 0 ≤ grid[i][j] ≤ 9
class Solution: def printGrid(self, arr): for i in range(9): for j in range(9): print(arr[i][j], end=" ") def used_in_box(self, grid, box_start_row, box_start_col, num): for row in range(3): for col in range(3): if grid[row + box_start_row][col + box_start_col] == num: return True return False def is_safe(self, grid, row, col, num): for c in range(9): if grid[row][c] == num: return False for r in range(9): if grid[r][col] == num: return False box_start_row = row - row % 3 box_start_col = col - col % 3 flag = self.used_in_box(grid, box_start_row, box_start_col, num) if not flag: return True return False def func(self, row, col, n, grid): flag = False for a in range(n): for b in range(n): if grid[a][b] == 0: row = a col = b flag = True break if flag: break if not flag: return True for num in range(1, n + 1): if self.is_safe(grid, row, col, num) and grid[row][col] == 0: grid[row][col] = num if self.func(row, col, n, grid): return True grid[row][col] = 0 return False def SolveSudoku(self, grid): n = len(grid[0]) row = 0 col = 0 if self.func(row, col, n, grid): return True return False
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR IF VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given an incomplete Sudoku configuration in terms of a 9 x 9 2-D square matrix (grid[][]), the task to find a solved Sudoku. For simplicity, you may assume that there will be only one unique solution. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. Sample Sudoku for you to get the logic for its solution: Example 1: Input: grid[][] = [[3 0 6 5 0 8 4 0 0], [5 2 0 0 0 0 0 0 0], [0 8 7 0 0 0 0 3 1 ], [0 0 3 0 1 0 0 8 0], [9 0 0 8 6 3 0 0 5], [0 5 0 0 9 0 6 0 0], [1 3 0 0 0 0 2 5 0], [0 0 0 0 0 0 0 7 4], [0 0 5 2 0 6 3 0 0]] Output: 3 1 6 5 7 8 4 9 2 5 2 9 1 3 4 7 6 8 4 8 7 6 2 9 5 3 1 2 6 3 4 1 5 9 8 7 9 7 4 8 6 3 1 2 5 8 5 1 7 9 2 6 4 3 1 3 8 9 4 7 2 5 6 6 9 2 3 5 1 8 7 4 7 4 5 2 8 6 3 1 9 Your Task: You need to complete the two functions: SolveSudoku(): Takes a grid as its argument and returns true if a solution is possible and false if it is not. printGrid(): Takes a grid as its argument and prints the 81 numbers of the solved Sudoku in a single line with space separation. NOTE: Do not give a new line character after printing the grid. It has already been taken care of in the Driver Code. Expected Time Complexity: O(9^{N*N}). Expected Auxiliary Space: O(N*N). Constraints: 0 ≤ grid[i][j] ≤ 9
class Solution: def solve(self, grid, row, col, cell, i, j): if j == 9: if i == 8: return True else: return self.solve(grid, row, col, cell, i + 1, 0) if grid[i][j]: return self.solve(grid, row, col, cell, i, j + 1) else: for k in range(1, 9 + 1): if ( k not in row[i] and k not in col[j] and k not in cell[i // 3][j // 3] ): grid[i][j] = k row[i].add(k) col[j].add(k) cell[i // 3][j // 3].add(k) if self.solve(grid, row, col, cell, i, j + 1): return True grid[i][j] = 0 row[i].remove(k) col[j].remove(k) cell[i // 3][j // 3].remove(k) return False def SolveSudoku(self, grid): row = [set() for i in range(9)] col = [set() for j in range(9)] cell = [[set() for i in range(3)] for j in range(3)] for i in range(9): for j in range(9): if grid[i][j]: row[i].add(grid[i][j]) col[j].add(grid[i][j]) cell[i // 3][j // 3].add(grid[i][j]) return self.solve(grid, row, col, cell, 0, 0) def printGrid(self, arr): for row in arr: print(*row, end=" ")
CLASS_DEF FUNC_DEF IF VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER FUNC_DEF FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Given an incomplete Sudoku configuration in terms of a 9 x 9 2-D square matrix (grid[][]), the task to find a solved Sudoku. For simplicity, you may assume that there will be only one unique solution. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. Sample Sudoku for you to get the logic for its solution: Example 1: Input: grid[][] = [[3 0 6 5 0 8 4 0 0], [5 2 0 0 0 0 0 0 0], [0 8 7 0 0 0 0 3 1 ], [0 0 3 0 1 0 0 8 0], [9 0 0 8 6 3 0 0 5], [0 5 0 0 9 0 6 0 0], [1 3 0 0 0 0 2 5 0], [0 0 0 0 0 0 0 7 4], [0 0 5 2 0 6 3 0 0]] Output: 3 1 6 5 7 8 4 9 2 5 2 9 1 3 4 7 6 8 4 8 7 6 2 9 5 3 1 2 6 3 4 1 5 9 8 7 9 7 4 8 6 3 1 2 5 8 5 1 7 9 2 6 4 3 1 3 8 9 4 7 2 5 6 6 9 2 3 5 1 8 7 4 7 4 5 2 8 6 3 1 9 Your Task: You need to complete the two functions: SolveSudoku(): Takes a grid as its argument and returns true if a solution is possible and false if it is not. printGrid(): Takes a grid as its argument and prints the 81 numbers of the solved Sudoku in a single line with space separation. NOTE: Do not give a new line character after printing the grid. It has already been taken care of in the Driver Code. Expected Time Complexity: O(9^{N*N}). Expected Auxiliary Space: O(N*N). Constraints: 0 ≤ grid[i][j] ≤ 9
class Solution: def find_empty_cell(self, board): for row in range(9): for col in range(9): if board[row][col] == 0: return row, col return None def SolveSudoku(self, board): empty_cell = self.find_empty_cell(board) if not empty_cell: return True row, col = empty_cell for num in range(1, 10): if self.is_valid(board, num, row, col): board[row][col] = num if self.SolveSudoku(board): return True board[row][col] = 0 return False def is_valid(self, board, num, row, col): for i in range(9): if board[row][i] == num: return False for j in range(9): if board[j][col] == num: return False box_row = row // 3 * 3 box_col = col // 3 * 3 for i in range(3): for j in range(3): if board[box_row + i][box_col + j] == num: return False return True def printGrid(self, arr): for row in arr: for col in row: print(col, end=" ")
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR RETURN NONE FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR RETURN NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Given an incomplete Sudoku configuration in terms of a 9 x 9 2-D square matrix (grid[][]), the task to find a solved Sudoku. For simplicity, you may assume that there will be only one unique solution. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. Sample Sudoku for you to get the logic for its solution: Example 1: Input: grid[][] = [[3 0 6 5 0 8 4 0 0], [5 2 0 0 0 0 0 0 0], [0 8 7 0 0 0 0 3 1 ], [0 0 3 0 1 0 0 8 0], [9 0 0 8 6 3 0 0 5], [0 5 0 0 9 0 6 0 0], [1 3 0 0 0 0 2 5 0], [0 0 0 0 0 0 0 7 4], [0 0 5 2 0 6 3 0 0]] Output: 3 1 6 5 7 8 4 9 2 5 2 9 1 3 4 7 6 8 4 8 7 6 2 9 5 3 1 2 6 3 4 1 5 9 8 7 9 7 4 8 6 3 1 2 5 8 5 1 7 9 2 6 4 3 1 3 8 9 4 7 2 5 6 6 9 2 3 5 1 8 7 4 7 4 5 2 8 6 3 1 9 Your Task: You need to complete the two functions: SolveSudoku(): Takes a grid as its argument and returns true if a solution is possible and false if it is not. printGrid(): Takes a grid as its argument and prints the 81 numbers of the solved Sudoku in a single line with space separation. NOTE: Do not give a new line character after printing the grid. It has already been taken care of in the Driver Code. Expected Time Complexity: O(9^{N*N}). Expected Auxiliary Space: O(N*N). Constraints: 0 ≤ grid[i][j] ≤ 9
class Solution: def SolveSudoku(self, grid): for i in range(9): for j in range(9): if grid[i][j] == 0: break else: continue break else: return True for i in range(9): for j in range(9): if grid[i][j] != 0: continue for k in range(1, 10): if self.isValid(grid, i, j, k): grid[i][j] = k if self.SolveSudoku(grid): return True grid[i][j] = 0 return False def isValid(self, grid, i, j, k): for p in range(9): if grid[i][p] == k or grid[p][j] == k: return False box_row = i // 3 * 3 box_col = j // 3 * 3 for pa in range(3): for pb in range(3): if grid[box_row + pa][box_col + pb] == k: return False return True def printGrid(self, arr): for i in range(9): for j in range(9): print(grid[i][j], end=" ")
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR STRING
Given an incomplete Sudoku configuration in terms of a 9 x 9 2-D square matrix (grid[][]), the task to find a solved Sudoku. For simplicity, you may assume that there will be only one unique solution. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. Sample Sudoku for you to get the logic for its solution: Example 1: Input: grid[][] = [[3 0 6 5 0 8 4 0 0], [5 2 0 0 0 0 0 0 0], [0 8 7 0 0 0 0 3 1 ], [0 0 3 0 1 0 0 8 0], [9 0 0 8 6 3 0 0 5], [0 5 0 0 9 0 6 0 0], [1 3 0 0 0 0 2 5 0], [0 0 0 0 0 0 0 7 4], [0 0 5 2 0 6 3 0 0]] Output: 3 1 6 5 7 8 4 9 2 5 2 9 1 3 4 7 6 8 4 8 7 6 2 9 5 3 1 2 6 3 4 1 5 9 8 7 9 7 4 8 6 3 1 2 5 8 5 1 7 9 2 6 4 3 1 3 8 9 4 7 2 5 6 6 9 2 3 5 1 8 7 4 7 4 5 2 8 6 3 1 9 Your Task: You need to complete the two functions: SolveSudoku(): Takes a grid as its argument and returns true if a solution is possible and false if it is not. printGrid(): Takes a grid as its argument and prints the 81 numbers of the solved Sudoku in a single line with space separation. NOTE: Do not give a new line character after printing the grid. It has already been taken care of in the Driver Code. Expected Time Complexity: O(9^{N*N}). Expected Auxiliary Space: O(N*N). Constraints: 0 ≤ grid[i][j] ≤ 9
class Solution: def SolveSudoku(self, grid): for i in range(9): for j in range(9): if grid[i][j] == 0: for num in range(1, 10): if self.IsValid(grid, i, j, num): grid[i][j] = num if self.SolveSudoku(grid) == True: return True else: grid[i][j] = 0 return False return True def IsValid(self, grid, row, col, num): for i in range(0, 9): if grid[row][i] == num: return False if grid[i][col] == num: return False for i in range(3): for j in range(3): if grid[int(row - row % 3 + i)][col - col % 3 + j] == num: return False return True def printGrid(self, arr): for i in range(len(arr)): for j in range(len(arr[0])): print(arr[i][j], end=" ")
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR STRING