description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times. Example 1: Input: K = 4 str = "1234567" Output: 7654321 Explanation: Three swaps can make the input 1234567 to 7654321, swapping 1 with 7, 2 with 6 and finally 3 with 5 Example 2: Input: K = 3 str = "3435335" Output: 5543333 Explanation: Three swaps can make the input 3435335 to 5543333, swapping 3 with 5, 4 with 5 and finally 3 with 4 Your task: You don't have to read input or print anything. Your task is to complete the function findMaximumNum() which takes the string and an integer as input and returns a string containing the largest number formed by perfoming the swap operation at most k times. Expected Time Complexity: O(n!/(n-k)!) , where n = length of input string Expected Auxiliary Space: O(n) Constraints: 1 ≤ |str| ≤ 30 1 ≤ K ≤ 10
class Solution: def findMaximumNum(self, s, k): ans = int(s) s = list(s) s = list(map(int, s)) def helper(start, s, k): nonlocal ans if k == 0 or start == len(s): ta = int("".join(list(map(str, s)))) ans = max(ta, ans) return biggest_index = [start] pointer = start + 1 while pointer < len(s): if s[pointer] >= s[biggest_index[-1]]: if s[biggest_index[-1]] != s[pointer]: biggest_index = [] biggest_index.append(pointer) pointer += 1 noswap = True for big in biggest_index: if big != start and s[big] != s[start]: noswap = False s[start], s[big] = s[big], s[start] helper(start + 1, s, k - 1) s[start], s[big] = s[big], s[start] if noswap and k: helper(start + 1, s, k) helper(0, s, k) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN ASSIGN VAR LIST VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR RETURN VAR
Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times. Example 1: Input: K = 4 str = "1234567" Output: 7654321 Explanation: Three swaps can make the input 1234567 to 7654321, swapping 1 with 7, 2 with 6 and finally 3 with 5 Example 2: Input: K = 3 str = "3435335" Output: 5543333 Explanation: Three swaps can make the input 3435335 to 5543333, swapping 3 with 5, 4 with 5 and finally 3 with 4 Your task: You don't have to read input or print anything. Your task is to complete the function findMaximumNum() which takes the string and an integer as input and returns a string containing the largest number formed by perfoming the swap operation at most k times. Expected Time Complexity: O(n!/(n-k)!) , where n = length of input string Expected Auxiliary Space: O(n) Constraints: 1 ≤ |str| ≤ 30 1 ≤ K ≤ 10
class Solution: def findMaximumNum(self, s, k): mx = float("-inf") def find(lst, k, ind): nonlocal mx if ind >= len(lst): return sm = 0 for i in lst: sm = sm * 10 + i if sm > mx: mx = sm if k == 0: return found = False for index in range(ind + 1, len(lst)): if lst[index] > lst[ind]: found = True lst[index], lst[ind] = lst[ind], lst[index] find(lst, k - 1, ind + 1) lst[index], lst[ind] = lst[ind], lst[index] if found == False: find(lst, k, ind + 1) l = [int(i) for i in s] find(l, k, 0) return mx
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER RETURN ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR
Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times. Example 1: Input: K = 4 str = "1234567" Output: 7654321 Explanation: Three swaps can make the input 1234567 to 7654321, swapping 1 with 7, 2 with 6 and finally 3 with 5 Example 2: Input: K = 3 str = "3435335" Output: 5543333 Explanation: Three swaps can make the input 3435335 to 5543333, swapping 3 with 5, 4 with 5 and finally 3 with 4 Your task: You don't have to read input or print anything. Your task is to complete the function findMaximumNum() which takes the string and an integer as input and returns a string containing the largest number formed by perfoming the swap operation at most k times. Expected Time Complexity: O(n!/(n-k)!) , where n = length of input string Expected Auxiliary Space: O(n) Constraints: 1 ≤ |str| ≤ 30 1 ≤ K ≤ 10
class Solution: def findMaximumNum(self, s, k): maxi = [float("-inf")] arr = [char for char in s] def swap(i, j, arr): arr[i], arr[j] = arr[j], arr[i] def helper(i, arr, k): if k == 0 or i == len(arr): return maxChar = arr[i] for idx in range(i + 1, len(arr)): if int(maxChar) < int(arr[idx]): maxChar = arr[idx] if maxChar != arr[i]: k -= 1 for idx in range(len(arr) - 1, i - 1, -1): if arr[idx] == maxChar: swap(idx, i, arr) maxi[0] = max(maxi[0], int("".join(arr))) helper(i + 1, arr, k) swap(idx, i, arr) helper(0, arr, k) return maxi[0]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR RETURN VAR NUMBER
Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times. Example 1: Input: K = 4 str = "1234567" Output: 7654321 Explanation: Three swaps can make the input 1234567 to 7654321, swapping 1 with 7, 2 with 6 and finally 3 with 5 Example 2: Input: K = 3 str = "3435335" Output: 5543333 Explanation: Three swaps can make the input 3435335 to 5543333, swapping 3 with 5, 4 with 5 and finally 3 with 4 Your task: You don't have to read input or print anything. Your task is to complete the function findMaximumNum() which takes the string and an integer as input and returns a string containing the largest number formed by perfoming the swap operation at most k times. Expected Time Complexity: O(n!/(n-k)!) , where n = length of input string Expected Auxiliary Space: O(n) Constraints: 1 ≤ |str| ≤ 30 1 ≤ K ≤ 10
class Solution: def findMaximumNum(self, s, k): arr = list(s) ans = [s] def rec(i, c): if i == len(s) or c == k: ans[0] = max(ans[0], "".join(arr)) return maxch = max([ord(ch) for ch in arr[i:]]) if ord(arr[i]) == maxch: rec(i + 1, c) return for j in range(i + 1, len(s)): if ord(arr[j]) == maxch: arr[i], arr[j] = arr[j], arr[i] rec(i + 1, c + 1) arr[i], arr[j] = arr[j], arr[i] rec(0, 0) return ans[0]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL STRING VAR RETURN ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER RETURN VAR NUMBER
Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times. Example 1: Input: K = 4 str = "1234567" Output: 7654321 Explanation: Three swaps can make the input 1234567 to 7654321, swapping 1 with 7, 2 with 6 and finally 3 with 5 Example 2: Input: K = 3 str = "3435335" Output: 5543333 Explanation: Three swaps can make the input 3435335 to 5543333, swapping 3 with 5, 4 with 5 and finally 3 with 4 Your task: You don't have to read input or print anything. Your task is to complete the function findMaximumNum() which takes the string and an integer as input and returns a string containing the largest number formed by perfoming the swap operation at most k times. Expected Time Complexity: O(n!/(n-k)!) , where n = length of input string Expected Auxiliary Space: O(n) Constraints: 1 ≤ |str| ≤ 30 1 ≤ K ≤ 10
class Solution: def findMaximumNum(self, s, k): s = list(s) self.ans = "0" def swap(s, k, left, t): if t == k or left >= len(s): tmp = "".join(s) if tmp > self.ans: self.ans = tmp if left < len(s) and t < k: cur_max = s[left] ind = left flag = 0 for i in range(left + 1, len(s)): if s[i] > cur_max: cur_max = s[i] ind = i flag = 1 if flag == 1: for j in range(left + 1, len(s)): if s[j] == cur_max: s[left], s[j] = s[j], s[left] left += 1 t += 1 swap(s, k, left, t) left -= 1 t -= 1 s[left], s[j] = s[j], s[left] else: swap(s, k, left + 1, t) left = 0 t = 0 swap(s, k, left, t) return self.ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FUNC_DEF IF VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF VAR VAR ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR
Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times. Example 1: Input: K = 4 str = "1234567" Output: 7654321 Explanation: Three swaps can make the input 1234567 to 7654321, swapping 1 with 7, 2 with 6 and finally 3 with 5 Example 2: Input: K = 3 str = "3435335" Output: 5543333 Explanation: Three swaps can make the input 3435335 to 5543333, swapping 3 with 5, 4 with 5 and finally 3 with 4 Your task: You don't have to read input or print anything. Your task is to complete the function findMaximumNum() which takes the string and an integer as input and returns a string containing the largest number formed by perfoming the swap operation at most k times. Expected Time Complexity: O(n!/(n-k)!) , where n = length of input string Expected Auxiliary Space: O(n) Constraints: 1 ≤ |str| ≤ 30 1 ≤ K ≤ 10
class Solution: def findMaximumNum(self, s, k): st = list(s) self.m = s def solve(st, i, k): if i >= len(st) or k == 0: return mxelm = st[i] for j in range(i + 1, len(st)): if int(st[j]) > int(mxelm): mxelm = st[j] if mxelm != st[i]: k -= 1 for j in range(i, len(st)): if st[j] == mxelm: st[j], st[i] = st[i], st[j] new = "".join(st) if int(new) > int(self.m): self.m = new solve(st, i + 1, k) st[j], st[i] = st[i], st[j] solve(st, 0, k) return self.m
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR VAR NUMBER RETURN ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR 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 VAR RETURN VAR
Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times. Example 1: Input: K = 4 str = "1234567" Output: 7654321 Explanation: Three swaps can make the input 1234567 to 7654321, swapping 1 with 7, 2 with 6 and finally 3 with 5 Example 2: Input: K = 3 str = "3435335" Output: 5543333 Explanation: Three swaps can make the input 3435335 to 5543333, swapping 3 with 5, 4 with 5 and finally 3 with 4 Your task: You don't have to read input or print anything. Your task is to complete the function findMaximumNum() which takes the string and an integer as input and returns a string containing the largest number formed by perfoming the swap operation at most k times. Expected Time Complexity: O(n!/(n-k)!) , where n = length of input string Expected Auxiliary Space: O(n) Constraints: 1 ≤ |str| ≤ 30 1 ≤ K ≤ 10
class Solution: def findMaximumNum(self, s, k): res = int(s) n = len(s) def swap(strg, i, j): if i == j: return strg ith = strg[i] jth = strg[j] left = strg[0:i] middle = strg[i + 1 : j] right = strg[j + 1 :] return left + jth + middle + ith + right def solve(strg, n, k, ind): nonlocal res if ind >= n or k == 0: return mx = strg[ind] for i in range(ind + 1, n): mx = max(mx, strg[i]) if mx != strg[ind]: k -= 1 for i in range(n - 1, ind - 1, -1): if strg[i] == mx: strg = swap(strg, ind, i) if int(strg) > res: res = int(strg) solve(strg, n, k, ind + 1) strg = swap(strg, ind, i) solve(s, n, k, 0) return res
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR FUNC_DEF IF VAR VAR VAR NUMBER RETURN ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR
Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times. Example 1: Input: K = 4 str = "1234567" Output: 7654321 Explanation: Three swaps can make the input 1234567 to 7654321, swapping 1 with 7, 2 with 6 and finally 3 with 5 Example 2: Input: K = 3 str = "3435335" Output: 5543333 Explanation: Three swaps can make the input 3435335 to 5543333, swapping 3 with 5, 4 with 5 and finally 3 with 4 Your task: You don't have to read input or print anything. Your task is to complete the function findMaximumNum() which takes the string and an integer as input and returns a string containing the largest number formed by perfoming the swap operation at most k times. Expected Time Complexity: O(n!/(n-k)!) , where n = length of input string Expected Auxiliary Space: O(n) Constraints: 1 ≤ |str| ≤ 30 1 ≤ K ≤ 10
class Solution: def findMaximumNum(self, s, k): s = list(s) maxStr = [""] getMaximumNum(s, 0, 0, k, maxStr) ansStr = "" return maxStr[0] def getGreatestNumber(s, idx): minVal = 0 ans = 0 for i in range(idx, len(s)): if ans <= int(s[i]): ans = int(s[i]) minVal = i return minVal def getLar(s, s2): str1 = "".join(s) if str1 > s2: s2 = str1 return s2 def getMaximumNum(s, idx, count, k, maxStr): if k == count: maxStr[0] = getLar(s, maxStr[0]) return if idx == len(s): maxStr[0] = getLar(s, maxStr[0]) return getIdx = getGreatestNumber(s, idx) if s[getIdx] != s[idx]: count += 1 for i in range(len(s)): if s[i] == s[getIdx]: s[i], s[idx] = s[idx], s[i] getMaximumNum(s, idx + 1, count, k, maxStr) s[i], s[idx] = s[idx], s[i]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST STRING EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR STRING RETURN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL STRING VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER RETURN IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR
Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times. Example 1: Input: K = 4 str = "1234567" Output: 7654321 Explanation: Three swaps can make the input 1234567 to 7654321, swapping 1 with 7, 2 with 6 and finally 3 with 5 Example 2: Input: K = 3 str = "3435335" Output: 5543333 Explanation: Three swaps can make the input 3435335 to 5543333, swapping 3 with 5, 4 with 5 and finally 3 with 4 Your task: You don't have to read input or print anything. Your task is to complete the function findMaximumNum() which takes the string and an integer as input and returns a string containing the largest number formed by perfoming the swap operation at most k times. Expected Time Complexity: O(n!/(n-k)!) , where n = length of input string Expected Auxiliary Space: O(n) Constraints: 1 ≤ |str| ≤ 30 1 ≤ K ≤ 10
class Solution: def helper(self, s, ans, i, k, n): if k == 0 or i >= n: return s = list(s) mx = max(s[i:]) if mx != s[i]: k -= 1 for x in range(i, n): if s[x] == mx: s[i], s[x] = s[x], s[i] ans[0] = max(ans[0], "".join(s)) self.helper("".join(s), ans, i + 1, k, n) s[i], s[x] = s[x], s[i] def findMaximumNum(self, s, k): ans = [""] self.helper(s, ans, 0, k, len(s)) return ans[0]
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST STRING EXPR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR NUMBER
Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times. Example 1: Input: K = 4 str = "1234567" Output: 7654321 Explanation: Three swaps can make the input 1234567 to 7654321, swapping 1 with 7, 2 with 6 and finally 3 with 5 Example 2: Input: K = 3 str = "3435335" Output: 5543333 Explanation: Three swaps can make the input 3435335 to 5543333, swapping 3 with 5, 4 with 5 and finally 3 with 4 Your task: You don't have to read input or print anything. Your task is to complete the function findMaximumNum() which takes the string and an integer as input and returns a string containing the largest number formed by perfoming the swap operation at most k times. Expected Time Complexity: O(n!/(n-k)!) , where n = length of input string Expected Auxiliary Space: O(n) Constraints: 1 ≤ |str| ≤ 30 1 ≤ K ≤ 10
class Solution: def match(self, num, res): for i in range(len(num)): if res[i] > num[i]: return if res[i] < num[i]: for i in range(len(num)): res[i] = num[i] return def setDigit(self, num, index, res, k): if k == 0 or index == len(num) - 1: self.match(num, res) return maxDigit = 0 for i in range(index, len(num)): maxDigit = max(maxDigit, num[i]) if num[index] == maxDigit: self.setDigit(num, index + 1, res, k) return for i in range(index + 1, len(num)): if num[i] == maxDigit: num[index], num[i] = num[i], num[index] self.setDigit(num, index + 1, res, k - 1) num[index], num[i] = num[i], num[index] def findMaximumNum(self, s, k): num = [int(x) for x in s] res = [int(x) for x in s] self.setDigit(num, 0, res, k) return "".join(str(x) for x in res)
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR RETURN FUNC_DEF IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times. Example 1: Input: K = 4 str = "1234567" Output: 7654321 Explanation: Three swaps can make the input 1234567 to 7654321, swapping 1 with 7, 2 with 6 and finally 3 with 5 Example 2: Input: K = 3 str = "3435335" Output: 5543333 Explanation: Three swaps can make the input 3435335 to 5543333, swapping 3 with 5, 4 with 5 and finally 3 with 4 Your task: You don't have to read input or print anything. Your task is to complete the function findMaximumNum() which takes the string and an integer as input and returns a string containing the largest number formed by perfoming the swap operation at most k times. Expected Time Complexity: O(n!/(n-k)!) , where n = length of input string Expected Auxiliary Space: O(n) Constraints: 1 ≤ |str| ≤ 30 1 ≤ K ≤ 10
class Solution: def findMaximumNum(self, s, k): def eval_(s): count = 0 for c in s: if c == "0": count += 1 else: break return eval(s[count:]) max_num = [eval_(s)] def helper(s, start, k): if k == 0: max_num[0] = max(eval_(s), max_num[0]) return max_num[0] = max(eval_(s), max_num[0]) for i in range(start, len(s) - 1): max_ind = i for j in range(i + 1, len(s)): if int(s[j]) > int(s[max_ind]): max_ind = j if max_ind == i: continue for jj in range(i + 1, len(s)): if s[jj] != s[max_ind]: continue j = jj new_s = s[:i] + s[j] + s[i + 1 : j] + s[i] + s[j + 1 :] if new_s[0] == "0": continue helper(new_s, i + 1, k - 1) helper(s, 0, k) return max_num[0]
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER RETURN ASSIGN VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR RETURN VAR NUMBER
Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times. Example 1: Input: K = 4 str = "1234567" Output: 7654321 Explanation: Three swaps can make the input 1234567 to 7654321, swapping 1 with 7, 2 with 6 and finally 3 with 5 Example 2: Input: K = 3 str = "3435335" Output: 5543333 Explanation: Three swaps can make the input 3435335 to 5543333, swapping 3 with 5, 4 with 5 and finally 3 with 4 Your task: You don't have to read input or print anything. Your task is to complete the function findMaximumNum() which takes the string and an integer as input and returns a string containing the largest number formed by perfoming the swap operation at most k times. Expected Time Complexity: O(n!/(n-k)!) , where n = length of input string Expected Auxiliary Space: O(n) Constraints: 1 ≤ |str| ≤ 30 1 ≤ K ≤ 10
class Solution: def findMaximumNum(self, s, k): maxi = [int(s)] idx = 0 chars = [char for char in s] self.findMaxUtil(chars, idx, k, maxi) return maxi[0] def findMaxUtil(self, s, idx, k, maxi): if idx > len(s) - 1 or k <= 0: return max_val = s[idx] for i in range(idx + 1, len(s)): if s[i] > max_val: max_val = s[i] if max_val != s[idx]: k -= 1 for i in range(idx, len(s)): if s[i] == max_val: s[idx], s[i] = s[i], s[idx] new_word = "".join(s) if int(new_word) > maxi[0]: maxi[0] = int(new_word) self.findMaxUtil(s, idx + 1, k, maxi) s[idx], s[i] = s[i], s[idx]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR NUMBER FUNC_DEF IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR
Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times. Example 1: Input: K = 4 str = "1234567" Output: 7654321 Explanation: Three swaps can make the input 1234567 to 7654321, swapping 1 with 7, 2 with 6 and finally 3 with 5 Example 2: Input: K = 3 str = "3435335" Output: 5543333 Explanation: Three swaps can make the input 3435335 to 5543333, swapping 3 with 5, 4 with 5 and finally 3 with 4 Your task: You don't have to read input or print anything. Your task is to complete the function findMaximumNum() which takes the string and an integer as input and returns a string containing the largest number formed by perfoming the swap operation at most k times. Expected Time Complexity: O(n!/(n-k)!) , where n = length of input string Expected Auxiliary Space: O(n) Constraints: 1 ≤ |str| ≤ 30 1 ≤ K ≤ 10
class Solution: def helper(self, num, k, ans, ind): n = len(num) if k <= 0 or ind >= n - 1: return maxm = num[ind] for i in range(ind + 1, n): if int(maxm) < int(num[i]): maxm = num[i] if maxm != num[ind]: k -= 1 for i in range(ind, n): if num[i] == maxm: num[i], num[ind] = num[ind], num[i] temp = "".join(num) if int(temp) > int(ans[0]): ans[0] = temp self.helper(num, k, ans, ind + 1) num[i], num[ind] = num[ind], num[i] def findMaximumNum(self, s, k): num = list(s) ans = [s] self.helper(num, k, ans, 0) return ans[0]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER RETURN ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR NUMBER
Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times. Example 1: Input: K = 4 str = "1234567" Output: 7654321 Explanation: Three swaps can make the input 1234567 to 7654321, swapping 1 with 7, 2 with 6 and finally 3 with 5 Example 2: Input: K = 3 str = "3435335" Output: 5543333 Explanation: Three swaps can make the input 3435335 to 5543333, swapping 3 with 5, 4 with 5 and finally 3 with 4 Your task: You don't have to read input or print anything. Your task is to complete the function findMaximumNum() which takes the string and an integer as input and returns a string containing the largest number formed by perfoming the swap operation at most k times. Expected Time Complexity: O(n!/(n-k)!) , where n = length of input string Expected Auxiliary Space: O(n) Constraints: 1 ≤ |str| ≤ 30 1 ≤ K ≤ 10
class Solution: def findMaximumNum(self, s, k): n = len(s) s = list(s) i = 0 count = 0 def helper(s, count, i, k, n): if count == k or i >= n - 1: return "".join(s) output = helper(s, count, i + 1, k, n) maxl = s[i] for j in range(i + 1, n): if s[j] >= maxl: maxl = s[j] for j in range(i + 1, n): if s[j] == maxl: s[i], s[j] = s[j], s[i] news = helper(s, count + 1, i + 1, k, n) if output < news: output = news s[j], s[i] = s[i], s[j] return output return helper(s, count, i, k, n)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR
Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times. Example 1: Input: K = 4 str = "1234567" Output: 7654321 Explanation: Three swaps can make the input 1234567 to 7654321, swapping 1 with 7, 2 with 6 and finally 3 with 5 Example 2: Input: K = 3 str = "3435335" Output: 5543333 Explanation: Three swaps can make the input 3435335 to 5543333, swapping 3 with 5, 4 with 5 and finally 3 with 4 Your task: You don't have to read input or print anything. Your task is to complete the function findMaximumNum() which takes the string and an integer as input and returns a string containing the largest number formed by perfoming the swap operation at most k times. Expected Time Complexity: O(n!/(n-k)!) , where n = length of input string Expected Auxiliary Space: O(n) Constraints: 1 ≤ |str| ≤ 30 1 ≤ K ≤ 10
class Solution: def findMaximumNum(self, s, k): num = [int(x) for x in s] res = [int(x) for x in s] self.find(num, res, 0, k) return "".join(str(x) for x in res) def find(self, num, res, index, k): if k == 0 or index == len(num) - 1: self.match(num, res) return maxdigit = 0 for i in range(index, len(num)): maxdigit = max(maxdigit, num[i]) if num[index] == maxdigit: self.find(num, res, index + 1, k) return for i in range(index + 1, len(num)): if num[i] == maxdigit: num[index], num[i] = num[i], num[index] self.find(num, res, index + 1, k - 1) num[index], num[i] = num[i], num[index] def match(self, num, res): for i in range(len(num)): if res[i] > num[i]: return if res[i] < num[i]: for j in range(len(num)): res[j] = num[j] return
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR RETURN
Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times. Example 1: Input: K = 4 str = "1234567" Output: 7654321 Explanation: Three swaps can make the input 1234567 to 7654321, swapping 1 with 7, 2 with 6 and finally 3 with 5 Example 2: Input: K = 3 str = "3435335" Output: 5543333 Explanation: Three swaps can make the input 3435335 to 5543333, swapping 3 with 5, 4 with 5 and finally 3 with 4 Your task: You don't have to read input or print anything. Your task is to complete the function findMaximumNum() which takes the string and an integer as input and returns a string containing the largest number formed by perfoming the swap operation at most k times. Expected Time Complexity: O(n!/(n-k)!) , where n = length of input string Expected Auxiliary Space: O(n) Constraints: 1 ≤ |str| ≤ 30 1 ≤ K ≤ 10
class Solution: def findMaximumNum(self, s, k): def f(arr, start, k): if k == 0: return if start == len(arr): return maximum = max(arr[start : len(arr)]) if arr[start] != maximum: k -= 1 for i in range(start, len(arr)): if arr[i] == maximum: arr[start], arr[i] = arr[i], arr[start] newNo = int("".join([str(ch) for ch in arr])) if newNo > self.maximumNo: self.maximumNo = newNo f(arr, start + 1, k) arr[start], arr[i] = arr[i], arr[start] arr = [int(ch) for ch in s] self.maximumNo = int("".join([str(ch) for ch in arr])) f(arr, 0, k) return self.maximumNo
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER RETURN IF VAR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR RETURN VAR
Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times. Example 1: Input: K = 4 str = "1234567" Output: 7654321 Explanation: Three swaps can make the input 1234567 to 7654321, swapping 1 with 7, 2 with 6 and finally 3 with 5 Example 2: Input: K = 3 str = "3435335" Output: 5543333 Explanation: Three swaps can make the input 3435335 to 5543333, swapping 3 with 5, 4 with 5 and finally 3 with 4 Your task: You don't have to read input or print anything. Your task is to complete the function findMaximumNum() which takes the string and an integer as input and returns a string containing the largest number formed by perfoming the swap operation at most k times. Expected Time Complexity: O(n!/(n-k)!) , where n = length of input string Expected Auxiliary Space: O(n) Constraints: 1 ≤ |str| ≤ 30 1 ≤ K ≤ 10
class Solution: def findMaximumNum(self, s, k): def swap(arr, i, j): arr[i], arr[j] = arr[j], arr[i] def solve(idx, k, n): if k == 0 or idx >= n: return rmax = arr[idx] for j in range(idx + 1, n): if arr[j] > rmax: rmax = arr[j] if rmax == arr[idx]: solve(idx + 1, k, n) return for j in range(idx, n): if arr[j] == rmax: swap(arr, idx, j) temp = "".join(arr) ans[0] = max(ans[0], temp) solve(idx + 1, k - 1, n) swap(arr, idx, j) arr = list(s) n = len(arr) ans = [s] solve(0, k, n) return ans[0]
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR VAR RETURN ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR EXPR FUNC_CALL VAR NUMBER VAR VAR RETURN VAR NUMBER
Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times. Example 1: Input: K = 4 str = "1234567" Output: 7654321 Explanation: Three swaps can make the input 1234567 to 7654321, swapping 1 with 7, 2 with 6 and finally 3 with 5 Example 2: Input: K = 3 str = "3435335" Output: 5543333 Explanation: Three swaps can make the input 3435335 to 5543333, swapping 3 with 5, 4 with 5 and finally 3 with 4 Your task: You don't have to read input or print anything. Your task is to complete the function findMaximumNum() which takes the string and an integer as input and returns a string containing the largest number formed by perfoming the swap operation at most k times. Expected Time Complexity: O(n!/(n-k)!) , where n = length of input string Expected Auxiliary Space: O(n) Constraints: 1 ≤ |str| ≤ 30 1 ≤ K ≤ 10
class Solution: def findMaximumNum(self, s, k): string = [x for x in s] maxn = [int(s)] def maxi(maxn, string, index, k): if k == 0 or index == len(string): return curr = string[index] for i in range(index + 1, len(string)): if string[i] > curr: curr = string[i] if curr != string[index]: k = k - 1 for i in range(index, len(string)): if string[i] == curr: string[index], string[i] = string[i], string[index] new = "".join(string) if int(new) > maxn[0]: maxn[0] = int(new) maxi(maxn, string, index + 1, k) string[index], string[i] = string[i], string[index] maxi(maxn, string, 0, k) return str(maxn[0])
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER VAR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR RETURN FUNC_CALL VAR VAR NUMBER
Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times. Example 1: Input: K = 4 str = "1234567" Output: 7654321 Explanation: Three swaps can make the input 1234567 to 7654321, swapping 1 with 7, 2 with 6 and finally 3 with 5 Example 2: Input: K = 3 str = "3435335" Output: 5543333 Explanation: Three swaps can make the input 3435335 to 5543333, swapping 3 with 5, 4 with 5 and finally 3 with 4 Your task: You don't have to read input or print anything. Your task is to complete the function findMaximumNum() which takes the string and an integer as input and returns a string containing the largest number formed by perfoming the swap operation at most k times. Expected Time Complexity: O(n!/(n-k)!) , where n = length of input string Expected Auxiliary Space: O(n) Constraints: 1 ≤ |str| ≤ 30 1 ≤ K ≤ 10
class Solution: def findMaximumNum(self, s, k): def fun(a, k, ans): if a == []: return int(ans) maxi = max(a) idx = a.index(maxi) res = 0 if idx != 0 and k != 0: for i in range(len(a)): if a[i] == maxi: a[0], a[i] = a[i], a[0] res = max(res, fun(a[1:], k - 1, ans + str(a[0]))) a[0], a[i] = a[i], a[0] else: return fun(a[1:], k, ans + str(a[0])) return res a = [] for i in s: a.append(i) return fun(a, k, "")
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR LIST RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR STRING
Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times. Example 1: Input: K = 4 str = "1234567" Output: 7654321 Explanation: Three swaps can make the input 1234567 to 7654321, swapping 1 with 7, 2 with 6 and finally 3 with 5 Example 2: Input: K = 3 str = "3435335" Output: 5543333 Explanation: Three swaps can make the input 3435335 to 5543333, swapping 3 with 5, 4 with 5 and finally 3 with 4 Your task: You don't have to read input or print anything. Your task is to complete the function findMaximumNum() which takes the string and an integer as input and returns a string containing the largest number formed by perfoming the swap operation at most k times. Expected Time Complexity: O(n!/(n-k)!) , where n = length of input string Expected Auxiliary Space: O(n) Constraints: 1 ≤ |str| ≤ 30 1 ≤ K ≤ 10
import sys class Solution: def swap(self, str, i, j): s = list(str) s[i], s[j] = s[j], s[i] return "".join(s) def findMaximumNum(self, s, k): import sys sys.setrecursionlimit(10000000) result = [""] self.findMax(0, s, k, result) return result[0] def findMax(self, index, s, k, result): if k == 0 or index >= len(s) - 1: return maxchar = s[index] for i in range(index + 1, len(s)): if s[i] > maxchar: maxchar = s[i] if maxchar != s[index]: k -= 1 for j in range(len(s) - 1, index - 1, -1): if s[j] == maxchar: s = self.swap(s, index, j) if s > result[0]: result[0] = s[:] self.findMax(index + 1, s, k, result) s = self.swap(s, index, j)
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL STRING VAR FUNC_DEF IMPORT EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST STRING EXPR FUNC_CALL VAR NUMBER VAR VAR VAR RETURN VAR NUMBER FUNC_DEF IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR
Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times. Example 1: Input: K = 4 str = "1234567" Output: 7654321 Explanation: Three swaps can make the input 1234567 to 7654321, swapping 1 with 7, 2 with 6 and finally 3 with 5 Example 2: Input: K = 3 str = "3435335" Output: 5543333 Explanation: Three swaps can make the input 3435335 to 5543333, swapping 3 with 5, 4 with 5 and finally 3 with 4 Your task: You don't have to read input or print anything. Your task is to complete the function findMaximumNum() which takes the string and an integer as input and returns a string containing the largest number formed by perfoming the swap operation at most k times. Expected Time Complexity: O(n!/(n-k)!) , where n = length of input string Expected Auxiliary Space: O(n) Constraints: 1 ≤ |str| ≤ 30 1 ≤ K ≤ 10
class Solution: def findMaximumNum(self, s, k): s = list(s) n = len(s) maximum = -float("inf") def backtrack(k, idx): nonlocal maximum if k == 0 or idx == n: return curr_max = s[idx] for j in range(idx + 1, n): if j < n and int(s[j]) > int(curr_max): curr_max = s[j] if idx < n and curr_max != s[idx]: k -= 1 for j in range(idx, n): if s[j] == curr_max: s[idx], s[j] = s[j], s[idx] maximum = max(maximum, int("".join(s))) backtrack(k, idx + 1) s[idx], s[j] = s[j], s[idx] backtrack(k, 0) return maximum
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF IF VAR NUMBER VAR VAR RETURN ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR
Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times. Example 1: Input: K = 4 str = "1234567" Output: 7654321 Explanation: Three swaps can make the input 1234567 to 7654321, swapping 1 with 7, 2 with 6 and finally 3 with 5 Example 2: Input: K = 3 str = "3435335" Output: 5543333 Explanation: Three swaps can make the input 3435335 to 5543333, swapping 3 with 5, 4 with 5 and finally 3 with 4 Your task: You don't have to read input or print anything. Your task is to complete the function findMaximumNum() which takes the string and an integer as input and returns a string containing the largest number formed by perfoming the swap operation at most k times. Expected Time Complexity: O(n!/(n-k)!) , where n = length of input string Expected Auxiliary Space: O(n) Constraints: 1 ≤ |str| ≤ 30 1 ≤ K ≤ 10
class Solution: def findMaximumNum(self, s, k): def backtrack(string, k, start): nonlocal maxi if k <= 0 or start >= len(string) - 1: return cur_digit = string[start] for i in range(start + 1, len(string)): if cur_digit < string[i]: cur_digit = string[i] if cur_digit != string[start]: k -= 1 for i in range(start, len(string)): if cur_digit == string[i]: string[i], string[start] = string[start], string[i] new_string = "".join(string) if maxi < new_string: maxi = new_string backtrack(string, k, start + 1) string[i], string[start] = string[start], string[i] maxi = s string = [char for char in s] backtrack(string, k, 0) return maxi
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR
Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times. Example 1: Input: K = 4 str = "1234567" Output: 7654321 Explanation: Three swaps can make the input 1234567 to 7654321, swapping 1 with 7, 2 with 6 and finally 3 with 5 Example 2: Input: K = 3 str = "3435335" Output: 5543333 Explanation: Three swaps can make the input 3435335 to 5543333, swapping 3 with 5, 4 with 5 and finally 3 with 4 Your task: You don't have to read input or print anything. Your task is to complete the function findMaximumNum() which takes the string and an integer as input and returns a string containing the largest number formed by perfoming the swap operation at most k times. Expected Time Complexity: O(n!/(n-k)!) , where n = length of input string Expected Auxiliary Space: O(n) Constraints: 1 ≤ |str| ≤ 30 1 ≤ K ≤ 10
class Solution: def findMaximumNum(self, s, k): N = len(s) input = list(s) return self.permutateBetter(input, s, k, 0, N) def permutateBetter(self, input, s, k, idx, N): if k == 0 or idx == N: return s maxm = input[idx] for v in range(idx + 1, N): maxm = max(maxm, input[v]) if maxm != input[idx]: k -= 1 for j in range(idx, N): if input[j] == maxm: input[idx], input[j] = input[j], input[idx] s = max(s, "".join(input)) s = self.permutateBetter(input, s, k, idx + 1, N) input[idx], input[j] = input[j], input[idx] return s
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR NUMBER VAR FUNC_DEF IF VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR
Consider a grid of size $n \times n$. The rows are numbered top to bottom from $1$ to $n$, the columns are numbered left to right from $1$ to $n$. The robot is positioned in a cell $(1, 1)$. It can perform two types of moves: D — move one cell down; R — move one cell right. The robot is not allowed to move outside the grid. You are given a sequence of moves $s$ — the initial path of the robot. This path doesn't lead the robot outside the grid. You are allowed to perform an arbitrary number of modifications to it (possibly, zero). With one modification, you can duplicate one move in the sequence. That is, replace a single occurrence of D with DD or a single occurrence of R with RR. Count the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of testcases. The first line of each testcase contains the single integer $n$ ($2 \le n \le 10^8$) — the number of rows and columns in the grid. The second line of each testcase contains a non-empty string $s$, consisting only of characters D and R, — the initial path of the robot. This path doesn't lead the robot outside the grid. The total length of strings $s$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print a single integer — the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. -----Examples----- Input 3 4 RD 5 DRDRDRDR 3 D Output 13 9 3 -----Note----- In the first testcase, it's enough to consider the following modified paths: RD $\rightarrow$ RRD $\rightarrow$ RRRD $\rightarrow$ RRRDD $\rightarrow$ RRRDDD — this path visits cells $(1, 1)$, $(1, 2)$, $(1, 3)$, $(1, 4)$, $(2, 4)$, $(3, 4)$ and $(4, 4)$; RD $\rightarrow$ RRD $\rightarrow$ RRDD $\rightarrow$ RRDDD — this path visits cells $(1, 1)$, $(1, 2)$, $(1, 3)$, $(2, 3)$, $(3, 3)$ and $(4, 3)$; RD $\rightarrow$ RDD $\rightarrow$ RDDD — this path visits cells $(1, 1)$, $(1, 2)$, $(2, 2)$, $(3, 2)$ and $(4, 2)$. Thus, the cells that are visited on at least one modified path are: $(1, 1)$, $(1, 2)$, $(1, 3)$, $(1, 4)$, $(2, 2)$, $(2, 3)$, $(2, 4)$, $(3, 2)$, $(3, 3)$, $(3, 4)$, $(4, 2)$, $(4, 3)$ and $(4, 4)$. In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR. In the third testcase, the cells that are visited on at least one modified path are: $(1, 1)$, $(2, 1)$ and $(3, 1)$. Here are the cells for all testcases:
Z = input for _ in range(int(Z())): m = int(Z()) s = Z() n = len(s) y = chr(150 - ord(s[0])) x = (s + y).index(y) print( 1 + n + (m - x) * m - (1 + s.count("D", x)) * (1 + s.count("R", x)) if x < n else m )
ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR STRING VAR BIN_OP NUMBER FUNC_CALL VAR STRING VAR VAR
Consider a grid of size $n \times n$. The rows are numbered top to bottom from $1$ to $n$, the columns are numbered left to right from $1$ to $n$. The robot is positioned in a cell $(1, 1)$. It can perform two types of moves: D — move one cell down; R — move one cell right. The robot is not allowed to move outside the grid. You are given a sequence of moves $s$ — the initial path of the robot. This path doesn't lead the robot outside the grid. You are allowed to perform an arbitrary number of modifications to it (possibly, zero). With one modification, you can duplicate one move in the sequence. That is, replace a single occurrence of D with DD or a single occurrence of R with RR. Count the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of testcases. The first line of each testcase contains the single integer $n$ ($2 \le n \le 10^8$) — the number of rows and columns in the grid. The second line of each testcase contains a non-empty string $s$, consisting only of characters D and R, — the initial path of the robot. This path doesn't lead the robot outside the grid. The total length of strings $s$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print a single integer — the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. -----Examples----- Input 3 4 RD 5 DRDRDRDR 3 D Output 13 9 3 -----Note----- In the first testcase, it's enough to consider the following modified paths: RD $\rightarrow$ RRD $\rightarrow$ RRRD $\rightarrow$ RRRDD $\rightarrow$ RRRDDD — this path visits cells $(1, 1)$, $(1, 2)$, $(1, 3)$, $(1, 4)$, $(2, 4)$, $(3, 4)$ and $(4, 4)$; RD $\rightarrow$ RRD $\rightarrow$ RRDD $\rightarrow$ RRDDD — this path visits cells $(1, 1)$, $(1, 2)$, $(1, 3)$, $(2, 3)$, $(3, 3)$ and $(4, 3)$; RD $\rightarrow$ RDD $\rightarrow$ RDDD — this path visits cells $(1, 1)$, $(1, 2)$, $(2, 2)$, $(3, 2)$ and $(4, 2)$. Thus, the cells that are visited on at least one modified path are: $(1, 1)$, $(1, 2)$, $(1, 3)$, $(1, 4)$, $(2, 2)$, $(2, 3)$, $(2, 4)$, $(3, 2)$, $(3, 3)$, $(3, 4)$, $(4, 2)$, $(4, 3)$ and $(4, 4)$. In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR. In the third testcase, the cells that are visited on at least one modified path are: $(1, 1)$, $(2, 1)$ and $(3, 1)$. Here are the cells for all testcases:
def main(): for _ in range(int(input())): n = int(input()) s = input() t = "".join([("D" if i == "R" else "R") for i in s]) poss = s.find("R") post = t.find("R") if s == "R" * len(s) or s == "D" * len(s): print(n) continue res, cnta, cntb = poss * (n - 1) + post * (n - 1), 0, 0 for i in range(len(s) - 1, poss - 1, -1): if s[i] == "D": res += cnta else: cnta += 1 for i in range(len(t) - 1, post - 1, -1): if t[i] == "D": res += cntb else: cntb += 1 print(n * n - res) main()
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING VAR STRING STRING STRING VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR BIN_OP STRING FUNC_CALL VAR VAR VAR BIN_OP STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR
Consider a grid of size $n \times n$. The rows are numbered top to bottom from $1$ to $n$, the columns are numbered left to right from $1$ to $n$. The robot is positioned in a cell $(1, 1)$. It can perform two types of moves: D — move one cell down; R — move one cell right. The robot is not allowed to move outside the grid. You are given a sequence of moves $s$ — the initial path of the robot. This path doesn't lead the robot outside the grid. You are allowed to perform an arbitrary number of modifications to it (possibly, zero). With one modification, you can duplicate one move in the sequence. That is, replace a single occurrence of D with DD or a single occurrence of R with RR. Count the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of testcases. The first line of each testcase contains the single integer $n$ ($2 \le n \le 10^8$) — the number of rows and columns in the grid. The second line of each testcase contains a non-empty string $s$, consisting only of characters D and R, — the initial path of the robot. This path doesn't lead the robot outside the grid. The total length of strings $s$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print a single integer — the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. -----Examples----- Input 3 4 RD 5 DRDRDRDR 3 D Output 13 9 3 -----Note----- In the first testcase, it's enough to consider the following modified paths: RD $\rightarrow$ RRD $\rightarrow$ RRRD $\rightarrow$ RRRDD $\rightarrow$ RRRDDD — this path visits cells $(1, 1)$, $(1, 2)$, $(1, 3)$, $(1, 4)$, $(2, 4)$, $(3, 4)$ and $(4, 4)$; RD $\rightarrow$ RRD $\rightarrow$ RRDD $\rightarrow$ RRDDD — this path visits cells $(1, 1)$, $(1, 2)$, $(1, 3)$, $(2, 3)$, $(3, 3)$ and $(4, 3)$; RD $\rightarrow$ RDD $\rightarrow$ RDDD — this path visits cells $(1, 1)$, $(1, 2)$, $(2, 2)$, $(3, 2)$ and $(4, 2)$. Thus, the cells that are visited on at least one modified path are: $(1, 1)$, $(1, 2)$, $(1, 3)$, $(1, 4)$, $(2, 2)$, $(2, 3)$, $(2, 4)$, $(3, 2)$, $(3, 3)$, $(3, 4)$, $(4, 2)$, $(4, 3)$ and $(4, 4)$. In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR. In the third testcase, the cells that are visited on at least one modified path are: $(1, 1)$, $(2, 1)$ and $(3, 1)$. Here are the cells for all testcases:
def calc(s, n): ld = s.find("R") res = ld * (n - 1) y = 0 for i in range(len(s) - 1, ld - 1, -1): if s[i] == "D": res += y else: y += 1 return res out = "" for _ in range(int(input())): n = int(input()) s = input() if s.count(s[0]) == len(s): out += str(n) + "\n" continue ans = n * n ans -= calc(s, n) ans -= calc("".join([("D" if c == "R" else "R") for c in s]), n) out += str(ans) + "\n" print(out)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL STRING VAR STRING STRING STRING VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR
Consider a grid of size $n \times n$. The rows are numbered top to bottom from $1$ to $n$, the columns are numbered left to right from $1$ to $n$. The robot is positioned in a cell $(1, 1)$. It can perform two types of moves: D — move one cell down; R — move one cell right. The robot is not allowed to move outside the grid. You are given a sequence of moves $s$ — the initial path of the robot. This path doesn't lead the robot outside the grid. You are allowed to perform an arbitrary number of modifications to it (possibly, zero). With one modification, you can duplicate one move in the sequence. That is, replace a single occurrence of D with DD or a single occurrence of R with RR. Count the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of testcases. The first line of each testcase contains the single integer $n$ ($2 \le n \le 10^8$) — the number of rows and columns in the grid. The second line of each testcase contains a non-empty string $s$, consisting only of characters D and R, — the initial path of the robot. This path doesn't lead the robot outside the grid. The total length of strings $s$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print a single integer — the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. -----Examples----- Input 3 4 RD 5 DRDRDRDR 3 D Output 13 9 3 -----Note----- In the first testcase, it's enough to consider the following modified paths: RD $\rightarrow$ RRD $\rightarrow$ RRRD $\rightarrow$ RRRDD $\rightarrow$ RRRDDD — this path visits cells $(1, 1)$, $(1, 2)$, $(1, 3)$, $(1, 4)$, $(2, 4)$, $(3, 4)$ and $(4, 4)$; RD $\rightarrow$ RRD $\rightarrow$ RRDD $\rightarrow$ RRDDD — this path visits cells $(1, 1)$, $(1, 2)$, $(1, 3)$, $(2, 3)$, $(3, 3)$ and $(4, 3)$; RD $\rightarrow$ RDD $\rightarrow$ RDDD — this path visits cells $(1, 1)$, $(1, 2)$, $(2, 2)$, $(3, 2)$ and $(4, 2)$. Thus, the cells that are visited on at least one modified path are: $(1, 1)$, $(1, 2)$, $(1, 3)$, $(1, 4)$, $(2, 2)$, $(2, 3)$, $(2, 4)$, $(3, 2)$, $(3, 3)$, $(3, 4)$, $(4, 2)$, $(4, 3)$ and $(4, 4)$. In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR. In the third testcase, the cells that are visited on at least one modified path are: $(1, 1)$, $(2, 1)$ and $(3, 1)$. Here are the cells for all testcases:
for _ in range(int(input())): n = int(input()) s = input() posX, posY = 0, 0 maxDown = [0] maxRight = [0] for c in s: if c == "D": posY += 1 maxDown[-1] += 1 maxRight.append(posX) else: posX += 1 maxDown.append(posY) maxRight[-1] += 1 if posX == 0 or posY == 0: print(n) else: jeuBas = n - posY - 1 jeuDroite = n - posX - 1 nbCase = 0 for i in maxDown: if i == 0: nbCase += n - 1 else: nbCase += n - 1 - i - jeuBas for i in maxRight: if i == 0: nbCase += n - 1 else: nbCase += n - 1 - i - jeuDroite print(n * n - nbCase)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR FOR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
Consider a grid of size $n \times n$. The rows are numbered top to bottom from $1$ to $n$, the columns are numbered left to right from $1$ to $n$. The robot is positioned in a cell $(1, 1)$. It can perform two types of moves: D — move one cell down; R — move one cell right. The robot is not allowed to move outside the grid. You are given a sequence of moves $s$ — the initial path of the robot. This path doesn't lead the robot outside the grid. You are allowed to perform an arbitrary number of modifications to it (possibly, zero). With one modification, you can duplicate one move in the sequence. That is, replace a single occurrence of D with DD or a single occurrence of R with RR. Count the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of testcases. The first line of each testcase contains the single integer $n$ ($2 \le n \le 10^8$) — the number of rows and columns in the grid. The second line of each testcase contains a non-empty string $s$, consisting only of characters D and R, — the initial path of the robot. This path doesn't lead the robot outside the grid. The total length of strings $s$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print a single integer — the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. -----Examples----- Input 3 4 RD 5 DRDRDRDR 3 D Output 13 9 3 -----Note----- In the first testcase, it's enough to consider the following modified paths: RD $\rightarrow$ RRD $\rightarrow$ RRRD $\rightarrow$ RRRDD $\rightarrow$ RRRDDD — this path visits cells $(1, 1)$, $(1, 2)$, $(1, 3)$, $(1, 4)$, $(2, 4)$, $(3, 4)$ and $(4, 4)$; RD $\rightarrow$ RRD $\rightarrow$ RRDD $\rightarrow$ RRDDD — this path visits cells $(1, 1)$, $(1, 2)$, $(1, 3)$, $(2, 3)$, $(3, 3)$ and $(4, 3)$; RD $\rightarrow$ RDD $\rightarrow$ RDDD — this path visits cells $(1, 1)$, $(1, 2)$, $(2, 2)$, $(3, 2)$ and $(4, 2)$. Thus, the cells that are visited on at least one modified path are: $(1, 1)$, $(1, 2)$, $(1, 3)$, $(1, 4)$, $(2, 2)$, $(2, 3)$, $(2, 4)$, $(3, 2)$, $(3, 3)$, $(3, 4)$, $(4, 2)$, $(4, 3)$ and $(4, 4)$. In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR. In the third testcase, the cells that are visited on at least one modified path are: $(1, 1)$, $(2, 1)$ and $(3, 1)$. Here are the cells for all testcases:
def solve(): t = int(input()) for r in range(t): n = int(input()) path = input() cnt_r = path.count("R") cnt_d = path.count("D") if cnt_r == 0 or cnt_d == 0: result = n else: move_r = n - cnt_r - 1 move_d = n - cnt_d - 1 result = 1 cur_char = "" prev_char = "" last_char = "" for cur_char in path: result += 1 if prev_char != cur_char: last_char = prev_char if last_char == "R": result += move_r elif last_char == "D": result += move_d prev_char = cur_char result += (1 + move_r) * (1 + move_d) result -= 1 print(result) solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR STRING VAR VAR IF VAR STRING VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Consider a grid of size $n \times n$. The rows are numbered top to bottom from $1$ to $n$, the columns are numbered left to right from $1$ to $n$. The robot is positioned in a cell $(1, 1)$. It can perform two types of moves: D — move one cell down; R — move one cell right. The robot is not allowed to move outside the grid. You are given a sequence of moves $s$ — the initial path of the robot. This path doesn't lead the robot outside the grid. You are allowed to perform an arbitrary number of modifications to it (possibly, zero). With one modification, you can duplicate one move in the sequence. That is, replace a single occurrence of D with DD or a single occurrence of R with RR. Count the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of testcases. The first line of each testcase contains the single integer $n$ ($2 \le n \le 10^8$) — the number of rows and columns in the grid. The second line of each testcase contains a non-empty string $s$, consisting only of characters D and R, — the initial path of the robot. This path doesn't lead the robot outside the grid. The total length of strings $s$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print a single integer — the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. -----Examples----- Input 3 4 RD 5 DRDRDRDR 3 D Output 13 9 3 -----Note----- In the first testcase, it's enough to consider the following modified paths: RD $\rightarrow$ RRD $\rightarrow$ RRRD $\rightarrow$ RRRDD $\rightarrow$ RRRDDD — this path visits cells $(1, 1)$, $(1, 2)$, $(1, 3)$, $(1, 4)$, $(2, 4)$, $(3, 4)$ and $(4, 4)$; RD $\rightarrow$ RRD $\rightarrow$ RRDD $\rightarrow$ RRDDD — this path visits cells $(1, 1)$, $(1, 2)$, $(1, 3)$, $(2, 3)$, $(3, 3)$ and $(4, 3)$; RD $\rightarrow$ RDD $\rightarrow$ RDDD — this path visits cells $(1, 1)$, $(1, 2)$, $(2, 2)$, $(3, 2)$ and $(4, 2)$. Thus, the cells that are visited on at least one modified path are: $(1, 1)$, $(1, 2)$, $(1, 3)$, $(1, 4)$, $(2, 2)$, $(2, 3)$, $(2, 4)$, $(3, 2)$, $(3, 3)$, $(3, 4)$, $(4, 2)$, $(4, 3)$ and $(4, 4)$. In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR. In the third testcase, the cells that are visited on at least one modified path are: $(1, 1)$, $(2, 1)$ and $(3, 1)$. Here are the cells for all testcases:
import sys def input(): return sys.stdin.readline()[:-1] def readline(): return map(int, input().split()) def solve(): n = int(input()) s = input() ans = len(s) + 1 r_cnt = s.count("R") d_cnt = len(s) - r_cnt width = r_cnt + 1 height = d_cnt + 1 if r_cnt and d_cnt: ans += (n - width) * (n - height) d = s.find("D") if d != -1: ans += (width - d) * (n - height) r = s.find("R") if r != -1: ans += (height - r) * (n - width) print(ans) t = int(input()) for __ in range(t): solve()
IMPORT FUNC_DEF RETURN FUNC_CALL VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Consider a grid of size $n \times n$. The rows are numbered top to bottom from $1$ to $n$, the columns are numbered left to right from $1$ to $n$. The robot is positioned in a cell $(1, 1)$. It can perform two types of moves: D — move one cell down; R — move one cell right. The robot is not allowed to move outside the grid. You are given a sequence of moves $s$ — the initial path of the robot. This path doesn't lead the robot outside the grid. You are allowed to perform an arbitrary number of modifications to it (possibly, zero). With one modification, you can duplicate one move in the sequence. That is, replace a single occurrence of D with DD or a single occurrence of R with RR. Count the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of testcases. The first line of each testcase contains the single integer $n$ ($2 \le n \le 10^8$) — the number of rows and columns in the grid. The second line of each testcase contains a non-empty string $s$, consisting only of characters D and R, — the initial path of the robot. This path doesn't lead the robot outside the grid. The total length of strings $s$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print a single integer — the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. -----Examples----- Input 3 4 RD 5 DRDRDRDR 3 D Output 13 9 3 -----Note----- In the first testcase, it's enough to consider the following modified paths: RD $\rightarrow$ RRD $\rightarrow$ RRRD $\rightarrow$ RRRDD $\rightarrow$ RRRDDD — this path visits cells $(1, 1)$, $(1, 2)$, $(1, 3)$, $(1, 4)$, $(2, 4)$, $(3, 4)$ and $(4, 4)$; RD $\rightarrow$ RRD $\rightarrow$ RRDD $\rightarrow$ RRDDD — this path visits cells $(1, 1)$, $(1, 2)$, $(1, 3)$, $(2, 3)$, $(3, 3)$ and $(4, 3)$; RD $\rightarrow$ RDD $\rightarrow$ RDDD — this path visits cells $(1, 1)$, $(1, 2)$, $(2, 2)$, $(3, 2)$ and $(4, 2)$. Thus, the cells that are visited on at least one modified path are: $(1, 1)$, $(1, 2)$, $(1, 3)$, $(1, 4)$, $(2, 2)$, $(2, 3)$, $(2, 4)$, $(3, 2)$, $(3, 3)$, $(3, 4)$, $(4, 2)$, $(4, 3)$ and $(4, 4)$. In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR. In the third testcase, the cells that are visited on at least one modified path are: $(1, 1)$, $(2, 1)$ and $(3, 1)$. Here are the cells for all testcases:
def solve(): n = int(input()) s = input() m = len(s) flag = True for i in range(1, m): if s[i] != s[i - 1]: flag = False if flag: return n x, y = 1, 1 for c in s: if c == "R": y = y + 1 if c == "D": x = x + 1 x, y = n - x, n - y res = m for i in range(m): if i and s[i] != s[i - 1]: flag = True if flag: if s[i] == "R": res = res + x if s[i] == "D": res = res + y return res + (x + 1) * (y + 1) for i in range(int(input())): print(solve())
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR RETURN VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def solve(self, i, j, m, n, grid, word): if grid[i][j] != word[0]: return False temp = [[1, 0], [0, 1], [-1, 0], [0, -1], [-1, 1], [1, -1], [1, 1], [-1, -1]] r = i c = j for k in temp: flag = True i = r j = c i = i + k[0] j = j + k[1] for q in range(1, len(word)): if i >= 0 and i < m and j >= 0 and j < n and grid[i][j] == word[q]: i = i + k[0] j = j + k[1] else: flag = False break if flag: return True return False def searchWord(self, grid, word): ans = [] m = len(grid) n = len(grid[0]) for i in range(m): for j in range(n): if self.solve(i, j, m, n, grid, word): ans.append((i, j)) return ans
CLASS_DEF FUNC_DEF IF VAR VAR VAR VAR NUMBER RETURN 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 ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR LIST 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 FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def searchWord(self, grid, word): def dfs(r, c, ind, d): if r >= row or c >= col or r < 0 or c < 0 or grid[r][c] != word[ind]: return False if ind == len(word) - 1: return True return dfs(r + d[0], c + d[1], ind + 1, d) row = len(grid) col = len(grid[0]) dir = [(1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (-1, 1), (1, -1), (-1, -1)] ans = [] for r in range(row): for c in range(col): if grid[r][c] == word[0]: for d in dir: if dfs(r, c, 0, d): ans.append([r, c]) break return ans
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR RETURN NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def helper(self, idx, dir, i, j, grid, word): if not (0 <= i and i < len(grid) and 0 <= j and j < len(grid[0])): return False if word[idx] != grid[i][j]: return False if idx == len(word) - 1: return True if dir == 0: return ( self.helper(idx + 1, 1, i, j + 1, grid, word) or self.helper(idx + 1, 2, i - 1, j + 1, grid, word) or self.helper(idx + 1, 3, i - 1, j, grid, word) or self.helper(idx + 1, 4, i - 1, j - 1, grid, word) or self.helper(idx + 1, 5, i, j - 1, grid, word) or self.helper(idx + 1, 6, i + 1, j - 1, grid, word) or self.helper(idx + 1, 7, i + 1, j, grid, word) or self.helper(idx + 1, 8, i + 1, j + 1, grid, word) ) elif dir == 1: return self.helper(idx + 1, 1, i, j + 1, grid, word) elif dir == 2: return self.helper(idx + 1, 2, i - 1, j + 1, grid, word) elif dir == 3: return self.helper(idx + 1, 3, i - 1, j, grid, word) elif dir == 4: return self.helper(idx + 1, 4, i - 1, j - 1, grid, word) elif dir == 5: return self.helper(idx + 1, 5, i, j - 1, grid, word) elif dir == 6: return self.helper(idx + 1, 6, i + 1, j - 1, grid, word) elif dir == 7: return self.helper(idx + 1, 7, i + 1, j, grid, word) elif dir == 8: return self.helper(idx + 1, 8, i + 1, j + 1, grid, word) def searchWord(self, grid, word): n = len(grid) m = len(grid[0]) Ans = [] for i in range(n): for j in range(m): if self.helper(0, 0, i, j, grid, word): Ans.append([i, j]) return Ans
CLASS_DEF FUNC_DEF IF NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR VAR RETURN NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def helper(self, word, moves, i, j, grid): if grid[i][j] != word[0]: return False for a, b in moves: new_i, new_j = i + a, j + b st = True for k in range(1, len(word)): if ( 0 <= new_i < len(grid) and 0 <= new_j < len(grid[0]) and grid[new_i][new_j] == word[k] ): new_i += a new_j += b else: st = False break if st: return True return False def searchWord(self, grid, word): moves = [[0, 1], [1, 0], [0, -1], [-1, 0], [1, 1], [1, -1], [-1, -1], [-1, 1]] res = [] for i in range(len(grid)): for j in range(len(grid[0])): if self.helper(word, moves, i, j, grid) == True: res.append([i, j]) return res
CLASS_DEF FUNC_DEF IF VAR VAR VAR VAR NUMBER RETURN NUMBER FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR RETURN NUMBER RETURN NUMBER FUNC_DEF 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 ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def searchWord(self, grid, word): res = [] for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] == word[0]: if self.find(i, j, grid, word, 1, "any"): res.append((i, j)) return res def find(self, i, j, grid, word, x, dir): if x == len(word): return True if ( (dir == "top" or dir == "any") and i + 1 < len(grid) and grid[i + 1][j] == word[x] ): if self.find(i + 1, j, grid, word, x + 1, "top"): return True if ( (dir == "bottom" or dir == "any") and i - 1 >= 0 and grid[i - 1][j] == word[x] ): if self.find(i - 1, j, grid, word, x + 1, "bottom"): return True if (dir == "left" or dir == "any") and j - 1 >= 0 and grid[i][j - 1] == word[x]: if self.find(i, j - 1, grid, word, x + 1, "left"): return True if ( (dir == "right" or dir == "any") and j + 1 < len(grid[0]) and grid[i][j + 1] == word[x] ): if self.find(i, j + 1, grid, word, x + 1, "right"): return True if ( (dir == "topleft" or dir == "any") and i - 1 >= 0 and j - 1 >= 0 and grid[i - 1][j - 1] == word[x] ): if self.find(i - 1, j - 1, grid, word, x + 1, "topleft"): return True if ( (dir == "bottomright" or dir == "any") and i + 1 < len(grid) and j + 1 < len(grid[0]) and grid[i + 1][j + 1] == word[x] ): if self.find(i + 1, j + 1, grid, word, x + 1, "bottomright"): return True if ( (dir == "bottomleft" or dir == "any") and i + 1 < len(grid) and j - 1 >= 0 and grid[i + 1][j - 1] == word[x] ): if self.find(i + 1, j - 1, grid, word, x + 1, "bottomleft"): return True if ( (dir == "topright" or dir == "any") and i - 1 >= 0 and j + 1 < len(grid[0]) and grid[i - 1][j + 1] == word[x] ): if self.find(i - 1, j + 1, grid, word, x + 1, "topright"): return True return False
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR STRING VAR STRING BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER STRING RETURN NUMBER IF VAR STRING VAR STRING BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER STRING RETURN NUMBER IF VAR STRING VAR STRING BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER STRING RETURN NUMBER IF VAR STRING VAR STRING BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER STRING RETURN NUMBER IF VAR STRING VAR STRING BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER STRING RETURN NUMBER IF VAR STRING VAR STRING BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER STRING RETURN NUMBER IF VAR STRING VAR STRING BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER STRING RETURN NUMBER IF VAR STRING VAR STRING BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER STRING RETURN NUMBER RETURN NUMBER
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def searchWord(self, grid, word): m = len(grid) n = len(grid[0]) indexes = [[(0) for j in range(n)] for i in range(m)] d = [[1, 0], [-1, 0], [0, 1], [0, -1], [1, 1], [1, -1], [-1, 1], [-1, -1]] for i in range(0, m): for j in range(0, n): if grid[i][j] == word[0]: for k in range(0, len(d)): ans = self.find(grid, word, m, n, i, j, 0, d[k][0], d[k][1]) if ans is True: indexes[i][j] = 1 final = [] for i in range(0, m): for j in range(0, n): if indexes[i][j] == 1: final.append([i, j]) return final def find(self, grid, word, m, n, i, j, index, add_i, add_j): if index == len(word): return True if i < 0 or i >= m or j < 0 or j >= n: return False if grid[i][j] != word[index]: return False new_i = i + add_i new_j = j + add_j ans = self.find(grid, word, m, n, new_i, new_j, index + 1, add_i, add_j) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR 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 FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def search(self, i, j, grid, word, n, m): if grid[i][j] != word[0]: return False drc = [[-1, 0], [-1, 1], [0, 1], [1, 1], [1, 0], [1, -1], [0, -1], [-1, -1]] for xy in drc: x = xy[0] y = xy[1] f = True r = i + x c = j + y for k in range(1, len(word)): if 0 <= r < n and 0 <= c < m and grid[r][c] == word[k]: r += x c += y else: f = False break if f: return True def searchWord(self, grid, word): n = len(grid) m = len(grid[0]) res = [] for i in range(n): for j in range(m): if self.search(i, j, grid, word, n, m): res.append([i, j]) return res
CLASS_DEF FUNC_DEF IF VAR VAR VAR VAR NUMBER RETURN 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 FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def searchWord(self, grid, word): def fun(i, j, k, s, f, l1, m1, n1): if i < n and j < m and i >= 0 and j >= 0 and s + grid[i][j] == word: return 1 if i < 0 or j < 0 or i >= n or j >= m: return ans = 0 if f == 0 and grid[i][j] == word[k]: ans = fun(i - 1, j, k + 1, s + grid[i][j], 0, l1, m1, n1) f += 1 i, j, k = l1, m1, n1 s = "" if ans == 1: return 1 if f == 1 and grid[i][j] == word[k]: ans = fun(i + 1, j, k + 1, s + grid[i][j], 1, l1, m1, n1) f += 1 i, j, k = l1, m1, n1 s = "" if ans == 1: return 1 if f == 2 and grid[i][j] == word[k]: ans = fun(i, j + 1, k + 1, s + grid[i][j], 2, l1, m1, n1) f += 1 i, j, k = l1, m1, n1 s = "" if ans == 1: return 1 if f == 3 and grid[i][j] == word[k]: ans = fun(i, j - 1, k + 1, s + grid[i][j], 3, l1, m1, n1) f += 1 i, j, k = l1, m1, n1 s = "" if ans == 1: return 1 if f == 4 and grid[i][j] == word[k]: ans = fun(i - 1, j + 1, k + 1, s + grid[i][j], 4, l1, m1, n1) f += 1 i, j, k = l1, m1, n1 s = "" if ans == 1: return 1 if f == 5 and grid[i][j] == word[k]: ans = fun(i - 1, j - 1, k + 1, s + grid[i][j], 5, l1, m1, n1) f += 1 i, j, k = l1, m1, n1 s = "" if ans == 1: return 1 if f == 6 and grid[i][j] == word[k]: ans = fun(i + 1, j - 1, k + 1, s + grid[i][j], 6, l1, m1, n1) f += 1 i, j, k = l1, m1, n1 s = "" if ans == 1: return 1 if f == 7 and grid[i][j] == word[k]: ans = fun(i + 1, j + 1, k + 1, s + grid[i][j], 7, l1, m1, n1) f += 1 i, j, k = l1, m1, n1 s = "" if ans == 1: return 1 return ans res = [] for i in range(0, n): for j in range(0, m): if grid[i][j] == word[0]: if len(word) == 1: res.append([i, j]) else: ans = fun(i, j, 0, "", 0, i, j, 0) if ans == 1: res.append([i, j]) return res
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR RETURN ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR STRING IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR STRING IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR STRING IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR STRING IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR STRING IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR STRING IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR STRING IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR STRING IF VAR NUMBER RETURN NUMBER RETURN VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER STRING NUMBER VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
def search2D(grid, row, col, word): di = [[-1, 0], [1, 0], [1, 1], [1, -1], [-1, -1], [-1, 1], [0, 1], [0, -1]] if grid[row][col] != word[0]: return False R = len(grid) C = len(grid[0]) for x, y in di: rd, cd = row + x, col + y flag = True for k in range(1, len(word)): if 0 <= rd < R and 0 <= cd < C and word[k] == grid[rd][cd]: rd += x cd += y else: flag = False break if flag: return True return False def patternSearch(grid, word): res = [] R = len(grid) C = len(grid[0]) for row in range(R): for col in range(C): if search2D(grid, row, col, word): res.append((row, col)) return res class Solution: def searchWord(self, grid, word): return patternSearch(grid, word)
FUNC_DEF 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 IF VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR LIST 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 FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def __init__(self): self.R = None self.C = None self.dir = [ [-1, 0], [1, 0], [1, 1], [1, -1], [-1, -1], [-1, 1], [0, 1], [0, -1], ] def search2D(self, grid, row, col, word): if grid[row][col] != word[0]: return False for x, y in self.dir: rd, cd = row + x, col + y flag = True for k in range(1, len(word)): if 0 <= rd < self.R and 0 <= cd < self.C and word[k] == grid[rd][cd]: rd += x cd += y else: flag = False break if flag: return True return False def searchWord(self, grid, word): self.R = len(grid) self.C = len(grid[0]) f = [] for row in range(self.R): for col in range(self.C): if self.search2D(grid, row, col, word): f.append((row, col)) return sorted(set(f))
CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NONE 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 FUNC_DEF IF VAR VAR VAR VAR NUMBER RETURN NUMBER FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def perf(self, grid, word, i, j, n, m): move = [[-1, 0], [-1, -1], [0, -1], [1, -1], [1, 0], [1, 1], [0, 1], [-1, 1]] for k in range(8): x = i + move[k][0] y = j + move[k][1] idx = 1 for p in range(1, len(word)): if x < 0 or x >= n or y < 0 or y >= m or grid[x][y] != word[p]: break x = x + move[k][0] y = y + move[k][1] idx += 1 if idx == len(word): return True return False def searchWord(self, grid, word): n = len(grid) m = len(grid[0]) res = [] for i in range(n): for j in range(m): if grid[i][j] == word[0]: if self.perf(grid, word, i, j, n, m): res.append([i, j]) return res
CLASS_DEF FUNC_DEF 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 FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def searchWord(self, grid, word): n = len(grid) m = len(grid[0]) def dfs(i, j, k, ind): if ind == len(word): return True if i < 0 or j < 0 or i >= n or j >= m or grid[i][j] != word[ind]: return False return dfs(i + k[0], j + k[1], k, ind + 1) d = [(1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (-1, 1), (1, -1), (-1, -1)] ans = [] for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] == word[0]: for k in d: if dfs(i, j, k, 0): ans.append([i, j]) break return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def search(self, grid, row, col, word, r, c, dirc): if grid[row][col] != word[0]: return False for x, y in dirc: rd = row + x cd = col + y flag = True for k in range(1, len(word)): if 0 <= rd < r and 0 <= cd < c and word[k] == grid[rd][cd]: rd += x cd += y else: flag = False break if flag: return True return False def searchWord(self, grid, word): r = len(grid) c = len(grid[0]) dirc = [[-1, 0], [1, 0], [1, 1], [1, -1], [-1, -1], [-1, 1], [0, 1], [0, -1]] res = [] for row in range(r): for col in range(c): if self.search(grid, row, col, word, r, c, dirc): res.append([row, col]) res.sort() return res
CLASS_DEF FUNC_DEF IF VAR VAR VAR VAR NUMBER RETURN NUMBER FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR 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 ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def searchWord(self, grid, word): ans = [] r = len(grid) c = len(grid[0]) def recurs(c, starti, startj, direct, i, j, ans): if c == "": if [starti, startj] not in ans: ans.append([starti, startj]) return elif i < 0 or i == len(grid) or j < 0 or j == len(grid[0]): return elif grid[i][j] == c[0]: a = c[1:] if not direct: recurs(a, i, j, "L", i, j - 1, ans) recurs(a, i, j, "R", i, j + 1, ans) recurs(a, i, j, "U", i - 1, j, ans) recurs(a, i, j, "D", i + 1, j, ans) recurs(a, i, j, "UR", i - 1, j + 1, ans) recurs(a, i, j, "UL", i - 1, j - 1, ans) recurs(a, i, j, "DL", i + 1, j - 1, ans) recurs(a, i, j, "DR", i + 1, j + 1, ans) elif direct == "L": recurs(a, starti, startj, "L", i, j - 1, ans) elif direct == "R": recurs(a, starti, startj, "R", i, j + 1, ans) elif direct == "U": recurs(a, starti, startj, "U", i - 1, j, ans) elif direct == "D": recurs(a, starti, startj, "D", i + 1, j, ans) elif direct == "UR": recurs(a, starti, startj, "UR", i - 1, j + 1, ans) elif direct == "UL": recurs(a, starti, startj, "UL", i - 1, j - 1, ans) elif direct == "DL": recurs(a, starti, startj, "DL", i + 1, j - 1, ans) else: recurs(a, starti, startj, "DR", i + 1, j + 1, ans) else: return for i in range(r): for j in range(c): recurs(word, None, None, None, i, j, ans) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR STRING IF LIST VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER RETURN IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR STRING VAR BIN_OP VAR NUMBER VAR IF VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR STRING VAR BIN_OP VAR NUMBER VAR IF VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR STRING BIN_OP VAR NUMBER VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR STRING BIN_OP VAR NUMBER VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR STRING BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR STRING BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR STRING BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NONE NONE NONE VAR VAR VAR RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def searchWord(self, M, w): R, C = len(M), len(M[0]) ans = [] for r in range(R): for c in range(C): if M[r][c] == w[0] and expand(r, c, R, C, M, w): ans.append([r, c]) return ans def expand(sr, sc, R, C, M, w): for dr in range(-1, 2): for dc in range(-1, 2): if dr == 0 and dc == 0: continue r, c = sr, sc i = 0 while 0 <= r < R and 0 <= c < C and i < len(w) and M[r][c] == w[i]: i += 1 r, c = r + dr, c + dc if i == len(w): return True return False
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE NUMBER VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
def dfs(row, col, word, m, n, grid, direction): if len(word) == 0: return True x, y = direction if ( row + x >= 0 and row + x < m and col + y >= 0 and col + y < n and grid[row + x][col + y] == word[0] ): if dfs(row + x, col + y, word[1:], m, n, grid, direction): return True return False class Solution: def searchWord(self, grid, word): m = len(grid) n = len(grid[0]) result = [] directions = [ (-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1), ] for i in range(m): for j in range(n): if grid[i][j] == word[0]: for direction in directions: if dfs(i, j, word[1:], m, n, grid, direction): result.append((i, j)) break return result
FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def __init__(self): self.ans = {} def searchWord(self, grid, word): ans = [] n = len(grid) m = len(grid[0]) dirs = [(-1, 0), (0, -1), (-1, -1), (-1, 1), (1, -1), (1, 0), (1, 1), (0, 1)] def is_possible(grid, word, i, j, dirs): n = len(grid) m = len(grid[0]) for dir in dirs: newi = i newj = j c = 0 while ( 0 <= newi < n and 0 <= newj < m and c < len(word) and grid[newi][newj] == word[c] ): newi += dir[0] newj += dir[1] c += 1 if c == len(word): return True return False for i in range(n): for j in range(m): if grid[i][j] == word[0] and is_possible(grid, word, i, j, dirs): ans.append([i, j]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE NUMBER VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def solve(self, i, j, k, n, m, l, dx, dy, grid, visited, word): if visited[i][j]: return False visited[i][j] = 1 x, y = dx + i, dy + j if x >= 0 and x < n and y >= 0 and y < m: if k + 1 == l - 1 and word[k + 1] == grid[x][y]: return True elif k + 1 < l and word[k + 1] == grid[x][y]: if self.solve(x, y, k + 1, n, m, l, dx, dy, grid, visited, word): return True return False def searchWord(self, grid, word): n, m, l = len(grid), len(grid[0]), len(word) res = [] dx = [0, 0, 1, -1, 1, 1, -1, -1] dy = [1, -1, 0, 0, -1, 1, -1, 1] for i in range(n): for j in range(m): if grid[i][j] == word[0]: if l == 1 and [i, j] not in res: res.append([i, j]) continue for ind in range(8): visited = [([0] * m) for i in range(n)] if ( self.solve( i, j, 0, n, m, l, dx[ind], dy[ind], grid, visited, word ) and [i, j] not in res ): res.append([i, j]) res.sort() return res
CLASS_DEF FUNC_DEF IF VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR NUMBER LIST VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
def search(grid, word, row, col): y = [0, -1, -1, -1, 0, 1, 1, 1] x = [1, 1, 0, -1, -1, -1, 0, 1] for i in range(8): X = x[i] + row Y = y[i] + col k = 1 while ( X >= 0 and X < len(grid) and Y >= 0 and Y < len(grid[0]) and k < len(word) and grid[X][Y] == word[k] ): X += x[i] Y += y[i] k += 1 if k == len(word): return True else: continue return False class Solution: def searchWord(self, grid, word): row = len(grid) col = len(grid[0]) ans = [] for i in range(row): for j in range(col): if grid[i][j] == word[0]: if search(grid, word, i, j): move = i, j ans.append(move) return ans
FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def searchWord(self, grid, word): n = len(grid) m = len(grid[0]) l = len(word) output = [] for i in range(n): for j in range(m): if i >= l - 1: candi = "" for k in range(l): candi += grid[i - k][j] if candi == word: output.append((i, j)) if i + l <= n: candi = "" for k in range(l): candi += grid[i + k][j] if candi == word: output.append((i, j)) if j >= l - 1: candi = "" for k in range(l): candi += grid[i][j - k] if candi == word: output.append((i, j)) if j + l <= m: candi = "" for k in range(l): candi += grid[i][j + k] if candi == word: output.append((i, j)) if i >= l - 1 and j >= l - 1: candi = "" for k in range(l): candi += grid[i - k][j - k] if candi == word: output.append((i, j)) if i >= l - 1 and j + l <= m: candi = "" for k in range(l): candi += grid[i - k][j + k] if candi == word: output.append((i, j)) if i + l <= n and j >= l - 1: candi = "" for k in range(l): candi += grid[i + k][j - k] if candi == word: output.append((i, j)) if i + l <= n and j + l <= m: candi = "" for k in range(l): candi += grid[i + k][j + k] if candi == word: output.append((i, j)) return sorted(set(output))
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def __compare_direction_substring( self, i, j, k, offset_row, offset_col, n, grid, word ): while k < n: if grid[i][j] != word[k]: return False i += offset_row j += offset_col k += 1 return True def searchWord(self, grid, word): r, c, n = len(grid), len(grid[0]), len(word) offsets = (0, 1), (0, -1), (1, 0), (-1, 0), (-1, 1), (-1, -1), (1, -1), (1, 1) idx_within_range = lambda idx, start, end: start <= idx < end res = [] for i in range(r): for j in range(c): if grid[i][j] == word[0]: for offset_row, offset_col in offsets: end_pos_row, end_pos_col = ( i + (n - 1) * offset_row, j + (n - 1) * offset_col, ) if idx_within_range(end_pos_row, 0, r) and idx_within_range( end_pos_col, 0, c ): if self.__compare_direction_substring( i + offset_row, j + offset_col, 1, offset_row, offset_col, n, grid, word, ): res.append([i, j]) break return res
CLASS_DEF FUNC_DEF WHILE VAR VAR IF VAR VAR VAR VAR VAR RETURN NUMBER VAR VAR VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def __init__(self): self.ans = [] def dfs(self, grid, i, j, n, m, curr_word, word, start_i, start_j, dir): if curr_word == len(word): if (start_i, start_j) not in self.ans: self.ans.append((start_i, start_j)) return True if i >= n or i < 0 or j >= m or j < 0 or grid[i][j] != word[curr_word]: return temp = grid[i][j] grid[i][j] = "#" if dir == "V": self.dfs(grid, i + 1, j, n, m, curr_word + 1, word, start_i, start_j, dir) self.dfs(grid, i - 1, j, n, m, curr_word + 1, word, start_i, start_j, dir) if dir == "H": self.dfs(grid, i, j + 1, n, m, curr_word + 1, word, start_i, start_j, dir) self.dfs(grid, i, j - 1, n, m, curr_word + 1, word, start_i, start_j, dir) if dir == "DR": self.dfs( grid, i + 1, j + 1, n, m, curr_word + 1, word, start_i, start_j, dir ) self.dfs( grid, i - 1, j - 1, n, m, curr_word + 1, word, start_i, start_j, dir ) if dir == "DL": self.dfs( grid, i + 1, j - 1, n, m, curr_word + 1, word, start_i, start_j, dir ) self.dfs( grid, i - 1, j + 1, n, m, curr_word + 1, word, start_i, start_j, dir ) grid[i][j] = temp def searchWord(self, grid, word): try: n = len(grid) m = len(grid[0]) for i in range(n): for j in range(m): if grid[i][j] == word[0]: self.dfs(grid, i, j, n, m, 0, word, i, j, "H") self.dfs(grid, i, j, n, m, 0, word, i, j, "V") self.dfs(grid, i, j, n, m, 0, word, i, j, "DR") self.dfs(grid, i, j, n, m, 0, word, i, j, "DL") return self.ans except Exception as e: print("e", e)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR RETURN ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR STRING IF VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR 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 VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR STRING RETURN VAR VAR EXPR FUNC_CALL VAR STRING VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def searchWord(self, grid, word): def count(row, col): if grid[row][col] != word[0]: return False for x, y in rcmat: rd = row + x cl = col + y flag = True for k in range(1, len(word)): if -1 < rd < R and -1 < cl < C and grid[rd][cl] == word[k]: rd = rd + x cl = cl + y else: flag = False break if flag: return True return False ls = [] rcmat = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, 0], [1, 1], [1, -1]] R = len(grid) C = len(grid[0]) for i in range(R): for j in range(C): if count(i, j): ls.append([i, j]) return ls
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR VAR VAR NUMBER RETURN NUMBER FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR LIST 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 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 FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def searchWord(self, grid, word): def check(i, j, x, y, ll, n, grid, word, r, c): if i < 0 or i >= r or j < 0 or j >= c: return False if ll == n - 1: if word[ll] == grid[i][j]: return True return False if word[ll] == grid[i][j]: return check(i + x, j + y, x, y, ll + 1, n, grid, word, r, c) return False r = len(grid) c = len(grid[0]) n = len(word) ans = [] step = [[1, 1], [-1, -1], [1, -1], [-1, 1], [1, 0], [-1, 0], [0, 1], [0, -1]] for i in range(r): for j in range(c): if grid[i][j] == word[0]: for x, y in step: if check(i, j, x, y, 0, n, grid, word, r, c): ans += [[i, j]] break return ans
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER IF VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST 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 FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER FOR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR LIST LIST VAR VAR RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def internal_search(self, idx, word, grid, row, col, row_max, col_max): if row < 0 or row > row_max or col < 0 or col > col_max: return False if grid[row][col] != word[0]: return False dxy = [[0, 1], [0, -1], [1, 0], [-1, 0], [1, 1], [1, -1], [-1, 1], [-1, -1]] for dx, dy in dxy: new_row = row + dx new_col = col + dy found = True for i in range(1, len(word)): if ( new_row >= 0 and new_row <= row_max and new_col >= 0 and new_col <= col_max and word[i] == grid[new_row][new_col] ): new_row += dx new_col += dy else: found = False break if found: return True return False def searchWord(self, grid, word): row_count = len(grid) col_count = len(grid[0]) res = [] for row in range(row_count): for col in range(col_count): if self.internal_search( 0, word, grid, row, col, row_count - 1, col_count - 1 ): res.append([row, col]) return res
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR VAR VAR NUMBER RETURN 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 FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def searchWord(self, grid, word): def valid(i, j): if 0 <= i < len(grid) and 0 <= j < len(grid[0]): return True return False x = [0, 0, -1, -1, -1, 1, 1, 1] y = [-1, 1, -1, 0, 1, -1, 0, 1] def soln(i, j, dirn, m): if m == len(word): return True if dirn == -1: for d in range(8): tx = i + x[d] ty = j + y[d] if valid(tx, ty) and grid[tx][ty] == word[m]: if soln(tx, ty, d, m + 1): return True return False else: tx = i + x[dirn] ty = j + y[dirn] if valid(tx, ty) and grid[tx][ty] == word[m]: if soln(tx, ty, dirn, m + 1): return True return False soln.res = [] for r in range(len(grid)): for c in range(len(grid[0])): if grid[r][c] == word[0]: if soln(r, c, -1, 1): soln.res.append((r, c)) return soln.res
CLASS_DEF FUNC_DEF FUNC_DEF IF NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN 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 IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR 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 VAR VAR IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def searchWord(self, grid, word): def found(i, j, d): wl = len(word) for k in range(wl): if grid[i][j] != word[k]: return False else: i, j = i + d[0], j + d[1] if i < 0 or i >= n or j < 0 or j >= m: return False if k < wl - 1 else True return True directions = [ (0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (-1, -1), (1, -1), (-1, 1), ] r = [] n, m = len(grid), len(grid[0]) for i in range(n): for j in range(m): for d in directions: if found(i, j, d): r.append([i, j]) break return r
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN VAR BIN_OP VAR NUMBER NUMBER NUMBER RETURN NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def searchWord(self, grid, word): def dfs(i, r, c, move): if ( r < 0 or r >= len(grid) or c < 0 or c >= len(grid[0]) or word[i] != grid[r][c] ): return False if i == len(word) - 1 and word[i] == grid[r][c]: return True if move == "": return ( dfs(i + 1, r - 1, c, "t") or dfs(i + 1, r - 1, c + 1, "tr") or dfs(i + 1, r, c + 1, "r") or dfs(i + 1, r + 1, c + 1, "br") or dfs(i + 1, r + 1, c, "b") or dfs(i + 1, r + 1, c - 1, "bl") or dfs(i + 1, r, c - 1, "l") or dfs(i + 1, r - 1, c - 1, "tl") ) elif move == "t": return dfs(i + 1, r - 1, c, "t") elif move == "tr": return dfs(i + 1, r - 1, c + 1, "tr") elif move == "r": return dfs(i + 1, r, c + 1, "r") elif move == "br": return dfs(i + 1, r + 1, c + 1, "br") elif move == "b": return dfs(i + 1, r + 1, c, "b") elif move == "bl": return dfs(i + 1, r + 1, c - 1, "bl") elif move == "l": return dfs(i + 1, r, c - 1, "l") else: return dfs(i + 1, r - 1, c - 1, "tl") res = [] for i in range(len(grid)): for j in range(len(grid[0])): if dfs(0, i, j, ""): res.append([i, j]) return res
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR RETURN NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR RETURN NUMBER IF VAR STRING RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR STRING FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR STRING FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING IF VAR STRING RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR STRING IF VAR STRING RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING IF VAR STRING RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING IF VAR STRING RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING IF VAR STRING RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR STRING IF VAR STRING RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING IF VAR STRING RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR NUMBER VAR VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def searchWord(self, grid, word): n, m, l, x = len(grid), len(grid[0]), len(word), [] def right(i, j, k, n, m, l): if k == l: return True if j >= m or grid[i][j] != word[k]: return False return right(i, j + 1, k + 1, n, m, l) def left(i, j, k, n, m, l): if k == l: return True if j < 0 or grid[i][j] != word[k]: return False return left(i, j - 1, k + 1, n, m, l) def up(i, j, k, n, m, l): if k == l: return True if i < 0 or grid[i][j] != word[k]: return False return up(i - 1, j, k + 1, n, m, l) def down(i, j, k, n, m, l): if k == l: return True if i >= n or grid[i][j] != word[k]: return False return down(i + 1, j, k + 1, n, m, l) def upleft(i, j, k, n, m, l): if k == l: return True if i < 0 or j < 0 or grid[i][j] != word[k]: return False return upleft(i - 1, j - 1, k + 1, n, m, l) def upright(i, j, k, n, m, l): if k == l: return True if i < 0 or j >= m or grid[i][j] != word[k]: return False return upright(i - 1, j + 1, k + 1, n, m, l) def downright(i, j, k, n, m, l): if k == l: return True if i >= n or j >= m or grid[i][j] != word[k]: return False return downright(i + 1, j + 1, k + 1, n, m, l) def downleft(i, j, k, n, m, l): if k == l: return True if i >= n or j < 0 or grid[i][j] != word[k]: return False return downleft(i + 1, j - 1, k + 1, n, m, l) for i in range(n): for j in range(m): if grid[i][j] == word[0]: if right(i, j, 0, n, m, l) == True: x.append([i, j]) continue if left(i, j, 0, n, m, l) == True: x.append([i, j]) continue if up(i, j, 0, n, m, l) == True: x.append([i, j]) continue if down(i, j, 0, n, m, l) == True: x.append([i, j]) continue if upleft(i, j, 0, n, m, l): x.append([i, j]) continue if upright(i, j, 0, n, m, l): x.append([i, j]) continue if downright(i, j, 0, n, m, l): x.append([i, j]) continue elif downleft(i, j, 0, n, m, l): x.append([i, j]) continue return x
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR LIST FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def searchWord(self, grid, word): directions = [ [1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1], ] if not grid or not word: return [] n = len(grid) m = len(grid[0]) res = [] def dfs(i, j): if word[0] != grid[i][j]: return False for dir in directions: newx = i + dir[0] newy = j + dir[1] flag = True for k in range(1, len(word)): if ( newx < n and newx >= 0 and newy < m and newy >= 0 and word[k] == grid[newx][newy] ): newx += dir[0] newy += dir[1] else: flag = False break if flag: return True return False for i in range(n): for j in range(m): if dfs(i, j): res.append([i, j]) return res
CLASS_DEF FUNC_DEF 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 IF VAR VAR RETURN LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER VAR VAR VAR RETURN NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def findWord(self, mat, target, dir, i, j, curind): if i < 0 or j < 0 or i >= len(mat) or j >= len(mat[0]): return 0 curchar = mat[i][j] if target[curind] != curchar: return 0 if curind == len(target) - 1: return 1 count = 0 if dir == 0: return ( self.findWord(mat, target, 1, i, j + 1, curind + 1) or self.findWord(mat, target, 2, i - 1, j + 1, curind + 1) or self.findWord(mat, target, 3, i - 1, j, curind + 1) or self.findWord(mat, target, 4, i - 1, j - 1, curind + 1) or self.findWord(mat, target, 5, i, j - 1, curind + 1) or self.findWord(mat, target, 6, i + 1, j - 1, curind + 1) or self.findWord(mat, target, 7, i + 1, j, curind + 1) or self.findWord(mat, target, 8, i + 1, j + 1, curind + 1) ) elif dir == 1: return self.findWord(mat, target, 1, i, j + 1, curind + 1) elif dir == 2: return self.findWord(mat, target, 2, i - 1, j + 1, curind + 1) elif dir == 3: return self.findWord(mat, target, 3, i - 1, j, curind + 1) elif dir == 4: return self.findWord(mat, target, 4, i - 1, j - 1, curind + 1) elif dir == 5: return self.findWord(mat, target, 5, i, j - 1, curind + 1) elif dir == 6: return self.findWord(mat, target, 6, i + 1, j - 1, curind + 1) elif dir == 7: return self.findWord(mat, target, 7, i + 1, j, curind + 1) elif dir == 8: return self.findWord(mat, target, 8, i + 1, j + 1, curind + 1) return count def searchWord(self, grid, word): ans = [] for i in range(0, len(grid)): for j in range(0, len(grid[0])): if self.findWord(grid, word, 0, i, j, 0): ans.append([i, j]) return ans
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR RETURN NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def searchWord(self, grid, word): r = len(grid) c = len(grid[0]) res = [] l = len(word) for i in range(len(grid)): for j in range(len(grid[i])): if grid[i][j] == word[0]: h_r = grid[i][j : j + l] h_r_w = "".join(h_r) if j - l + 1 >= 0: h_l = grid[i][j - l + 1 : j + 1][::-1] h_l_w = "".join(h_l) else: h_l_w = "" v_d = [x[j] for x in grid][i : i + l] v_d_w = "".join(v_d) if i - l + 1 >= 0: v_u = [x[j] for x in grid][i - l + 1 : i + 1][::-1] v_u_w = "".join(v_u) else: v_u_w = "" if i + l - 1 < r and j + l - 1 < c: d1_d = [grid[i + x][j + x] for x in range(0, l)] d1_d_w = "".join(d1_d) else: d1_d_w = "" if i - l + 1 >= 0 and j - l + 1 >= 0: d1_u = [grid[i - x][j - x] for x in range(0, l)] d1_u_w = "".join(d1_u) else: d1_u_w = "" if i + l - 1 < r and j - l + 1 >= 0: d2_d = [grid[i + x][j - x] for x in range(0, l)] d2_d_w = "".join(d2_d) else: d2_d_w = "" if i - l + 1 >= 0 and j + l - 1 < c: d2_u = [grid[i - x][j + x] for x in range(0, l)] d2_u_w = "".join(d2_u) else: d2_u_w = "" if h_r_w == word: res.append([i, j]) elif h_l_w == word: res.append([i, j]) elif v_d_w == word: res.append([i, j]) elif v_u_w == word: res.append([i, j]) elif d1_d_w == word: res.append([i, j]) elif d1_u_w == word: res.append([i, j]) elif d2_d_w == word: res.append([i, j]) elif d2_u_w == word: res.append([i, j]) return res
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR STRING ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR STRING IF BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR STRING IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR STRING IF BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR STRING IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR STRING IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def searchWord(self, board, word): def help(board, w, r, c): x = [ [-1, 0], [1, 0], [-1, 0], [0, 1], [0, -1], [-1, -1], [-1, 1], [1, -1], [1, 1], ] if board[r][c] != w[0]: return False for i in x: rs = r + i[0] cs = c + i[1] Flag = True for k in range(1, len(w)): if ( 0 <= rs < len(board) and 0 <= cs < len(board[0]) and w[k] == board[rs][cs] ): rs += i[0] cs += i[1] else: Flag = False break if Flag: return True return False l = [] rows = len(board) cols = len(board[0]) for i in range(rows): for j in range(cols): if help(board, word, i, j): l.append([i, j]) l.sort(key=lambda x: x[0]) return l
CLASS_DEF FUNC_DEF FUNC_DEF 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 IF VAR VAR VAR VAR NUMBER RETURN NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR LIST 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 FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
direction = [[-1, 0], [1, 0], [1, 1], [1, -1], [-1, -1], [-1, 1], [0, 1], [0, -1]] class Solution: def searchWord(self, grid, word): ans = [] for i in range(len(grid)): for j in range(len(grid[0])): if self.findword(grid, i, j, word): ans.append((i, j)) return ans def findword(self, grid, row, col, word): if grid[row][col] != word[0]: return False for x, y in direction: rd, cd = row + x, col + y flag = True for k in range(1, len(word)): if ( rd in range(len(grid)) and cd in range(len(grid[0])) and word[k] == grid[rd][cd] ): rd += x cd += y else: flag = False break if flag: return True return False
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 CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR VAR VAR NUMBER RETURN NUMBER FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR RETURN NUMBER RETURN NUMBER
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def rec(self, i, j, grid, m, n, word, l, vis): paths = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]] for kk in paths: k = 0 ii, jj = i, j while 0 <= ii < m and 0 <= jj < n: if k == l: return True if grid[ii][jj] != word[k]: break ii, jj = ii + kk[0], jj + kk[1] k += 1 if k == l: return True return False def searchWord(self, grid, word): m, n = len(grid), len(grid[0]) ans = [] l = len(word) for i in range(m): for j in range(n): vis = [([0] * n) for i in range(m)] if self.rec(i, j, grid, m, n, word, l, vis): ans.append([i, j]) return ans
CLASS_DEF FUNC_DEF 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 FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR WHILE NUMBER VAR VAR NUMBER VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def find(self, p, q, grid, word, r, n, m): di = [[0, -1], [0, 1], [-1, 0], [-1, -1], [-1, 1], [1, 0], [1, -1], [1, 1]] for i in range(8): c = 0 h = p h1 = q while ( h < n and h1 < m and h >= 0 and h1 >= 0 and c < r and word[c] == grid[h][h1] ): c += 1 h += di[i][0] h1 += di[i][1] if c == r: return True return False def searchWord(self, grid, word): n = len(grid) m = len(grid[0]) l = [] for i in range(n): for j in range(m): if grid[i][j] == word[0]: if self.find(i, j, grid, word, len(word), n, m): l.append([i, j]) return l
CLASS_DEF FUNC_DEF 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 FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def searchWord(self, grid, word): def helper(i, j, k, n, m, l, x, y): if k == l: return True if i < 0 or j < 0 or j >= m or i >= n or grid[i][j] != word[k]: return False return helper(i + x, j + y, k + 1, n, m, l, x, y) n, m = len(grid), len(grid[0]) l = len(word) res = [] direction = [ [0, 1], [1, 0], [0, -1], [-1, 0], [1, 1], [1, -1], [-1, -1], [-1, 1], ] for i in range(n): for j in range(m): for x, y in direction: if grid[i][j] == word[0]: if helper(i, j, 0, n, m, l, x, y): res.append([i, j]) break return res
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST 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 FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def isValid(self, x, y, n, m): if x >= 0 and y >= 0 and x < n and y < m: return True return False def solve(self, mat, word, i, j, n, m, X, Y): for x in range(8): nx = i + X[x] ny = j + Y[x] flag = True for w in range(1, len(word)): if self.isValid(nx, ny, n, m) and mat[nx][ny] == word[w]: nx += X[x] ny += Y[x] else: flag = False if flag: return True return False def searchWord(self, mat, word): n = len(mat) m = len(mat[0]) X = [0, 0, 1, -1, -1, -1, 1, 1] Y = [1, -1, 0, 0, -1, 1, 1, -1] ans = [] for i in range(n): for j in range(m): if mat[i][j] == word[0]: if self.solve(mat, word, i, j, n, m, X, Y): ans.append([i, j]) return ans
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR RETURN NUMBER RETURN NUMBER 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 ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def solve(self, i, j, grid, word, x, temp): if x == len(word): return True if ( (temp == "top" or temp == "first") and i + 1 < len(grid) and grid[i + 1][j] == word[x] ): if self.solve(i + 1, j, grid, word, x + 1, "top"): return True if ( (temp == "bottom" or temp == "first") and i - 1 >= 0 and grid[i - 1][j] == word[x] ): if self.solve(i - 1, j, grid, word, x + 1, "bottom"): return True if ( (temp == "left" or temp == "first") and j - 1 >= 0 and grid[i][j - 1] == word[x] ): if self.solve(i, j - 1, grid, word, x + 1, "left"): return True if ( (temp == "right" or temp == "first") and j + 1 < len(grid[0]) and grid[i][j + 1] == word[x] ): if self.solve(i, j + 1, grid, word, x + 1, "right"): return True if ( (temp == "topleft" or temp == "first") and i - 1 >= 0 and j - 1 >= 0 and grid[i - 1][j - 1] == word[x] ): if self.solve(i - 1, j - 1, grid, word, x + 1, "topleft"): return True if ( (temp == "bottomright" or temp == "first") and i + 1 < len(grid) and j + 1 < len(grid[0]) and grid[i + 1][j + 1] == word[x] ): if self.solve(i + 1, j + 1, grid, word, x + 1, "bottomright"): return True if ( (temp == "bottomleft" or temp == "first") and i + 1 < len(grid) and j - 1 >= 0 and grid[i + 1][j - 1] == word[x] ): if self.solve(i + 1, j - 1, grid, word, x + 1, "bottomleft"): return True if ( (temp == "topright" or temp == "first") and i - 1 >= 0 and j + 1 < len(grid[0]) and grid[i - 1][j + 1] == word[x] ): if self.solve(i - 1, j + 1, grid, word, x + 1, "topright"): return True return False def searchWord(self, grid, word): res = [] for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] == word[0]: if self.solve(i, j, grid, word, 1, "first"): res.append((i, j)) return res
CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR STRING VAR STRING BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER STRING RETURN NUMBER IF VAR STRING VAR STRING BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER STRING RETURN NUMBER IF VAR STRING VAR STRING BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER STRING RETURN NUMBER IF VAR STRING VAR STRING BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER STRING RETURN NUMBER IF VAR STRING VAR STRING BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER STRING RETURN NUMBER IF VAR STRING VAR STRING BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER STRING RETURN NUMBER IF VAR STRING VAR STRING BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER STRING RETURN NUMBER IF VAR STRING VAR STRING BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER STRING RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def searchWord(self, grid, word): result = [] for i in range(0, len(grid)): for j in range(0, len(grid[0])): if grid[i][j] == word[0] and self.dfs(i, j, 0, word, grid, 0): result.append([i, j]) return result def dfs(self, i, j, count, word, grid, prevdir): if len(word) == count: return True if ( i >= 0 and j >= 0 and i < len(grid) and j < len(grid[0]) and grid[i][j] == word[count] ): temp = grid[i][j] grid[i][j] = 0 if prevdir == 0: if ( self.dfs(i + 1, j, count + 1, word, grid, 1) or self.dfs(i - 1, j, count + 1, word, grid, 2) or self.dfs(i, j + 1, count + 1, word, grid, 3) or self.dfs(i, j - 1, count + 1, word, grid, 4) or self.dfs(i + 1, j + 1, count + 1, word, grid, 5) or self.dfs(i - 1, j - 1, count + 1, word, grid, 6) or self.dfs(i - 1, j + 1, count + 1, word, grid, 7) or self.dfs(i + 1, j - 1, count + 1, word, grid, 8) ): grid[i][j] = temp return True if ( prevdir == 1 and self.dfs(i + 1, j, count + 1, word, grid, 1) or prevdir == 2 and self.dfs(i - 1, j, count + 1, word, grid, 2) or prevdir == 3 and self.dfs(i, j + 1, count + 1, word, grid, 3) or prevdir == 4 and self.dfs(i, j - 1, count + 1, word, grid, 4) or prevdir == 5 and self.dfs(i + 1, j + 1, count + 1, word, grid, 5) or prevdir == 6 and self.dfs(i - 1, j - 1, count + 1, word, grid, 6) or prevdir == 7 and self.dfs(i - 1, j + 1, count + 1, word, grid, 7) or prevdir == 8 and self.dfs(i + 1, j - 1, count + 1, word, grid, 8) ): grid[i][j] = temp return True grid[i][j] = temp return False
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN NUMBER IF VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR RETURN NUMBER
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def solve(self, i, j, grid, word, idx, dirx, diry): if idx == len(word): return True else: found = False if ( 0 <= i + dirx < len(grid) and 0 <= j + diry < len(grid[0]) and grid[i + dirx][j + diry] == word[idx] ): found |= self.solve(i + dirx, j + diry, grid, word, idx + 1, dirx, diry) return found def searchWord(self, grid, word): dx = [1, -1, 0, 0, 1, 1, -1, -1] dy = [0, 0, 1, -1, 1, -1, 1, -1] r = len(grid) c = len(grid[0]) ans = [] for i in range(r): for j in range(c): if grid[i][j] == word[0]: found = False if len(word) == 1: found = True else: found = False for k in range(8): if ( 0 <= i + dx[k] < len(grid) and 0 <= j + dy[k] < len(grid[0]) and grid[i + dx[k]][j + dy[k]] == word[1] ): found |= self.solve( i + dx[k], j + dy[k], grid, word, 2, dx[k], dy[k] ) if found: ans.append((i, j)) return ans
CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER IF NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def searchWord(self, grid, word): ans = [] n = len(grid) m = len(grid[0]) def find(i, j, k, dr, word, grid): if k == len(word): return True if ( i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] != word[k] ): return False if dr == 0 and grid[i][j] == word[k]: return find(i, j + 1, k + 1, 0, word, grid) elif dr == 1 and grid[i][j] == word[k]: return find(i, j - 1, k + 1, 1, word, grid) elif dr == 2 and grid[i][j] == word[k]: return find(i + 1, j, k + 1, 2, word, grid) elif dr == 3 and grid[i][j] == word[k]: return find(i - 1, j, k + 1, 3, word, grid) elif dr == 4 and grid[i][j] == word[k]: return find(i + 1, j + 1, k + 1, 4, word, grid) elif dr == 5 and grid[i][j] == word[k]: return find(i + 1, j - 1, k + 1, 5, word, grid) elif dr == 6 and grid[i][j] == word[k]: return find(i - 1, j + 1, k + 1, 6, word, grid) elif dr == 7 and grid[i][j] == word[k]: return find(i - 1, j - 1, k + 1, 7, word, grid) return False for i in range(n): for j in range(m): if grid[i][j] == word[0]: if find(i, j + 1, 1, 0, word, grid): ans.append([i, j]) elif find(i, j - 1, 1, 1, word, grid): ans.append([i, j]) elif find(i + 1, j, 1, 2, word, grid): ans.append([i, j]) elif find(i - 1, j, 1, 3, word, grid): ans.append([i, j]) elif find(i + 1, j + 1, 1, 4, word, grid): ans.append([i, j]) elif find(i + 1, j - 1, 1, 5, word, grid): ans.append([i, j]) elif find(i - 1, j + 1, 1, 6, word, grid): ans.append([i, j]) elif find(i - 1, j - 1, 1, 7, word, grid): ans.append([i, j]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR RETURN NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def searchWord(self, grid, word): def possible(word, grid, i, j, d): row = [0, 1, 0, -1, 1, 1, -1, -1] col = [1, 0, -1, 0, 1, -1, -1, 1] if d == 0: right = len(grid[0]) - j - 1 >= len(word) - 1 if not right: return if d == 1: down = len(grid) - i - 1 >= len(word) - 1 if not down: return if d == 2: left = j >= len(word) - 1 if not left: return if d == 3: up = i >= len(word) - 1 if not up: return if d == 4: right = len(grid[0]) - j - 1 >= len(word) - 1 down = len(grid) - i - 1 >= len(word) - 1 if not right or not down: return if d == 5: left = j >= len(word) - 1 down = len(grid) - i - 1 >= len(word) - 1 if not left or not down: return if d == 6: up = i >= len(word) - 1 left = j >= len(word) - 1 if not left or not up: return if d == 7: up = i >= len(word) - 1 right = len(grid[0]) - j - 1 >= len(word) - 1 if not right or not up: return h = 1 a = row[d] + i b = col[d] + j while True: if word[h] == grid[a][b]: if h == len(word) - 1: return True h += 1 a += row[d] b += col[d] else: return result = [] for i in range(len(grid)): for j in range(len(grid[0])): if word[0] == grid[i][j]: if len(word) == 1: result.append([i, j]) continue for k in range(8): if possible(word, grid, i, j, k): result.append([i, j]) break return result
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR RETURN IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR RETURN IF VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR RETURN IF VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR RETURN IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR RETURN IF VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR RETURN IF VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR RETURN IF VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR RETURN ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR WHILE NUMBER IF VAR VAR VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR RETURN ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def searchWord(self, grid, word): dx = [1, 1, 0, -1, -1, -1, 0, 1] dy = [0, 1, 1, 1, 0, -1, -1, -1] res = [] for i in range(len(grid)): for j in range(len(grid[0])): for k in range(8): x = i y = j flag = True for l in range(len(word)): if ( x >= len(grid) or x < 0 or y >= len(grid[0]) or y < 0 or grid[x][y] != word[l] ): flag = False break else: x += dx[k] y += dy[k] if flag: res.append([i, j]) break return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def searchWord(self, grid, word): n = len(grid) m = len(grid[0]) d = [[-1, 0], [1, 0], [1, 1], [1, -1], [-1, -1], [-1, 1], [0, 1], [0, -1]] def solve(grid, row, col, w, n, m): if w[0] != grid[row][col]: return False for di in d: r = row + di[0] c = col + di[1] flag = True for j in range(1, len(w)): if r >= 0 and r < n and c >= 0 and c < m and w[j] == grid[r][c]: r += di[0] c += di[1] else: flag = False break if flag: return True return False ans = [] for i in range(n): for j in range(m): if solve(grid, i, j, word, n, m): ans.append([i, j]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR 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 FUNC_DEF IF VAR NUMBER VAR VAR VAR RETURN NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def searchWord(self, grid, word): ans = set() dir = [[-1, -1], [-1, 1], [1, -1], [1, 1], [-1, 0], [0, -1], [0, 1], [1, 0]] def dfs(i, j, ind, d): if ind == len(word): return True if i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]): return False if grid[i][j] != word[ind]: return False return dfs(i + dir[d][0], j + dir[d][1], ind + 1, d) for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] == word[0]: for k in range(8): if dfs(i, j, 0, k): ans.add((i, j)) return sorted(list(ans))
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR 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 FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def searchWord(self, grid, word): row = len(grid) col = len(grid[0]) directory = [ [-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1], ] output = [] for i in range(row): for j in range(col): if self.findString(grid, word, i, j, row, col, directory): output.append([i, j]) return output def findString(self, grid, word, row, col, row_max, col_max, directory): if grid[row][col] != word[0]: return False for x, y in directory: rd = row + x cd = col + y flag = True for k in range(1, len(word)): if 0 <= rd < row_max and 0 <= cd < col_max and word[k] == grid[rd][cd]: rd = rd + x cd = cd + y else: flag = False break if flag: return True return False
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR 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 ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR FUNC_DEF IF VAR VAR VAR VAR NUMBER RETURN NUMBER FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR RETURN NUMBER RETURN NUMBER
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def check(self, grid, word, n, m, n_word, i, j): y = i x = j index = 0 while y >= 0 and grid[y][x] == word[index]: y -= 1 index += 1 if index == n_word: return True y = i x = j index = 0 while y >= 0 and x < m and grid[y][x] == word[index]: y -= 1 x += 1 index += 1 if index == n_word: return True y = i x = j index = 0 while x < m and grid[y][x] == word[index]: x += 1 index += 1 if index == n_word: return True y = i x = j index = 0 while y < n and x < m and grid[y][x] == word[index]: y += 1 x += 1 index += 1 if index == n_word: return True y = i x = j index = 0 while y < n and grid[y][x] == word[index]: y += 1 index += 1 if index == n_word: return True y = i x = j index = 0 while y < n and x >= 0 and grid[y][x] == word[index]: y += 1 x -= 1 index += 1 if index == n_word: return True y = i x = j index = 0 while x >= 0 and grid[y][x] == word[index]: x -= 1 index += 1 if index == n_word: return True y = i x = j index = 0 while y >= 0 and x >= 0 and grid[y][x] == word[index]: y -= 1 x -= 1 index += 1 if index == n_word: return True return False def searchWord(self, grid, word): n = len(grid) m = len(grid[0]) n_word = len(word) ans = [] for i in range(n): for j in range(m): if self.check(grid, word, n, m, n_word, i, j): ans.append([i, j]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions. Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once. Example 1: Input: grid = {{a,b,c},{d,r,f},{g,h,i}}, word = "abc" Output: {{0,0}} Explanation: From (0,0) one can find "abc" in horizontally right direction. Example 2: Input: grid = {{a,b,a,b},{a,b,e,b},{e,b,e,b}} ,word = "abe" Output: {{0,0},{0,2},{1,0}} Explanation: From (0,0) one can find "abe" in right-down diagonal. From (0,2) one can find "abe" in left-down diagonal. From (1,0) one can find "abe" in Horizontally right direction. Your Task: You don't need to read or print anything, Your task is to complete the function searchWord() which takes grid and word as input parameters and returns a list containing the positions from where the word originates in any direction. If there is no such position then returns an empty list. Expected Time Complexity: O(n*m*k) where k is constant Expected Space Complexity: O(1) Constraints: 1 <= n <= m <= 100 1 <= |word| <= 10
class Solution: def searchWord(self, grid, word): n, m, l, x = len(grid), len(grid[0]), len(word), [] def helper(i, j, k, n, m, l, di, dj): if k == l: return True if j < 0 or i < 0 or j >= m or i >= n or grid[i][j] != word[k]: return False return helper(i + di, j + dj, k + 1, n, m, l, di, dj) moves = [[0, 1], [1, 0], [0, -1], [-1, 0], [1, 1], [1, -1], [-1, -1], [-1, 1]] for i in range(n): for j in range(m): for di, dj in moves: if grid[i][j] == word[0]: if helper(i, j, 0, n, m, l, di, dj): x.append([i, j]) break return x
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR LIST FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR 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 FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target. Each number in candidates may only be used once in the combination. Note: All numbers (including target) will be positive integers. The solution set must not contain duplicate combinations. Example 1: Input: candidates = [10,1,2,7,6,1,5], target = 8, A solution set is: [ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6] ] Example 2: Input: candidates = [2,5,2,1,2], target = 5, A solution set is: [   [1,2,2],   [5] ]
class Solution: def combinationSum2(self, candidates, target): result = [] def combinationSumRecu(self, candidates, target, start, intermediate, result): if target == 0: result.append(list(intermediate)) while start < len(candidates) and candidates[start] <= target: intermediate.append(candidates[start]) combinationSumRecu( self, candidates, target - candidates[start], start + 1, intermediate, result, ) start += 1 intermediate.pop() while ( start > 0 and start < len(candidates) and candidates[start] == candidates[start - 1] ): start += 1 combinationSumRecu(self, sorted(candidates), target, 0, [], result) return result
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER LIST VAR RETURN VAR
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target. Each number in candidates may only be used once in the combination. Note: All numbers (including target) will be positive integers. The solution set must not contain duplicate combinations. Example 1: Input: candidates = [10,1,2,7,6,1,5], target = 8, A solution set is: [ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6] ] Example 2: Input: candidates = [2,5,2,1,2], target = 5, A solution set is: [   [1,2,2],   [5] ]
class Solution: def find(self, candidates, result, current, target, pos, num, current_num): if target == 0: temp = current[:] result.append(temp) else: for i in range(pos, len(candidates)): n = str(candidates[i]) if candidates[i] > target or num[n] < current_num[n]: break if num[n] == current_num[n]: continue current.append(candidates[i]) current_num[n] += 1 self.find( candidates, result, current, target - candidates[i], i, num, current_num, ) value = current.pop() current_num[str(value)] -= 1 def combinationSum2(self, candidates, target): result = [] current = [] candidates.sort() can = candidates[:] count = 0 num = {} current_num = {} for i in range(len(candidates)): if i == 0 or candidates[i] != candidates[i - 1]: num[str(candidates[i])] = 1 current_num[str(candidates[i])] = 0 continue else: num[str(candidates[i])] += 1 del can[i - count] count += 1 self.find(can, result, current, target, 0, num, current_num) return result
CLASS_DEF FUNC_DEF IF VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR RETURN VAR
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target. Each number in candidates may only be used once in the combination. Note: All numbers (including target) will be positive integers. The solution set must not contain duplicate combinations. Example 1: Input: candidates = [10,1,2,7,6,1,5], target = 8, A solution set is: [ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6] ] Example 2: Input: candidates = [2,5,2,1,2], target = 5, A solution set is: [   [1,2,2],   [5] ]
class Solution: def combinationSum2(self, candidates, target): def dfs(i, val, path): while i < len(candidates): num = candidates[i] val_ = val + num path_ = path + [num] if val_ > target: return elif val_ == target: ans.append(path_) return dfs(i + 1, val_, path_) while i < len(candidates) - 1 and candidates[i] == candidates[i + 1]: i += 1 i += 1 candidates = sorted(candidates) ans = [] dfs(0, 0, []) return ans
CLASS_DEF FUNC_DEF FUNC_DEF WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR LIST VAR IF VAR VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER NUMBER LIST RETURN VAR
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target. Each number in candidates may only be used once in the combination. Note: All numbers (including target) will be positive integers. The solution set must not contain duplicate combinations. Example 1: Input: candidates = [10,1,2,7,6,1,5], target = 8, A solution set is: [ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6] ] Example 2: Input: candidates = [2,5,2,1,2], target = 5, A solution set is: [   [1,2,2],   [5] ]
class Solution: def combinationSum2(self, candidates, target): dp = [None] + [set() for i in range(target)] candidates.sort() for i in candidates: if i > target: break for j in range(target - i, 0, -1): dp[i + j] |= {(_ + (i,)) for _ in dp[j]} dp[i].add((i,)) return list(dp[-1])
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NONE FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target. Each number in candidates may only be used once in the combination. Note: All numbers (including target) will be positive integers. The solution set must not contain duplicate combinations. Example 1: Input: candidates = [10,1,2,7,6,1,5], target = 8, A solution set is: [ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6] ] Example 2: Input: candidates = [2,5,2,1,2], target = 5, A solution set is: [   [1,2,2],   [5] ]
class Solution: def combinationSum2(self, candidates, target): def backtracing(nums, target, idx, path, ret): if not target: if path not in ret: ret.append(path) for i in range(idx, len(nums)): if nums[i] > target: break backtracing(nums, target - nums[i], i + 1, path + [nums[i]], ret) candidates.sort() ret = [] backtracing(candidates, target, 0, [], ret) return ret
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR NUMBER LIST VAR RETURN VAR
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target. Each number in candidates may only be used once in the combination. Note: All numbers (including target) will be positive integers. The solution set must not contain duplicate combinations. Example 1: Input: candidates = [10,1,2,7,6,1,5], target = 8, A solution set is: [ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6] ] Example 2: Input: candidates = [2,5,2,1,2], target = 5, A solution set is: [   [1,2,2],   [5] ]
class Solution: def combinationSum2(self, candidates, target): sortedCandidates = sorted(candidates) if len(candidates) == 0 or target < sortedCandidates[0]: return [] if target == sortedCandidates[0]: return [[sortedCandidates[0]]] resultWith0 = self.combinationSum2( sortedCandidates[1:], target - sortedCandidates[0] ) nextIndex = 0 while ( nextIndex < len(candidates) and sortedCandidates[nextIndex] == sortedCandidates[0] ): nextIndex += 1 resultNo0 = self.combinationSum2(sortedCandidates[nextIndex:], target) return [([sortedCandidates[0]] + item) for item in resultWith0] + resultNo0
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER RETURN LIST IF VAR VAR NUMBER RETURN LIST LIST VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP BIN_OP LIST VAR NUMBER VAR VAR VAR VAR
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target. Each number in candidates may only be used once in the combination. Note: All numbers (including target) will be positive integers. The solution set must not contain duplicate combinations. Example 1: Input: candidates = [10,1,2,7,6,1,5], target = 8, A solution set is: [ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6] ] Example 2: Input: candidates = [2,5,2,1,2], target = 5, A solution set is: [   [1,2,2],   [5] ]
class Solution: def combinationSum2(self, candidates, target): res = [] candidates.sort() self.dfs(candidates, target, 0, [], res) return res def dfs(self, nums, target, index, path, res): if target < 0: return if target == 0: res.append(path) return for i in range(index, len(nums)): if i > index and nums[i] == nums[i - 1]: continue if nums[i] > target: break self.dfs(nums, target - nums[i], i + 1, path + [nums[i]], res)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER LIST VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR VAR VAR
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target. Each number in candidates may only be used once in the combination. Note: All numbers (including target) will be positive integers. The solution set must not contain duplicate combinations. Example 1: Input: candidates = [10,1,2,7,6,1,5], target = 8, A solution set is: [ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6] ] Example 2: Input: candidates = [2,5,2,1,2], target = 5, A solution set is: [   [1,2,2],   [5] ]
class Solution(object): def combinationSum2(self, candidates, target): result = [] temp = [] candidates.sort(reverse=True) self.util(candidates, target, result, temp) return result def util(self, nums, target, result, temp): for i in range(len(nums)): if nums[i] == target and temp + [nums[i]] not in result: result.append(temp + [nums[i]]) elif nums[i] < target: self.util(nums[i + 1 :], target - nums[i], result, temp + [nums[i]]) return
CLASS_DEF VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR LIST VAR VAR RETURN
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target. Each number in candidates may only be used once in the combination. Note: All numbers (including target) will be positive integers. The solution set must not contain duplicate combinations. Example 1: Input: candidates = [10,1,2,7,6,1,5], target = 8, A solution set is: [ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6] ] Example 2: Input: candidates = [2,5,2,1,2], target = 5, A solution set is: [   [1,2,2],   [5] ]
class Solution: def combinationSum2(self, candidates, target): def produce(nums, target, current, output): if target == 0: output.append(list(current)) return for i, n in enumerate(nums): if n > target: continue if i > 0 and nums[i] == nums[i - 1]: continue current.append(n) produce(nums[i + 1 :], target - n, current, output) current.pop() output = [] candidates.sort() produce(candidates, target, [], output) return output
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR LIST VAR RETURN VAR
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target. Each number in candidates may only be used once in the combination. Note: All numbers (including target) will be positive integers. The solution set must not contain duplicate combinations. Example 1: Input: candidates = [10,1,2,7,6,1,5], target = 8, A solution set is: [ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6] ] Example 2: Input: candidates = [2,5,2,1,2], target = 5, A solution set is: [   [1,2,2],   [5] ]
class Solution: def combinationSum2(self, candidates, target): def combination(candidates, k, target, res, result): for i in range(k, len(candidates)): if target == candidates[i]: temp = [j for j in res] temp.append(candidates[i]) temp.sort() if temp not in result: result.append(temp) return elif target < candidates[i]: return else: res.append(candidates[i]) combination(candidates, i + 1, target - candidates[i], res, result) res.pop() candidates.sort() res = [] result = [] combination(candidates, 0, target, res, result) return result
CLASS_DEF FUNC_DEF FUNC_DEF FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER VAR VAR VAR RETURN VAR
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target. Each number in candidates may only be used once in the combination. Note: All numbers (including target) will be positive integers. The solution set must not contain duplicate combinations. Example 1: Input: candidates = [10,1,2,7,6,1,5], target = 8, A solution set is: [ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6] ] Example 2: Input: candidates = [2,5,2,1,2], target = 5, A solution set is: [   [1,2,2],   [5] ]
class Solution: def combinationSum2(self, candidates, target): def helper(L, target, path, ans): if target == 0: ans.append(path) return for i in range(len(L)): if L[i] > target: return if i > 0 and L[i] == L[i - 1]: continue helper(L[i + 1 :], target - L[i], path + [L[i]], ans) ans = [] helper(sorted(candidates), target, [], ans) return ans
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR LIST VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR LIST VAR RETURN VAR
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target. Each number in candidates may only be used once in the combination. Note: All numbers (including target) will be positive integers. The solution set must not contain duplicate combinations. Example 1: Input: candidates = [10,1,2,7,6,1,5], target = 8, A solution set is: [ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6] ] Example 2: Input: candidates = [2,5,2,1,2], target = 5, A solution set is: [   [1,2,2],   [5] ]
class Solution: def combinationSum2(self, candidates, target): if not candidates: return [] result = [] candidates.sort() self.dfs(result, [], 0, target, candidates) return result def dfs(self, result, combin, start, target, candidates): if target == 0: result.append([i for i in combin]) else: for index in range(start, len(candidates)): if candidates[index] > target: break if index > start and candidates[index] == candidates[index - 1]: continue combin.append(candidates[index]) self.dfs( result, combin, index + 1, target - candidates[index], candidates ) combin.pop()
CLASS_DEF FUNC_DEF IF VAR RETURN LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR LIST NUMBER VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target. Each number in candidates may only be used once in the combination. Note: All numbers (including target) will be positive integers. The solution set must not contain duplicate combinations. Example 1: Input: candidates = [10,1,2,7,6,1,5], target = 8, A solution set is: [ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6] ] Example 2: Input: candidates = [2,5,2,1,2], target = 5, A solution set is: [   [1,2,2],   [5] ]
class Solution: def combinationSum2(self, nums, target): def dfs(a, i, target): for j in range(i, len(nums)): if target <= nums[j]: if target == nums[j]: ans.append(a + [nums[j]]) break if i == j or nums[j] != nums[j - 1]: dfs(a + [nums[j]], j + 1, target - nums[j]) ans = [] nums.sort() dfs([], 0, target) return ans
CLASS_DEF FUNC_DEF FUNC_DEF FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST NUMBER VAR RETURN VAR
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, A, B): count = 0 b = 0 new = A while len(new) <= len(B) + len(A): if B in new: return count + 1 count += 1 new = new + A return -1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN NUMBER
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, A, B): la = len(A) lb = len(B) min_repeat = 1 repeated_a = A while len(repeated_a) <= la + lb: if B in repeated_a: return min_repeat repeated_a += A min_repeat += 1 return -1
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR RETURN VAR VAR VAR VAR NUMBER RETURN NUMBER
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, A, B): n = len(B) // len(A) if B in A * n: return n if B in A * (n + 1): return n + 1 if B in A * (n + 2): return n + 2 return -1
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR RETURN VAR IF VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER IF VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN NUMBER
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, A, B): temp_A = "" m = len(B) n = len(A) repeat_times = m // n + 2 if B not in A * repeat_times: return -1 cnt = 0 while True: temp_A = temp_A + A cnt += 1 if B in temp_A: return cnt
CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR RETURN NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR RETURN VAR
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, A, B): re = len(B) // len(A) stri = "" stri += A * re if B in stri: return re else: stri += A if B in stri: return re + 1 else: return -1
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING VAR BIN_OP VAR VAR IF VAR VAR RETURN VAR VAR VAR IF VAR VAR RETURN BIN_OP VAR NUMBER RETURN NUMBER
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, A, B): a = "" repeat = 0 while len(a) < len(A) + len(B): a += A repeat += 1 if B in a: return repeat return -1
CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR RETURN NUMBER
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, a, b): if b in a: return 1 s = a count = 1 while len(s) < 2 * len(b): if b in s: return count else: s += a count += 1 else: return -1
CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR IF VAR VAR RETURN VAR VAR VAR VAR NUMBER RETURN NUMBER
Given two strings A and B. Find minimum number of times A has to be repeated such that B is a Substring of it. If B can never be a substring then return -1. Example 1: Input: A = "abcd" B = "cdabcdab" Output: 3 Explanation: Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times. Example 2: Input: A = "ab" B = "cab" Output : -1 Explanation: No matter how many times we repeat A, we can't get a string such that B is a substring of it. Your Task: You don't need to read input or print anything. Your task is to complete the function minRepeats() which takes 2 strings A, and B respectively and returns the minimum number of times A has to be repeated such that B is a substring of it. Return -1 if it's not possible. Expected Time Complexity: O(|A| * |B|) Expected Auxiliary Space: O(|B|) Constraints: 1 ≤ |A|, |B| ≤ 10^{3} String A and B consists of lower case alphabets
class Solution: def minRepeats(self, A, B): matchstr = "" repeat = 0 for e in set(A): if e not in B: return -1 while len(matchstr) <= len(B): matchstr += A repeat += 1 if B in matchstr: return repeat return -1
CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR RETURN NUMBER