description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
k = n // 2
l = arr[:k]
p = arr[k:]
arr.clear()
for i in range(len(p)):
arr.append(l[i])
arr.append(p[i])
return arr | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
l = []
x = n // 2
for i in range(x):
print
l.extend([arr[i], arr[i + x]])
arr[:] = l | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
for i in range(n // 2):
arr[i] += arr[n // 2 + i] * 10000
for i in range(n // 2 - 1, -1, -1):
arr[2 * i + 1] = arr[i] // 10000
arr[2 * i] = arr[i] % 10000 | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR VAR NUMBER |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
mid = n // 2
for i in range(mid):
x = arr.pop(i + mid)
arr.insert(2 * i + 1, x)
return arr | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR RETURN VAR |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
L = n // 2
l1 = arr[0:L]
l2 = arr[L:n]
a = []
for i in range(L):
a.append(l1[i])
a.append(l2[i])
arr.clear()
arr.extend(a)
return arr | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
k = n // 2
h1 = arr[:k]
h2 = arr[k:]
arr.clear()
for i in range(n // 2):
arr += [h1[i]]
arr += [h2[i]]
return arr | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR LIST VAR VAR VAR LIST VAR VAR RETURN VAR |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
l = n // 2
p = []
q = []
for i in range(l):
p.append(arr[i])
for i in range(l, n):
q.append(arr[i])
arr[0] = p[0]
px = 1
qx = 0
for i in range(1, n):
if i % 2 != 0:
arr[i] = q[qx]
qx += 1
else:
arr[i] = p[px]
px += 1
return arr | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER 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):
ai = 0
bi = 0
def isEqual(a):
b = 0
for _ in range(len(B)):
if A[a] != B[b]:
return False
b += 1
a = (a + 1) % len(A)
return True
start = -1
for i in range(len(A)):
if A[i] == B[0]:
if isEqual(i):
start = i
break
if start == -1:
return -1
if start == 0:
remaining = len(B) - len(A)
else:
remaining = len(B) - start + 1
count = 1
while remaining > 0:
count += 1
remaining -= len(A)
return count | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR FUNC_CALL 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):
for i in B:
if i not in A:
return -1
S = A
c = 1
while len(S) < len(B):
S = S + A
c = c + 1
if B in S:
return c
elif B in S + A:
return c + 1
else:
return -1 | CLASS_DEF FUNC_DEF FOR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR RETURN VAR IF VAR BIN_OP 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):
cnt = 0
C = ""
while len(C) < len(B):
C += A
cnt += 1
if B in C:
return cnt
else:
C += A
cnt += 1
if B in C:
return cnt
else:
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR STRING WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN 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):
x = A * (len(B) // len(A) + len(A))
if B not in x:
return -1
c = 1
x = A
while B not in x:
x = x + A
c += 1
return c | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER 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 = 1
if A.find(B) > -1:
return count
s = A
found = False
while len(s) < 2 * len(B):
s = s + A
count += 1
if s.find(B) != -1:
found = True
break
if found:
return count
else:
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF 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):
x = len(a)
y = len(b)
if x > y and b in a:
return 1
elif x > y and b in a * 2:
return 2
elif x > y:
return -1
elif y % x == 0 and b == a * (y // x):
return y // x
elif b in a * (y // x + 1):
return y // x + 1
else:
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR IF VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP BIN_OP 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):
ans = -1
for i in range(len(A)):
index = i
found = True
for j in range(len(B)):
if A[index % len(A)] != B[j]:
found = False
break
index += 1
if found:
ans = index // len(A)
if len(A) * ans != index:
ans += 1
break
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER 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):
start_ix = A.find(B[0])
if start_ix == -1:
return -1
ans = 1
ans += (start_ix + len(B)) // len(A)
if ans == 1 and B not in A:
ans += 1
S = A * ans
if B in S:
return ans if S.find(B) + len(B) > len(S) - len(A) else ans - 1
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR 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):
S = A
ans = 1
while len(S) < len(B):
S += A
ans += 1
if B in S:
return ans
elif B in S + A:
return ans + 1
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR IF VAR BIN_OP 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):
m = len(A)
n = len(B)
count = 0
found = False
for i in range(m):
j = i
k = 0
count = 1
while k < n and A[j] == B[k]:
if k == n - 1:
found = True
break
j = (j + 1) % m
if j == 0:
count += 1
k += 1
if found:
return count
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF 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):
i = 0
s = A
while B not in s and len(s) < 2 * len(B):
if B[i] not in A:
return -1
s += A
i += 1
if B in s:
return i + 1
else:
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR RETURN NUMBER VAR VAR VAR NUMBER 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):
s = ""
if B in A:
return 1
nA = len(A)
nB = len(B)
nS = 0
cnt = 0
while nS < nB:
s += A
nS += nA
cnt += 1
if B in s:
return cnt
elif B in s + A:
return cnt + 1
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR STRING IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR IF VAR BIN_OP 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):
count = 0
string = ""
if set(B) <= set(A):
while B not in string:
string += A
count += 1
if len(string) >= len(B):
break
if B in string:
return count
elif B in string + A:
return count + 1
else:
return -1
else:
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR STRING IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR IF VAR BIN_OP VAR VAR RETURN BIN_OP VAR NUMBER RETURN 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):
c = 0
i = 0
while i < len(B) - len(A):
if A == B[i : i + len(A)]:
c += 1
i = i + len(A) - 1
i += 1
if B in A * c:
return c
elif B in A + A * c or B in A * c + A:
return c + 1
elif B in A + A * c + A:
return c + 2
else:
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR RETURN VAR IF VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR RETURN BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR BIN_OP VAR 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):
i = 0
ans = 1
while i < len(a):
f = 0
if a[i] == b[0]:
j = i
k = 0
while k < len(b):
if a[j] != b[k]:
f = 1
break
j += 1
k += 1
if j == len(a) and k != len(b):
ans += 1
j = 0
if f == 0:
break
i += 1
if f != 0 or i == len(a):
return -1
else:
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR RETURN NUMBER 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):
S = A
count = 1
for i in range(400):
if B in S:
return count
else:
S = S + A
count += 1
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR RETURN VAR ASSIGN VAR BIN_OP 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):
i = A.find(B[0])
j = (i + len(B)) / len(A)
if B in A * (int(j) + 2):
if int(j) < j:
j = int(j + 1)
else:
j = int(j)
if j == 1 and B not in A:
return j + 1
return j
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR RETURN BIN_OP VAR NUMBER 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):
str = A
num = 1
while B not in str:
if len(str) > len(B) and num > 3:
return -1
str += A
num = num + 1
return num | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER 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):
x = max(len(B) // len(A), len(A) // len(B))
for i in range(2):
if B in A * (x + i):
return x + i
break
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR BIN_OP VAR VAR RETURN 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):
nA = len(A)
nB = len(B)
for i in range(nA):
k = i
cnt = 1
isValid = True
for j in range(nB):
if A[k] != B[j]:
isValid = False
k = (k + 1) % nA
if k == 0:
cnt += 1
if isValid:
if k == 0:
cnt -= 1
return cnt
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER IF VAR IF VAR NUMBER VAR NUMBER 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):
c = 1
x = A
while A.find(B) == -1:
c += 1
A += x
if len(A) > len(B) and A.find(B) == -1:
return -1
if A.find(B) == -1:
return -1
return c | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER 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):
i = 0
count = 1
s = A
while i < len(B) / len(A) + 2:
if s.find(B) != -1:
return count
s += A
count += 1
i += 1
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR VAR NUMBER 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):
if B in A:
return 1
cnt = 1
temp = A
while len(temp) <= len(B):
if B in temp:
return cnt
else:
cnt += 1
temp += A
if B in temp:
return cnt
temp += A
cnt += 1
if B in temp:
return cnt
return -1 | CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR VAR NUMBER VAR VAR IF VAR VAR RETURN 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
i = 2
a = ""
while len(a) <= len(B):
a = A * i
i += 1
if B in a:
return i - 1
return -1 | CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER 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):
reps = 1
index_a = len(A) - 1
index_b = len(B) - 1
current_a = index_a
while index_a >= 0:
while B[index_b] == A[current_a]:
current_a -= 1
index_b -= 1
if index_b < 0:
return reps
if current_a < 0:
current_a = len(A) - 1
reps += 1
index_b = len(B) - 1
index_a -= 1
current_a = index_a
reps = 1
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN VAR IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN 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 = set(A)
b = set(B)
if b.issubset(a):
S = ""
ans = 0
while len(S) < len(B):
S += A
ans += 1
if B in S:
return ans
elif B in S + A:
return ans + 1
else:
return -1
else:
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR IF VAR BIN_OP VAR VAR RETURN BIN_OP VAR NUMBER RETURN 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):
for i in range(1, len(B) // len(A) + 3):
if B in A * i:
return i
return -1 | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP 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):
for i in range(0, len(B)):
if B[i] not in A:
return -1
S = ""
S = S + A
c = 1
if B in S:
return c
if B in S + A:
return c + 1
f = 0
while len(S) <= len(B):
S = S + A
c = c + 1
if B in S:
return c
elif B in S + A:
return c + 1
return -1 | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR RETURN NUMBER ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR VAR RETURN VAR IF VAR BIN_OP VAR VAR RETURN BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR RETURN VAR IF VAR BIN_OP 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):
n = len(B) // len(A) + 1
start = 0
end = n + 1
mid = start + (end - start) // 2
ans = -1
while start <= end:
p = A * mid
if B in p:
ans = mid
end = mid - 1
else:
start = mid + 1
mid = start + (end - start) // 2
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER 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):
l1 = len(A)
l2 = len(B)
K = 1
s = A
while (K - 2) * l1 + 2 <= l2:
if K * l1 >= l2:
for i in range(K * l1 - l2 + 1):
if s[i : i + l2] == B:
return K
K += 1
s += A
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 BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR IF BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR NUMBER 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):
n1 = len(A)
n2 = len(B)
ans1 = -1
for k in range(n1):
i = k
ans = 0
for j in range(n2):
if i == 0:
ans += 1
if A[i] != B[j]:
ans = -1
break
i += 1
i %= n1
ans1 = max(ans1, ans)
if ans1 != -1 and B.find(A) != 0:
ans1 += 1
return ans1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER 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):
s = set(A)
for i in B:
if i not in s:
return -1
if len(B) >= len(A):
n = len(B) // len(A)
A = A * n
a = A * (n + 1)
if B in A:
return n
elif B in a:
return n + 1
else:
return -1
else:
return 2 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR RETURN VAR IF VAR VAR RETURN BIN_OP VAR NUMBER RETURN 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):
st = a
m = len(b)
flag = 0
count = 0
while len(a) < len(b):
count += 1
a += st
if b in a:
count += 1
return count
else:
a += st
count += 1
if b in a:
return count + 1
else:
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR NUMBER 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):
x = A * 10**3
if B not in x:
return -1
c = 1
x = A
while B not in x:
x = x + A
c += 1
return c | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER 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):
n, m = len(A), len(B)
start = None
for i in range(n):
if A[i] == B[0]:
new = len(A[i:])
if A[i:] == B[:new]:
start = new
break
if start == None:
return -1
if start == m:
return 1
ans = 1
while m - start >= n:
if B[start : start + n] == A:
start = start + n
ans += 1
else:
return -1
if 0 < m - start < n:
if B[start:] == A[: m - start]:
ans += 1
else:
return -1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NONE RETURN NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER RETURN NUMBER IF NUMBER BIN_OP VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN NUMBER 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):
if len(A) > len(B):
return 2
else:
a = int(len(B) / len(A))
if B in A * a:
return a
elif B in A * (a + 1):
return a + 1
return -1 | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL 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 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):
ans = -1
for i in range(len(A)):
j = 0
tempans = self.Helper(A, B, i, j)
if tempans != -1:
return tempans
return ans
def Helper(self, A, B, i, j):
n = len(A)
count = 0
flag = False
while j < len(B):
while i < len(A) and j < len(B):
if A[i] == B[j]:
flag = True
j += 1
elif flag:
return -1
i += 1
if flag:
i = i % n
count += 1
else:
return -1
if flag:
return count
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER RETURN VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR RETURN NUMBER VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER RETURN NUMBER IF 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 findSubString(self, A, B):
a_len = len(A)
b_len = len(B)
repeat = b_len - a_len
if b_len - a_len == 0:
repeat = 1
all_done = False
for i in range(repeat):
all_done = False
for j in range(a_len):
if B[i + j] != A[j]:
all_done = False
break
else:
all_done = True
if all_done:
return True
return False
def minRepeats(self, A, B):
tmp_a = A
answer = 1
while len(tmp_a) < len(B):
tmp_a += A
answer += 1
if self.findSubString(B, tmp_a):
return answer
if self.findSubString(B, tmp_a + A):
return answer + 1
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN VAR IF FUNC_CALL VAR VAR BIN_OP 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 issubstring(self, str2, rep):
if str2 in rep:
return True
return False
def minRepeats(self, A, B):
ans = 1
S = A
while len(S) < len(B):
S += A
ans += 1
if self.issubstring(B, S):
return ans
elif self.issubstring(B, S + A):
return ans + 1
else:
return -1 | CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN VAR IF FUNC_CALL VAR VAR BIN_OP 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 findChar(self, A, B):
for b in B:
if b not in A:
return False
else:
return True
def minRepeats(self, A, B):
res = self.findChar(A, B)
if res:
i = 1
temp = A
l = len(A) + 1
while A.find(B) == -1:
A += temp
i += 1
if B.find(A[:l]) == -1 and A.find(B) == -1:
return -1
return i
else:
return -1 | CLASS_DEF FUNC_DEF FOR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN NUMBER 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):
count = 1
if A == B and len(A) == len(B):
return count
elif A == B[0 : len(A)]:
s = str(A)
while len(A) < len(B):
A = A + s
count = count + 1
elif len(A) > len(B):
A = A + A
count = count + 1
else:
s = str(A)
while len(A) <= len(B):
A = A + s
count = count + 1
if B in A:
return count
else:
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR IF VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP 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):
n = len(A)
m = len(B)
for i in range(1, m // n + 2):
if B in A * i:
return i
if B in A + A:
return 2
if B in A:
return 1
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR RETURN VAR IF VAR BIN_OP VAR VAR RETURN NUMBER IF VAR VAR RETURN 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):
dic2 = {}
for i in range(len(B)):
if dic2.get(B[i]):
dic2[B[i]] += 1
else:
dic2[B[i]] = 1
for keyy in dic2.keys():
if keyy not in A:
return -1
i = 0
j = 0
n = len(A)
m = len(B)
temp = 0
while temp < n:
if A[i] != B[0]:
i = i + 1
elif A[temp] != B[temp - i]:
i = i + 1
temp = temp + 1
count = 1
while j < m:
if A[i] != B[j]:
return -1
i = i + 1
j = j + 1
if i // n == 1 and j < m:
count = count + 1
i = i % n
return count | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP 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):
a = len(A)
b = len(B)
for i in range(a):
if A[i:] != B[: min(a - i, b)]:
continue
t = 1
j = min(a - i, b)
valid = True
while j < b:
if B[j : min(j + a, b)] == A[: min(a, b - j)]:
j += a
t += 1
else:
valid = False
break
if valid:
return t
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF 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 len(A) >= len(B):
if B in A:
return 1
A += A
if B in A:
return 2
return -1
c = 1
st = A
while len(A) <= len(B):
if B in A:
return c
A += st
c += 1
if B in A:
return c
else:
return -1 | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN 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):
n = len(A)
m = len(B)
count = 1
repeated_A = A
while len(repeated_A) < m:
repeated_A += A
count += 1
if B in repeated_A:
return count
repeated_A += A
count += 1
if B in repeated_A:
return count
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 VAR VAR VAR VAR NUMBER IF VAR VAR RETURN 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):
max_len = len(B) // len(A) + 2
for i in range(1, max_len + 1):
if B in A * i:
return i
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR RETURN VAR RETURN NUMBER |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class Solution:
def wordBoggle(self, board, dictionary):
ans = set()
for word in dictionary:
self.searchWord(ans, word, board)
return list(ans)
def searchWord(self, ans, word, board):
visited = [([False] * len(board[0])) for _ in range(len(board))]
idx = 0
for i in range(len(board)):
for j in range(len(board[0])):
if board[i][j] == word[0]:
self.solver(board, dictionary, ans, visited, i, j, "", 0, word)
def solver(self, board, dictionary, ans, visited, i, j, ssf, idx, word):
if (
i < 0
or j < 0
or i >= len(board)
or j >= len(board[0])
or visited[i][j] == True
or board[i][j] != word[idx]
):
return
if idx >= len(word):
return
if idx == len(word) - 1:
ans.add(word)
return
visited[i][j] = True
self.solver(
board, dictionary, ans, visited, i - 1, j, ssf + board[i][j], idx + 1, word
)
self.solver(
board,
dictionary,
ans,
visited,
i - 1,
j + 1,
ssf + board[i][j],
idx + 1,
word,
)
self.solver(
board, dictionary, ans, visited, i, j + 1, ssf + board[i][j], idx + 1, word
)
self.solver(
board,
dictionary,
ans,
visited,
i + 1,
j + 1,
ssf + board[i][j],
idx + 1,
word,
)
self.solver(
board, dictionary, ans, visited, i + 1, j, ssf + board[i][j], idx + 1, word
)
self.solver(
board,
dictionary,
ans,
visited,
i + 1,
j - 1,
ssf + board[i][j],
idx + 1,
word,
)
self.solver(
board, dictionary, ans, visited, i, j - 1, ssf + board[i][j], idx + 1, word
)
self.solver(
board,
dictionary,
ans,
visited,
i - 1,
j - 1,
ssf + board[i][j],
idx + 1,
word,
)
visited[i][j] = False | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER 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 EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR STRING NUMBER VAR FUNC_DEF IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR RETURN IF VAR FUNC_CALL VAR VAR RETURN IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class Solution:
def wordBoggle(self, board, dictionary):
n, m = len(board), len(board[0])
used = [[(False) for _ in range(m)] for _ in range(n)]
l = []
ls = set()
def dfs(row, col, s, k):
if len(s) <= k:
return True
flag = False
if 0 <= row < n and 0 <= col < m and not used[row][col]:
if s[k] == board[row][col]:
used[row][col] = True
flag = flag or dfs(row + 1, col - 1, s, k + 1)
flag = flag or dfs(row + 1, col, s, k + 1)
flag = flag or dfs(row + 1, col + 1, s, k + 1)
flag = flag or dfs(row, col - 1, s, k + 1)
flag = flag or dfs(row, col + 1, s, k + 1)
flag = flag or dfs(row - 1, col - 1, s, k + 1)
flag = flag or dfs(row - 1, col, s, k + 1)
flag = flag or dfs(row - 1, col + 1, s, k + 1)
used[row][col] = False
return flag
for w in dictionary:
if w in ls:
continue
for i in range(n):
for j in range(m):
if dfs(i, j, w, 0):
l.append(w)
ls.add(w)
break
else:
continue
break
return l | CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR FOR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class Solution:
def wordBoggle(self, board, dictionary):
ans = []
rows = len(board)
cols = len(board[0])
def dfs(r, c, i, wor, path):
if i == len(wor):
return True
if (
r < 0
or c < 0
or r >= rows
or c >= cols
or wor[i] != board[r][c]
or (r, c) in path
):
return False
path.add((r, c))
res = (
dfs(r + 1, c, i + 1, wor, path)
or dfs(r - 1, c, i + 1, wor, path)
or dfs(r - 1, c - 1, i + 1, wor, path)
or dfs(r + 1, c + 1, i + 1, wor, path)
or dfs(r, c + 1, i + 1, wor, path)
or dfs(r, c - 1, i + 1, wor, path)
or dfs(r - 1, c + 1, i + 1, wor, path)
or dfs(r + 1, c - 1, i + 1, wor, path)
)
path.remove((r, c))
return res
for wor in dictionary:
path = set()
for r in range(rows):
for c in range(cols):
if dfs(r, c, 0, wor, path) and wor not in ans:
ans.append(wor)
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 NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class Solution:
def wordBoggle(self, board, dictionary):
def dfs(k, board, i, j, R, C):
n = len(k)
bool = False
if k[0] == board[i][j] and n == 1:
return True
if k[0] == board[i][j]:
temp = board[i][j]
board[i][j] = "$"
a = [
[i - 1, j - 1],
[i - 1, j],
[i - 1, j + 1],
[i, j - 1],
[i, j + 1],
[i + 1, j - 1],
[i + 1, j],
[i + 1, j + 1],
]
for x in a:
if -1 < x[0] < R and -1 < x[1] < C:
bool = bool or dfs(k[1:], board, x[0], x[1], R, C)
if bool:
board[i][j] = temp
return True
board[i][j] = temp
return False
res = []
R = len(board)
C = len(board[0])
memo = {}
for i in dictionary:
if i[0] in memo:
if i not in memo[i[0]]:
memo[i[0]] += [i]
else:
memo[i[0]] = [i]
for i in range(R):
for j in range(C):
if board[i][j] in memo:
arr = []
for k in memo[board[i][j]]:
if dfs(k, board, i, j, R, C):
res += [k]
else:
arr += [k]
memo[board[i][j]] = arr
return res | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR STRING ASSIGN VAR LIST LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER LIST VAR BIN_OP VAR NUMBER LIST VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR IF VAR ASSIGN VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR VAR NUMBER LIST VAR ASSIGN VAR VAR NUMBER LIST VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR LIST VAR VAR LIST VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class Trie:
def __init__(self):
self.T = {}
def add(self, word):
t = self.T
for c in word:
if c not in t:
t[c] = {}
t = t[c]
t["#"] = True
class Solution:
def wordBoggle(self, board, dictionary):
t = Trie()
res = []
for w in dictionary:
t.add(w)
for r in range(len(board)):
for c in range(len(board[0])):
if len(res) == len(dictionary):
return res
self.dfs(board, t.T, r, c, "", res)
return res
def dfs(self, board, t, r, c, s, res):
if "#" in t and t["#"]:
res.append(s)
t["#"] = False
if r < 0 or r > len(board) - 1 or c < 0 or c > len(board[0]) - 1:
return
tmp = board[r][c]
if tmp not in t:
return
board[r][c] = "."
nbors = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
for dr, dc in nbors:
self.dfs(board, t[tmp], r + dr, c + dc, s + tmp, res)
board[r][c] = tmp
return | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF ASSIGN VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR DICT ASSIGN VAR VAR VAR ASSIGN VAR STRING NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR STRING VAR RETURN VAR FUNC_DEF IF STRING VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING NUMBER IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN ASSIGN VAR VAR VAR VAR IF VAR VAR RETURN ASSIGN VAR VAR VAR STRING ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class Solution:
def dfs(self, board, visited, word, root, r, c):
if len(word) == 0:
return True
i, j = root
visited.add((i, j))
neighbours = []
if i > 0:
neighbours.append((i - 1, j))
if j > 0:
neighbours.append((i, j - 1))
if i > 0 and j > 0:
neighbours.append((i - 1, j - 1))
if i < r - 1:
neighbours.append((i + 1, j))
if j < c - 1:
neighbours.append((i, j + 1))
if i < r - 1 and j < c - 1:
neighbours.append((i + 1, j + 1))
if i < r - 1 and j > 0:
neighbours.append((i + 1, j - 1))
if i > 0 and j < c - 1:
neighbours.append((i - 1, j + 1))
for neighbour in neighbours:
i, j = neighbour
if (i, j) not in visited and board[i][j] == word[0]:
finished = self.dfs(board, visited, word[1:], neighbour, r, c)
if finished:
return finished
visited.remove(root)
return False
def wordBoggle(self, board, dictionary):
starts = {}
found_words = []
r = len(board)
c = len(board[0])
visited = [([0] * c) for _ in range(r)]
for i in range(r):
for j in range(c):
for word in dictionary:
if word[0] == board[i][j]:
if word in starts:
starts[word].append((i, j))
else:
starts[word] = [(i, j)]
for word in starts:
for start in starts[word]:
found = self.dfs(board, set([start]), word[1:], start, r, c)
if found:
found_words.append(word)
break
return sorted(found_words) | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR IF VAR RETURN VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR LIST VAR VAR FOR VAR VAR FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class Solution:
def wordBoggle(self, board, dictionary):
def isvalid(x1, y1):
return x1 >= 0 and x1 < ROWS and y1 >= 0 and y1 < COLS
def check(x, y, index):
visited.add((x, y))
if index == len(word) - 1:
visited.remove((x, y))
return True
for X, Y in moves:
if (
isvalid(x + X, y + Y)
and (x + X, y + Y) not in visited
and word[index + 1] == board[x + X][y + Y]
and check(x + X, y + Y, index + 1)
):
visited.remove((x, y))
return True
visited.remove((x, y))
return False
moves = (1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (1, -1), (-1, 1), (-1, -1)
visited = set()
words = []
ROWS, COLS = len(board), len(board[0])
for i in range(ROWS):
for j in range(COLS):
k = 0
while k < len(dictionary):
if dictionary[k][0] == board[i][j] and dictionary[k] not in words:
word = dictionary[k]
if check(i, j, 0):
words.append(word)
dictionary.remove(word)
k -= 1
k += 1
return words | CLASS_DEF FUNC_DEF FUNC_DEF RETURN VAR NUMBER VAR VAR VAR NUMBER VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER FOR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR 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 ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER RETURN VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class Trie:
def __init__(self):
self.root = Node()
def insert(self, st):
curr = self.root
for i in range(len(st)):
if curr.childs[ord(st[i]) - ord("A")] == None:
curr.childs[ord(st[i]) - ord("A")] = Node()
curr = curr.childs[ord(st[i]) - ord("A")]
curr.s = st
class Node:
def __init__(self):
self.childs = [None] * 26
self.s = ""
class Solution:
def wordBoggle(self, board, dictionary):
def dfs(board, i, j, root, ans):
if (
i < 0
or j < 0
or i >= len(board)
or j >= len(board[0])
or visited[i][j] == True
):
return
if root.childs[ord(board[i][j]) - ord("A")] == None:
return
child = root.childs[ord(board[i][j]) - ord("A")]
if child.s != None and child.s != "":
ans.add(child.s)
visited[i][j] = True
for m in range(len(move)):
dfs(board, i + move[m][0], j + move[m][1], child, ans)
visited[i][j] = False
move = [[0, 1], [1, 0], [0, -1], [-1, 0], [1, 1], [1, -1], [-1, 1], [-1, -1]]
visited = [[(False) for i in range(len(board[0]))] for j in range(len(board))]
t = Trie()
for s in dictionary:
t.insert(s)
ans = set()
root = t.root
for i in range(len(board)):
for j in range(len(board[0])):
dfs(board, i, j, root, ans)
ans = list(ans)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NONE ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NONE NUMBER ASSIGN VAR STRING CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER RETURN IF VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR STRING NONE RETURN ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR STRING IF VAR NONE VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR VAR ASSIGN VAR 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 NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class Solution:
def recur_util(self, cur_ind, board, word):
res = False
visited = [([False] * len(board[0])) for _ in range(len(board))]
opts = [[0, 1], [1, 0], [1, 1], [0, -1], [-1, 0], [-1, -1], [1, -1], [-1, 1]]
def recur(x, y, visited, w_ind, word):
if x < 0 or y < 0 or x >= len(board) or y >= len(board[0]):
return
if visited[x][y]:
return
if word[w_ind] == board[x][y]:
if w_ind >= len(word) - 1:
nonlocal res
res = True
return
visited[x][y] = True
for opt in opts:
recur(x + opt[0], y + opt[1], visited, w_ind + 1, word)
visited[x][y] = False
else:
return
recur(cur_ind[0], cur_ind[1], visited, 0, word)
return res
def wordBoggle(self, board, dictionary):
res = []
for word in set(dictionary):
found = False
for i in range(len(board)):
for j in range(len(board[0])):
if board[i][j] == word[0]:
if self.recur_util((i, j), board, word):
res.append(word)
found = True
break
if found:
break
return res | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL 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 FUNC_DEF IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER RETURN IF VAR VAR VAR RETURN IF VAR VAR VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER RETURN ASSIGN VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER 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 EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR RETURN VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class TrieNode:
def __init__(self):
self.children = {}
self.end_of_word = False
class Trie:
def __init__(self):
self.root = TrieNode()
def add_word(self, word):
cur = self.root
for c in word:
if c not in cur.children:
cur.children[c] = TrieNode()
cur = cur.children[c]
cur.end_of_word = True
class Solution:
def wordBoggle(self, board, dictionary):
self.row = len(board)
self.col = len(board[0])
self.root = Trie()
for word in dictionary:
self.root.add_word(word)
self.visited = [[(0) for col in range(self.col)] for row in range(self.row)]
self.res = []
root = self.root.root
for row in range(self.row):
for col in range(self.col):
c = board[row][col]
if c in root.children:
self.dfs([row, col], root.children[c], c, board)
return self.res
def dfs(self, pos, root, path, board):
if root.end_of_word and path not in self.res:
self.res.append(path[:])
row, col = pos[0], pos[1]
self.visited[row][col] = 1
neighbors = [
[-1, 0],
[1, 0],
[0, -1],
[0, 1],
[1, 1],
[-1, -1],
[1, -1],
[-1, 1],
]
for nbr in neighbors:
new_row = row + nbr[0]
new_col = col + nbr[1]
if new_row in range(self.row) and new_col in range(self.col):
if self.visited[new_row][new_col] == 0:
c = board[new_row][new_col]
if c in root.children:
self.dfs([new_row, new_col], root.children[c], path + c, board)
self.visited[row][col] = 0
return | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR 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 FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | def dfs(mat, i, j, n, m, st, l, k):
global visited
global dirn
if k >= l:
return True
if mat[i][j] != st[k]:
return False
if k == l - 1:
return True
visited[i][j] = True
res = False
for d in dirn:
u = i + d[0]
v = j + d[1]
if u >= 0 and v >= 0 and u < n and v < m:
if visited[u][v] == False:
res = res or dfs(mat, u, v, n, m, st, l, k + 1)
if res == True:
visited[i][j] = False
return True
visited[i][j] = False
return False
def wordBoggleII(mat, words):
global visited
global dirn
n = len(mat)
m = len(mat[0])
res = []
visited = [[(False) for i in range(m)] for j in range(n)]
dirn = [(0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1)]
for word in words:
for i in range(n):
for j in range(m):
q = 0
if dfs(mat, i, j, n, m, word, len(word), 0) and word not in res:
q = 1
res.append(word)
break
if q == 1:
break
return res
class Solution:
def wordBoggle(self, board, dictionary):
return wordBoggleII(board, dictionary) | FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR RETURN NUMBER IF VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class TrieNode:
def __init__(self):
self.children = {}
self.is_word = False
class Trie:
def __init__(self):
self.root = TrieNode()
def insert(self, word):
cur = self.root
for letter in word:
if letter not in cur.children:
cur.children[letter] = TrieNode()
cur = cur.children[letter]
cur.is_word = True
def exists(self, word):
cur = self.root
for letter in word:
if letter not in cur.children:
return False
cur = cur.children[letter]
return cur.is_word
class Solution:
def wordBoggle(self, board, words):
USED = "#"
wordTrie = Trie()
for word in words:
wordTrie.insert(word)
result = set()
def backtrack(node, prefix, i, j):
if node.is_word:
result.add(prefix)
for di, dj in (
(0, 1),
(0, -1),
(-1, 0),
(1, 0),
(-1, -1),
(1, 1),
(-1, 1),
(1, -1),
):
ni, nj = i + di, j + dj
if (
0 <= ni < len(board)
and 0 <= nj < len(board[0])
and board[ni][nj] in node.children
):
tmp = board[ni][nj]
board[ni][nj] = USED
backtrack(node.children[tmp], prefix + tmp, ni, nj)
board[ni][nj] = tmp
for i in range(len(board)):
for j in range(len(board[i])):
if board[i][j] in wordTrie.root.children:
tmp = board[i][j]
board[i][j] = USED
backtrack(wordTrie.root.children[tmp], tmp, i, j)
board[i][j] = tmp
return list(result) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR FOR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR 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 ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class Solution:
def wordBoggle(self, board, dictionary):
self.R = len(board)
self.C = len(board[0])
self.board = board
res = set()
for word in dictionary:
visited = [[(False) for _ in range(self.C)] for _ in range(self.R)]
found = False
for i in range(self.R):
for j in range(self.C):
if self.board[i][j] == word[0]:
if self.dfs(word[1:], i, j, visited):
res.add(word)
found = True
break
if found:
break
return list(res)
def dfs(self, word, i, j, visited):
if not word:
return True
visited[i][j] = True
direction = [
[-1, -1],
[-1, 0],
[-1, 1],
[0, -1],
[0, 1],
[1, -1],
[1, 0],
[1, 1],
]
for d in direction:
if self.isValid(i + d[0], j + d[1], word[0], visited):
if self.dfs(word[1:], i + d[0], j + d[1], visited):
return True
visited[i][j] = False
return False
def isValid(self, i, j, ch, visited):
return (
i >= 0
and j >= 0
and i < self.R
and j < self.C
and not visited[i][j]
and ch == self.board[i][j]
) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR RETURN FUNC_CALL VAR VAR FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR 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 FOR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF RETURN VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class Solution:
def wordBoggle(self, board, dictionary):
class TrieNode:
def __init__(self):
self.child = {}
self.Eow = False
def insert(word, Trie):
for w in word:
if w not in Trie.child:
Trie.child[w] = TrieNode()
Trie = Trie.child[w]
Trie.Eow = True
def dfs(r, c, Trie, V, path):
if Trie.Eow:
res.append(path)
Trie.Eow = False
V.add(tuple((r, c)))
for ch in exp:
x, y = ch[0] + r, ch[1] + c
if (
0 <= x < L
and 0 <= y < W
and board[x][y] in Trie.child
and (x, y) not in V
):
path = path + board[x][y]
dfs(x, y, Trie.child[board[x][y]], V, path)
path = path[:-1]
V.discard(tuple((r, c)))
Trie = TrieNode()
for word in dictionary:
insert(word, Trie)
res = []
L = len(board)
W = len(board[0])
exp = [[1, 0], [-1, 0], [0, 1], [0, -1], [1, 1], [1, -1], [-1, 1], [-1, -1]]
for r in range(L):
for c in range(W):
if board[r][c] in Trie.child:
V = set()
dfs(r, c, Trie.child[board[r][c]], V, "" + board[r][c])
return res | CLASS_DEF FUNC_DEF CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER FUNC_DEF FOR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST 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 FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP STRING VAR VAR VAR RETURN VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class Solution:
def find(self, board, word):
visited = [[(False) for i in range(len(board[0]))] for j in range(len(board))]
def dfs(r, c, pos):
if pos == len(word):
return True
if (
0 <= r < len(board)
and 0 <= c < len(board[0])
and visited[r][c] == False
and board[r][c] == word[pos]
):
visited[r][c] = True
if (
dfs(r + 1, c, pos + 1)
or dfs(r, c + 1, pos + 1)
or dfs(r - 1, c, pos + 1)
or dfs(r, c - 1, pos + 1)
or dfs(r + 1, c + 1, pos + 1)
or dfs(r + 1, c - 1, pos + 1)
or dfs(r - 1, c + 1, pos + 1)
or dfs(r - 1, c - 1, pos + 1)
):
return True
visited[r][c] = False
return False
indx = 0
for i in range(len(board)):
for j in range(len(board[0])):
if board[i][j] == word[indx]:
if dfs(i, j, indx):
return True
return False
def wordBoggle(self, board, dictionary):
res = []
for i in range(len(dictionary)):
word = dictionary[i]
if self.find(board, word):
res.append(word)
return list(set(res)) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class TrieNode:
def __init__(self):
self.children = [None] * 26
self.isEndOfWord = False
class Solution:
def __init__(self):
self.root = TrieNode()
self.tempx = [0, 0, -1, 1, 1, -1, -1, 1]
self.tempy = [1, -1, 0, 0, 1, -1, 1, -1]
self.ans = set()
def wordBoggle(self, board, dictionary):
for word in dictionary:
self.insertTrie(self.root, word)
m = len(board)
n = len(board[0])
visited = [[(False) for _ in range(n)] for _ in range(m)]
for i in range(m):
for j in range(n):
char = board[i][j]
index = ord(char) - ord("A")
node = self.root.children[index]
if node:
visited[i][j] = True
self.dfs(node, visited, i, j, board, char)
visited[i][j] = False
return list(self.ans)
def dfs(self, node, visited, x, y, board, tempstr):
if node.isEndOfWord:
self.ans.add(tempstr)
for i in range(26):
if node.children[i]:
ch = chr(ord("A") + i)
for j in range(8):
newx = x + self.tempx[j]
newy = y + self.tempy[j]
if self.isValid(board, visited, newx, newy, ch):
visited[newx][newy] = True
self.dfs(
node.children[i], visited, newx, newy, board, tempstr + ch
)
visited[newx][newy] = False
def isValid(self, board, visited, x, y, ch):
row = len(board)
col = len(board[0])
if (
x >= 0
and x < row
and y >= 0
and y < col
and visited[x][y] == False
and ch == board[x][y]
):
return True
else:
return False
def insertTrie(self, root, word):
if len(word) == 0:
root.isEndOfWord = True
return
node = TrieNode()
index = ord(word[0]) - ord("A")
if root.children[index]:
node = root.children[index]
else:
root.children[index] = node
self.insertTrie(node, word[1:]) | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NONE NUMBER ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR 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 FUNC_DEF FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR 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 FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR IF VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR 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 ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class Solution:
def createTrie(self, words):
trie = {}
for word in words:
root = trie
for c in word:
if c not in root:
root[c] = {}
root = root[c]
root["#"] = "#"
return trie
def wordBoggle(self, board, dictionary):
trie = self.createTrie(dictionary)
self.res = set()
for row in range(len(board)):
for col in range(len(board[0])):
if board[row][col] in trie:
self.dfs(board, row, col, "", trie)
return list(self.res)
def inBounds(self, board, row, col):
if row < 0 or col < 0 or row == len(board) or col == len(board[0]):
return False
return True
def dfs(self, board, row, col, word, trie):
currChar = board[row][col]
newTrie = trie[currChar]
newWord = word + currChar
board[row][col] = ""
for ch in newTrie:
if ch == "#":
self.res.add(newWord)
else:
for x, y in [
(1, 0),
(-1, 0),
(0, 1),
(0, -1),
(1, 1),
(1, -1),
(-1, 1),
(-1, -1),
]:
if (
self.inBounds(board, row + x, col + y)
and ch == board[row + x][col + y]
):
self.dfs(board, row + x, col + y, newWord, newTrie)
board[row][col] = currChar | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR DICT ASSIGN VAR VAR VAR ASSIGN VAR STRING STRING RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL 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 EXPR FUNC_CALL VAR VAR VAR VAR STRING VAR RETURN FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR STRING FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class Solution:
def wordBoggle(self, board, dictionary):
def dfs(board, word, v, i, j, indx):
if indx == len(word):
return True
if (
i >= 0
and i < len(board)
and j >= 0
and j < len(board[0])
and v[i][j] == False
and board[i][j] == word[indx]
):
v[i][j] = True
if (
dfs(board, word, v, i - 1, j - 1, indx + 1)
or dfs(board, word, v, i - 1, j, indx + 1)
or dfs(board, word, v, i - 1, j + 1, indx + 1)
or dfs(board, word, v, i, j - 1, indx + 1)
or dfs(board, word, v, i, j + 1, indx + 1)
or dfs(board, word, v, i + 1, j - 1, indx + 1)
or dfs(board, word, v, i + 1, j, indx + 1)
or dfs(board, word, v, i + 1, j + 1, indx + 1)
):
return True
v[i][j] = False
return False
def exist(board, word):
r = len(board)
c = len(board[0])
v = [[(False) for i in range(c)] for j in range(r)]
for i in range(r):
for j in range(c):
if board[i][j] == word[0]:
if dfs(board, word, v, i, j, 0):
return True
return False
l = []
for i in dictionary:
if exist(board, i):
if i not in l:
l.append(i)
return l
if __name__ == "__main__":
t = int(input())
for _ in range(t):
N = int(input())
dictionary = [x for x in input().strip().split()]
line = input().strip().split()
R = int(line[0])
C = int(line[1])
board = []
for _ in range(R):
board.append([x for x in input().strip().split()])
obj = Solution()
found = obj.wordBoggle(board, dictionary)
if len(found) is 0:
print(-1)
continue
found.sort()
for i in found:
print(i, end=" ")
print() | CLASS_DEF FUNC_DEF 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 NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER 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 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 NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | def dfs(board, word, i, x, y, visited):
if i >= len(word):
return True
if (
x < 0
or x >= len(board)
or y < 0
or y >= len(board[0])
or visited[x][y]
or board[x][y] != word[i]
):
return False
visited[x][y] = True
a1 = dfs(board, word, i + 1, x, y + 1, visited)
a2 = dfs(board, word, i + 1, x, y - 1, visited)
a3 = dfs(board, word, i + 1, x + 1, y, visited)
a4 = dfs(board, word, i + 1, x - 1, y, visited)
a5 = dfs(board, word, i + 1, x + 1, y + 1, visited)
a6 = dfs(board, word, i + 1, x - 1, y + 1, visited)
a7 = dfs(board, word, i + 1, x + 1, y - 1, visited)
a8 = dfs(board, word, i + 1, x - 1, y - 1, visited)
visited[x][y] = False
if a1 or a2 or a3 or a4 or a5 or a6 or a7 or a8:
return True
return False
class Solution:
def wordBoggle(self, board, dictionary):
res = []
for word in dictionary:
visited = [([0] * len(board[0])) for i in range(len(board))]
flag = False
for i in range(len(board)):
for j in range(len(board[0])):
if board[i][j] == word[0]:
if dfs(board, word, 0, i, j, visited):
res.append(word)
flag = True
break
if flag:
break
res = set(res)
res = list(res)
return res | 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 VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER 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 VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class Solution:
def wordBoggle(self, board, dictionary):
m, n = len(board), len(board[0])
output = []
def possible(i, j, m, n):
return i >= 0 and j >= 0 and i < m and j < n
def solver(t, board, word, inx, m, n):
if inx == len(word):
if word not in output:
output.append(word)
return True
return False
i, j = t[0], t[1]
visited[i][j] = True
a = [-1, -1, 0, 1, 1, 1, 0, -1]
b = [0, -1, -1, -1, 0, 1, 1, 1]
for k in range(8):
r, c = i + a[k], j + b[k]
if (
possible(r, c, m, n)
and board[r][c] == word[inx]
and visited[r][c] == False
):
if solver([r, c], board, word, inx + 1, m, n):
return True
visited[i][j] = False
return False
for t in dictionary:
word = t
visited = [[(False) for i in range(n)] for j in range(m)]
for i in range(m):
for j in range(n):
if word[0] == board[i][j] and visited[i][j] == False:
solver([i, j], board, word, 1, m, n)
return output | CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FUNC_DEF RETURN VAR NUMBER VAR NUMBER VAR VAR VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR 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 FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR LIST VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR NUMBER VAR VAR RETURN VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class Node:
def __init__(self):
self.link = dict()
self.end = False
class Trie:
def __init__(self):
self.root = Node()
def insert(self, word):
tem = self.root
for i in word:
if i not in tem.link:
tem.link[i] = Node()
tem = tem.link[i]
tem.end = True
class Solution:
def wordBoggle(self, board, dictionary):
Root = Trie()
for word in dictionary:
Root.insert(word)
ans = set()
n = len(board)
m = len(board[0])
def dfs(i, j, n, m, s, ans, nod, vis):
vis.add((i, j))
s += board[i][j]
nod = nod.link[s[-1]]
if nod.end == True:
ans.add(s)
tem = [-1, 0, 1]
for p in tem:
for q in tem:
if not (p == 0 and q == 0):
ne = i + p
qe = j + q
if (
ne > -1
and qe > -1
and ne < n
and qe < m
and board[ne][qe] in nod.link
and (ne, qe) not in vis
):
dfs(ne, qe, n, m, s, ans, nod, vis)
vis.discard((i, j))
for i in range(n):
for j in range(m):
if board[i][j] in Root.root.link:
dfs(i, j, n, m, "", ans, Root.root, set())
return list(ans) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR VAR FOR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR STRING VAR VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class Solution:
def wordBoggle(self, board, dictionary):
m, n = len(board), len(board[0])
trie = {}
stop = "#"
for word in dictionary:
node = trie
for ch in word:
if ch not in node:
node[ch] = {}
node = node[ch]
node[stop] = True
def dfs(node, i, j, path):
if stop in node:
node.pop(stop, None)
res.append(path)
temp, board[i][j] = board[i][j], "$"
for nx, ny in [
(i + 1, j),
(i - 1, j),
(i, j + 1),
(i, j - 1),
(i + 1, j + 1),
(i - 1, j - 1),
(i + 1, j - 1),
(i - 1, j + 1),
]:
if 0 <= nx < m and 0 <= ny < n and board[nx][ny] in node:
dfs(node[board[nx][ny]], nx, ny, path + board[nx][ny])
board[i][j] = temp
res = []
for i in range(m):
for j in range(n):
if board[i][j] in trie:
node = trie
dfs(node[board[i][j]], i, j, board[i][j])
return res | CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR DICT ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR STRING FOR VAR VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR 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 ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class Solution:
def wordBoggle(self, board, dictionary):
res = []
for word in set(dictionary):
found = False
for r in range(len(board)):
if found:
break
for c in range(len(board[r])):
if has_word(board, r, c, word, 0):
found = True
break
if found:
res.append(word)
return res
def has_word(board, r, c, word, i):
if i == len(word):
return True
if r < 0 or c < 0 or r == len(board) or c == len(board[r]):
return False
if board[r][c] != word[i]:
return False
temp = board[r][c]
board[r][c] = ""
has_result = False
for nr, nc in [
(r + 1, c),
(r - 1, c),
(r, c + 1),
(r, c - 1),
(r + 1, c + 1),
(r + 1, c - 1),
(r - 1, c + 1),
(r - 1, c - 1),
]:
if has_word(board, nr, nc, word, i + 1):
has_result = True
break
board[r][c] = temp
return has_result | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR RETURN VAR 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 VAR RETURN NUMBER IF VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR STRING ASSIGN VAR NUMBER FOR VAR VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class Solution:
def wordBoggle(self, board, dictionary):
n = len(board)
m = len(board[0])
l = []
s = {}
for word in dictionary:
if word in s:
continue
s[word] = 1
k = len(word)
f = 0
for i in range(n):
for j in range(m):
if board[i][j] == word[0]:
def dfs(i, j, c):
if c == k:
return True
if i < 0 or j < 0 or i >= n or j >= m:
return False
if board[i][j] != word[c]:
return False
temp = board[i][j]
board[i][j] = "*"
g = (
dfs(i + 1, j, c + 1)
or dfs(i - 1, j, c + 1)
or dfs(i, j + 1, c + 1)
or dfs(i, j - 1, c + 1)
or dfs(i - 1, j + 1, c + 1)
or dfs(i + 1, j + 1, c + 1)
or dfs(i + 1, j - 1, c + 1)
or dfs(i - 1, j - 1, c + 1)
)
board[i][j] = temp
return g
if dfs(i, j, 0):
f = 1
break
if f == 1:
l.append(word)
break
return l | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class Trie:
def __init__(self):
self.root = Node()
def insert(self, st):
curr = self.root
for i in range(len(st)):
if curr.childs[ord(st[i]) - ord("A")] == None:
curr.childs[ord(st[i]) - ord("A")] = Node()
curr = curr.childs[ord(st[i]) - ord("A")]
curr.s = st
class Node:
def __init__(self):
self.childs = [None] * 26
self.s = ""
class Solution:
def wordBoggle(self, board, dictionary):
def dfs(board, i, j, root, ans):
if (
i < 0
or j < 0
or i >= len(board)
or j >= len(board[0])
or visited[i][j] == True
):
return
if root.childs[ord(board[i][j]) - ord("A")] == None:
return
child = root.childs[ord(board[i][j]) - ord("A")]
if child.s != None and child.s != "":
ans.add(child.s)
visited[i][j] = True
for m in range(len(move)):
dfs(board, i + move[m][0], j + move[m][1], child, ans)
visited[i][j] = False
move = [[0, 1], [1, 0], [0, -1], [-1, 0], [1, 1], [1, -1], [-1, 1], [-1, -1]]
visited = [[(False) for i in range(len(board[0]))] for j in range(len(board))]
t = Trie()
for s in dictionary:
t.insert(s)
ans = set()
root = t.root
for i in range(len(board)):
for j in range(len(board[0])):
dfs(board, i, j, root, ans)
ans = list(ans)
return ans
if __name__ == "__main__":
t = int(input())
for _ in range(t):
N = int(input())
dictionary = [x for x in input().strip().split()]
line = input().strip().split()
R = int(line[0])
C = int(line[1])
board = []
for _ in range(R):
board.append([x for x in input().strip().split()])
obj = Solution()
found = obj.wordBoggle(board, dictionary)
if len(found) is 0:
print(-1)
continue
found.sort()
for i in found:
print(i, end=" ")
print() | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NONE ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NONE NUMBER ASSIGN VAR STRING CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER RETURN IF VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR STRING NONE RETURN ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR STRING IF VAR NONE VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR VAR ASSIGN VAR 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 NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class Solution:
def bfs(self, i, j, k, n, m, l, board, visited, word):
if k == l:
return True
dx = [0, 0, 1, -1, 1, 1, -1, -1]
dy = [1, -1, 0, 0, -1, 1, 1, -1]
for ind in range(8):
x, y = i + dx[ind], j + dy[ind]
if x >= 0 and x < n and y >= 0 and y < m and board[x][y] == word[k]:
char = board[x][y]
board[x][y] = ""
if self.bfs(x, y, k + 1, n, m, l, board, visited, word):
board[x][y] = char
return True
board[x][y] = char
return False
def wordBoggle(self, board, dictionary):
res = []
n, m = len(board), len(board[0])
for word in dictionary:
l = len(word)
for i in range(n):
for j in range(m):
if board[i][j] == word[0]:
visited = [([0] * m) for i in range(n)]
board[i][j] = ""
if (
self.bfs(i, j, 1, n, m, l, board, visited, word)
and word not in res
):
res.append(word)
board[i][j] = word[0]
res.sort()
return res | CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER 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 VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR STRING IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR STRING IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR RETURN VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class TrieNode:
def __init__(self):
self.children = {}
self.isendWord = False
class Trie:
def __init__(self):
self.root = TrieNode()
def insert(self, word: str) -> None:
curr = self.root
for i in word:
if i not in curr.children:
curr.children[i] = TrieNode()
curr = curr.children[i]
curr.isendWord = True
class Solution:
def wordBoggle(self, board, words):
trie = Trie()
for word in words:
trie.insert(word)
rows = len(board)
cols = len(board[0])
res = set()
vis = set()
def dfs(i, j, node, word):
if (
i < 0
or j < 0
or i == rows
or j == cols
or (i, j) in vis
or board[i][j] not in node.children
):
return False
node = node.children[board[i][j]]
word += board[i][j]
vis.add((i, j))
if node.isendWord:
res.add(word)
dfs(i + 1, j, node, word)
dfs(i, j + 1, node, word)
dfs(i - 1, j, node, word)
dfs(i, j - 1, node, word)
dfs(i + 1, j + 1, node, word)
dfs(i + 1, j - 1, node, word)
dfs(i - 1, j + 1, node, word)
dfs(i - 1, j - 1, node, word)
vis.remove((i, j))
for i in range(rows):
for j in range(cols):
dfs(i, j, trie.root, "")
return list(res) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF VAR ASSIGN VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER NONE CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING RETURN FUNC_CALL VAR VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class Solution:
def wordBoggle(self, board, dictionary):
n, m = len(board), len(board[0])
answers = []
for word in dictionary:
for i in range(n):
for j in range(m):
if board[i][j] == word[0]:
visit = [([False] * m) for _ in range(n)]
visit[i][j] = True
if self.check(board, word[1:], visit, i, j):
answers.append(word)
visit[i][j] = False
return list(set(answers))
def check(self, board, word, visit, i, j):
if len(word) == 0:
return True
n, m = len(board), len(board[0])
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
for k in range(8):
_i = i + dx[k]
_j = j + dy[k]
if _i >= 0 and _i < n and _j >= 0 and _j < m:
if visit[_i][_j] == False and board[_i][_j] == word[0]:
visit[_i][_j] = True
if self.check(board, word[1:], visit, _i, _j):
return True
visit[_i][_j] = False
return False | CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR 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 FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class TrieNode:
def __init__(self):
self.children = {}
self.isWord = False
def addWord(self, word):
curr = self
for c in word:
if c not in curr.children:
curr.children[c] = TrieNode()
curr = curr.children[c]
curr.isWord = True
class Solution:
def wordBoggle(self, board, dictionary):
root = TrieNode()
for w in dictionary:
root.addWord(w)
rows, cols = len(board), len(board[0])
result, visit = set(), set()
def dfs(r, c, node, word):
if (
r < 0
or c < 0
or r == rows
or c == cols
or (r, c) in visit
or board[r][c] not in node.children
):
return
visit.add((r, c))
node = node.children[board[r][c]]
word += board[r][c]
if node.isWord:
result.add(word)
dfs(r - 1, c, node, word)
dfs(r + 1, c, node, word)
dfs(r, c - 1, node, word)
dfs(r, c + 1, node, word)
dfs(r + 1, c - 1, node, word)
dfs(r + 1, c + 1, node, word)
dfs(r - 1, c - 1, node, word)
dfs(r - 1, c + 1, node, word)
visit.remove((r, c))
for r in range(rows):
for c in range(cols):
dfs(r, c, root, "")
return list(result)
if __name__ == "__main__":
t = int(input())
for _ in range(t):
N = int(input())
dictionary = [x for x in input().strip().split()]
line = input().strip().split()
R = int(line[0])
C = int(line[1])
board = []
for _ in range(R):
board.append([x for x in input().strip().split()])
obj = Solution()
found = obj.wordBoggle(board, dictionary)
if len(found) is 0:
print(-1)
continue
found.sort()
for i in found:
print(i, end=" ")
print() | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING RETURN FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class Solution:
def wordBoggle(self, board, dictionary):
dir = [[1, 0], [0, 1], [-1, 0], [0, -1], [1, 1], [-1, 1], [1, -1], [-1, -1]]
res = []
def solve(r, c, word, i):
if i == len(word):
res.append(word)
return
for j, k in dir:
nr, nc = r + j, c + k
if (
nr < 0
or nc < 0
or nr >= len(board)
or nc >= len(board[0])
or board[nr][nc] != word[i]
):
continue
temp = board[r][c]
board[r][c] = -1
solve(nr, nc, word, i + 1)
board[r][c] = temp
for word in dictionary:
for i in range(len(board)):
for j in range(len(board[0])):
if word not in res:
if board[i][j] == word[0]:
solve(i, j, word, 1)
return list(set(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 ASSIGN VAR LIST FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR 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 EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | def backtracking(dictionary, board):
r, c = len(board), len(board[0])
res = set()
for word in dictionary:
for i in range(r):
state = False
for j in range(c):
if board[i][j] == word[0]:
vis = set()
vis.add((i, j))
text = board[i][j]
ind = 1
if helper(word, board, r, c, i, j, vis, text, ind):
res.add(word)
state = True
break
if state:
break
res = list(res)
return res
def helper(word, board, r, c, i, j, vis, text, ind):
if text == word:
return True
for ti, tj in zip([1, -1, 0, 0, 1, -1, -1, 1], [0, 0, 1, -1, 1, 1, -1, -1]):
ni, nj = ti + i, tj + j
if (
len(text) < len(word)
and (ni, nj) not in vis
and isValid(ni, nj, r, c, board, word, ind)
):
vis.add((ni, nj))
if helper(word, board, r, c, ni, nj, vis, text + board[ni][nj], ind + 1):
return True
vis.remove((ni, nj))
return False
def isValid(i, j, r, c, board, word, ind):
if i >= 0 and i < r and j >= 0 and j < c and board[i][j] == word[ind]:
return True
return False
class Solution:
def wordBoggle(self, board, dictionary):
return backtracking(dictionary, board) | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN NUMBER FOR VAR VAR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class Solution:
def wordBoggle(self, board, dicti):
def func(n, m, dicti, board, ind1, ind2, stri, ind, vis):
if ind == len(stri):
return True
if (
ind1 < 0
or ind2 < 0
or ind1 >= n
or ind2 >= m
or board[ind1][ind2] != stri[ind]
or vis[ind1][ind2] != 0
):
return False
vis[ind1][ind2] = 1
l1 = func(n, m, dicti, board, ind1 - 1, ind2, stri, ind + 1, vis)
l2 = func(n, m, dicti, board, ind1 + 1, ind2, stri, ind + 1, vis)
l3 = func(n, m, dicti, board, ind1, ind2 - 1, stri, ind + 1, vis)
l4 = func(n, m, dicti, board, ind1, ind2 + 1, stri, ind + 1, vis)
l5 = func(n, m, dicti, board, ind1 - 1, ind2 - 1, stri, ind + 1, vis)
l6 = func(n, m, dicti, board, ind1 - 1, ind2 + 1, stri, ind + 1, vis)
l7 = func(n, m, dicti, board, ind1 + 1, ind2 - 1, stri, ind + 1, vis)
l8 = func(n, m, dicti, board, ind1 + 1, ind2 + 1, stri, ind + 1, vis)
vis[ind1][ind2] = 0
return l1 or l2 or l3 or l4 or l5 or l6 or l7 or l8
dicti = list(set(dicti))
n = len(board)
m = len(board[0])
vis = [[(0) for i in range(m + 1)] for j in range(n)]
li = []
for i in dicti:
for j in range(n):
s2 = False
for k in range(m):
if board[j][k] == i[0]:
kk = func(n, m, dicti, board, j, k, i, 0, vis)
if kk:
li.append(i)
s2 = True
break
if s2:
s1 = True
break
return li | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER RETURN VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class Solution:
def wordBoggle(self, board, dictionary):
n = len(board)
m = len(board[0])
def solve(i, j, word, index, size):
count = 0
if i >= 0 and j >= 0 and i < n and j < m and word[index] == board[i][j]:
ch = board[i][j]
board[i][j] = "*"
index += 1
if index == size:
count = 1
else:
count = solve(i - 1, j - 1, word, index, size)
if count == 0:
count = solve(i - 1, j, word, index, size)
if count == 0:
count = solve(i - 1, j + 1, word, index, size)
if count == 0:
count = solve(i, j - 1, word, index, size)
if count == 0:
count = solve(i, j + 1, word, index, size)
if count == 0:
count = solve(i + 1, j - 1, word, index, size)
if count == 0:
count = solve(i + 1, j, word, index, size)
if count == 0:
count = solve(i + 1, j + 1, word, index, size)
board[i][j] = ch
return count
ans = []
s = set()
for i in range(n):
for j in range(m):
for word in dictionary:
if word not in s:
if solve(i, j, word, 0, len(word)) > 0:
s.add(word)
ans.append(word)
return ans
if __name__ == "__main__":
t = int(input())
for _ in range(t):
N = int(input())
dictionary = [x for x in input().strip().split()]
line = input().strip().split()
R = int(line[0])
C = int(line[1])
board = []
for _ in range(R):
board.append([x for x in input().strip().split()])
obj = Solution()
found = obj.wordBoggle(board, dictionary)
if len(found) is 0:
print(-1)
continue
found.sort()
for i in found:
print(i, end=" ")
print() | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class Solution:
def wordBoggle(self, board, dictionary):
def dfs(i, j, d):
if d == w:
return True
visited[i][j] = True
for k in range(8):
if (
0 <= i + a[k] < n
and 0 <= j + b[k] < m
and board[i + a[k]][j + b[k]] == word[d]
and not visited[i + a[k]][j + b[k]]
):
if dfs(i + a[k], j + b[k], d + 1):
return True
visited[i][j] = False
n = len(board)
m = len(board[0])
a = [0, 0, 1, -1, 1, 1, -1, -1]
b = [1, -1, 0, 0, 1, -1, 1, -1]
ans = []
for word in dictionary:
c = 0
w = len(word)
visited = [[(False) for i in range(m)] for j in range(n)]
if word in ans:
continue
for i in range(n):
for j in range(m):
if board[i][j] == word[0]:
if dfs(i, j, 1):
ans.append(word)
c = 1
break
if c == 1:
break
return ans | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF NUMBER BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER 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 VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF 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 EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER RETURN VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class Solution:
def wordBoggle(self, board, dictionary):
def dfs(i, j, k, word, vis):
vis[i][j] = True
if k == len(word):
return True
for x, y in directions:
if (
0 <= i + x < n
and 0 <= j + y < m
and board[i + x][j + y] == word[k]
and vis[i + x][j + y] == False
):
if dfs(i + x, j + y, k + 1, word, vis):
return True
vis[i][j] = False
return False
def find_word(word):
for i in range(n):
for j in range(m):
if board[i][j] == word[0]:
vis = [([False] * m) for _ in range(n)]
if dfs(i, j, 1, word, vis):
return True
return False
n = len(board)
m = len(board[0])
directions = [
[0, 1],
[1, 1],
[1, 0],
[1, -1],
[0, -1],
[-1, -1],
[-1, 0],
[-1, 1],
]
ans = []
for word in dictionary:
if find_word(word) and word not in ans:
ans.append(word)
return ans | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER FOR VAR VAR VAR IF NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER 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 VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class Solution:
def search(self, board, word, index, i, j):
n = len(board)
m = len(board[0])
if i < 0 or i > n - 1 or j < 0 or j > m - 1:
return False
if word[index] != board[i][j]:
return False
if index == len(word) - 1:
return True
ch = board[i][j]
board[i][j] = "#"
dx = [-1, 0, +1, -1, +1, -1, 0, +1]
dy = [+1, +1, +1, 0, 0, -1, -1, -1]
for x in range(len(dx)):
if self.search(board, word, index + 1, i + dx[x], j + dy[x]):
board[i][j] = ch
return True
board[i][j] = ch
return False
def exist(self, board, word):
for i in range(len(board)):
for j in range(len(board[0])):
if self.search(board, word, 0, i, j):
return True
return False
def wordBoggle(self, board, dictionary):
wordslist = set()
for i in range(len(dictionary)):
if self.exist(board, dictionary[i]):
wordslist.add(dictionary[i])
return list(wordslist) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR VAR RETURN NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR STRING 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 FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR RETURN NUMBER FUNC_DEF 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 NUMBER VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class Trie:
def __init__(self):
self.child = {}
self.e = 0
def add(self, word):
cur = self
for w in word:
if w not in cur.child:
cur.child[w] = Trie()
cur = cur.child[w]
cur.e = 1
class Solution:
def wordBoggle(self, board, dic):
root = Trie()
for w in dic:
root.add(w)
vis = set()
ans = set()
m, n = len(board), len(board[0])
def dfs(i, j, cur, p):
if cur.e == 1:
ans.add(p)
if cur.child == {}:
return
vis.add((i, j))
for di, dj in [
(1, 0),
(0, 1),
(-1, 0),
(0, -1),
(1, -1),
(-1, 1),
(-1, -1),
(1, 1),
]:
x1, y1 = i + di, j + dj
if (
0 <= x1 < m
and 0 <= y1 < n
and (x1, y1) not in vis
and board[x1][y1] in cur.child
):
dfs(x1, y1, cur.child[board[x1][y1]], p + board[x1][y1])
vis.remove((i, j))
for i in range(m):
for j in range(n):
if board[i][j] in root.child:
dfs(i, j, root.child[board[i][j]], board[i][j])
return list(ans) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR DICT RETURN EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class Solution:
def wordBoggle(self, board, dictionary):
n = len(board)
m = len(board[0])
def dfs(board, s, i, j, idx):
if i < 0 or i >= n or j < 0 or j >= m:
return False
if s[idx] != board[i][j]:
return False
if idx == len(s) - 1:
return True
temp = board[i][j]
board[i][j] = "*"
a = dfs(board, s, i, j + 1, idx + 1)
b = dfs(board, s, i, j - 1, idx + 1)
c = dfs(board, s, i + 1, j, idx + 1)
d = dfs(board, s, i - 1, j, idx + 1)
e = dfs(board, s, i + 1, j + 1, idx + 1)
f = dfs(board, s, i - 1, j + 1, idx + 1)
g = dfs(board, s, i + 1, j - 1, idx + 1)
h = dfs(board, s, i - 1, j - 1, idx + 1)
board[i][j] = temp
return a or b or c or e or f or g or h or d
store = set()
for i in range(n):
for j in range(m):
for word in dictionary:
if dfs(board, word, i, j, 0):
store.add(word)
return list(store) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR RETURN NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class Node:
def __init__(self):
self.chi = {}
self.isla = False
class trie:
def __init__(self):
self.root = Node()
def add(self, word):
t = self.root
for val in word:
if t.chi.get(val) is None:
t.chi[val] = Node()
t = t.chi[val]
t.isla = True
class Solution:
def wordBoggle(self, board, dictionary):
n = len(board)
m = len(board[0])
def isval(x, y, vis):
if x < 0 or y < 0 or x >= n or y >= m:
return False
return (x, y) not in vis
dx = [1, 1, 1, -1, -1, -1, 0, 0]
dy = [1, -1, 0, 1, -1, 0, 1, -1]
def dfs(x, y, t, s, vis):
vis.add((x, y))
if t.isla:
ans.add(s)
for i in range(8):
if isval(x + dx[i], y + dy[i], vis) and t.chi.get(
board[x + dx[i]][y + dy[i]]
):
dfs(
x + dx[i],
y + dy[i],
t.chi[board[x + dx[i]][y + dy[i]]],
s + board[x + dx[i]][y + dy[i]],
vis,
)
vis.remove((x, y))
gr = trie()
for val in dictionary:
gr.add(val)
ans = set()
vis = set()
for i in range(n):
for j in range(m):
if gr.root.chi.get(board[i][j]) is not None:
dfs(i, j, gr.root.chi[board[i][j]], board[i][j], vis)
return list(ans) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR RETURN NUMBER RETURN VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NONE EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class Solution:
def dfs(self, board, m, n, word, coords, curind, visited):
if curind == len(word):
return True
visited.add(coords)
i, j = coords
if (
i - 1 >= 0
and j - 1 >= 0
and board[i - 1][j - 1] == word[curind]
and (i - 1, j - 1) not in visited
):
if self.dfs(board, m, n, word, (i - 1, j - 1), curind + 1, visited):
return True
if i - 1 >= 0 and board[i - 1][j] == word[curind] and (i - 1, j) not in visited:
if self.dfs(board, m, n, word, (i - 1, j), curind + 1, visited):
return True
if (
i - 1 >= 0
and j + 1 < n
and board[i - 1][j + 1] == word[curind]
and (i - 1, j + 1) not in visited
):
if self.dfs(board, m, n, word, (i - 1, j + 1), curind + 1, visited):
return True
if j - 1 >= 0 and board[i][j - 1] == word[curind] and (i, j - 1) not in visited:
if self.dfs(board, m, n, word, (i, j - 1), curind + 1, visited):
return True
if j + 1 < n and board[i][j + 1] == word[curind] and (i, j + 1) not in visited:
if self.dfs(board, m, n, word, (i, j + 1), curind + 1, visited):
return True
if (
i + 1 < m
and j - 1 >= 0
and board[i + 1][j - 1] == word[curind]
and (i + 1, j - 1) not in visited
):
if self.dfs(board, m, n, word, (i + 1, j - 1), curind + 1, visited):
return True
if i + 1 < m and board[i + 1][j] == word[curind] and (i + 1, j) not in visited:
if self.dfs(board, m, n, word, (i + 1, j), curind + 1, visited):
return True
if (
i + 1 < m
and j + 1 < n
and board[i + 1][j + 1] == word[curind]
and (i + 1, j + 1) not in visited
):
if self.dfs(board, m, n, word, (i + 1, j + 1), curind + 1, visited):
return True
visited.remove(coords)
return False
def wordBoggle(self, board, dictionary):
startingletters = {}
m = len(board)
n = len(board[0])
out = []
for i in range(m):
for j in range(n):
if board[i][j] not in startingletters:
startingletters[board[i][j]] = [(i, j)]
else:
startingletters[board[i][j]].append((i, j))
for word in dictionary:
if word[0] in startingletters:
for position in startingletters[word[0]]:
visited = set()
if self.dfs(board, m, n, word, position, 1, visited):
out.append(word)
break
out = list(set(out))
out.sort()
return out | CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR DICT 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 ASSIGN VAR VAR VAR VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR FOR VAR VAR IF VAR NUMBER VAR FOR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class Solution:
def wordBoggle(self, board, dictionary):
ROWS = len(board)
COLS = len(board[0])
def dfs(board, i, j, word, ind, visited):
if ind == len(word):
return True
if i < 0 or j < 0 or i >= ROWS or j >= COLS or visited[i][j]:
return False
visited[i][j] = True
if word[ind] == board[i][j]:
dx = [1, -1, 0, 0, 1, 1, -1, -1]
dy = [0, 0, 1, -1, -1, 1, 1, -1]
for k in range(8):
x = i + dx[k]
y = j + dy[k]
if dfs(board, x, y, word, ind + 1, visited):
return True
visited[i][j] = False
return False
def checker(word, board, visited):
start = word[0]
for i in range(ROWS):
for j in range(COLS):
if board[i][j] == start:
if dfs(board, i, j, word, 0, visited):
return True
return False
found = set()
for word in dictionary:
visited = []
for i in range(ROWS):
visited.append([False] * COLS)
if checker(word, board, visited):
found.add(word)
return list(found) | 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 RETURN NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR 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 IF FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class Solution:
def wordBoggle(self, board, dictionary):
N = len(board)
M = len(board[0])
visited = [[(0) for _ in range(M)] for _ in range(N)]
res = []
words = set()
for k in range(len(dictionary)):
for i in range(N):
for j in range(M):
if board[i][j] == dictionary[k][0]:
flag = self.DFS(board, dictionary[k], visited, N, M, i, j, 0)
if flag == 1:
words.add(dictionary[k])
break
words = list(words)
return words
def DFS(self, board, word, visited, n, m, x, y, idx):
if idx == len(word):
return 1
if x >= n or x < 0 or y >= m or y < 0:
return 0
if visited[x][y] == 1:
return 0
if board[x][y] != word[idx]:
return 0
visited[x][y] = 1
flag = 0
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
for i in range(8):
xx = x + dx[i]
yy = y + dy[i]
flag |= self.DFS(board, word, visited, n, m, xx, yy, idx + 1)
visited[x][y] = 0
return flag | 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 ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER 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 VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | def dfs(board, r, c, word, wi, tu, visited):
a, b = tu
if wi == len(word) - 1:
return True
visited[a][b] = True
groups = [(-1, -1), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 1), (1, 0), (-1, 0)]
for g in groups:
i, j = g
if (
a + i >= 0
and a + i < r
and b + j >= 0
and b + j < c
and not visited[a + i][b + j]
and word[wi + 1] == board[a + i][b + j]
and dfs(board, r, c, word, wi + 1, (a + i, b + j), visited)
):
return True
visited[a][b] = False
return False
def util(board, word):
r, c = len(board), len(board[0])
for i in range(r):
for j in range(c):
if board[i][j] == word[0]:
visited = [([False] * c) for i in range(r)]
if dfs(board, r, c, word, 0, (i, j), visited):
return True
return False
class Solution:
def wordBoggle(self, board, dictionary):
ans = []
for word in dictionary:
if util(board, word):
ans.append(word)
return list(set(ans)) | FUNC_DEF ASSIGN VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR VAR 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 BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR VAR RETURN NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class TrieNode:
def __init__(self):
self.child = {}
self.last = False
def insert(self, word):
curr = self
for i in word:
if i not in curr.child:
curr.child[i] = TrieNode()
curr = curr.child[i]
curr.last = True
class Solution:
def wordBoggle(self, board, dictionary):
root = TrieNode()
for i in dictionary:
root.insert(i)
def dfs(i, j, node, curr, res):
if node.last:
res.append(curr)
node.last = False
if i < 0 or i >= r or j < 0 or j >= c or board[i][j] not in node.child:
return
tmp = board[i][j]
board[i][j] = "#"
for a, b in [
(1, 0),
(-1, 0),
(0, 1),
(0, -1),
(-1, -1),
(1, -1),
(-1, 1),
(1, 1),
]:
x = i + a
y = j + b
dfs(x, y, node.child[tmp], curr + tmp, res)
board[i][j] = tmp
res = []
r, c = len(board), len(board[0])
for i in range(r):
for j in range(c):
dfs(i, j, root, "", res)
return res | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR RETURN ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR STRING FOR VAR VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR 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 EXPR FUNC_CALL VAR VAR VAR VAR STRING VAR RETURN VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class TrieNode:
def __init__(self):
self.children = {}
self.end = False
class Solution:
def __init__(self):
self.root = TrieNode()
def insert(self, word, root):
cur = root
for c in word:
if c not in cur.children:
cur.children[c] = TrieNode()
cur = cur.children[c]
cur.end = True
def dfs(self, i, j, board, cur, visited, word, ans):
word += board[i][j]
if cur.end:
if word not in ans:
ans.append(word)
x = [
[i + 1, j],
[i + 1, j + 1],
[i, j + 1],
[i - 1, j],
[i, j - 1],
[i - 1, j - 1],
[i + 1, j - 1],
[i - 1, j + 1],
]
for k in range(8):
a = x[k][0]
b = x[k][1]
if a >= 0 and a < len(board) and b >= 0 and b < len(board[0]):
c = board[a][b]
if visited[a][b] == 0 and c in cur.children:
visited[a][b] = 1
self.dfs(a, b, board, cur.children[c], visited, word, ans)
visited[a][b] = 0
return
def wordBoggle(self, board, dictionary):
cur = self.root
for word in dictionary:
self.insert(word, cur)
word = ""
visited = [[(0) for i in range(len(board[0]))] for j in range(len(board))]
ans = []
for i in range(len(board)):
for j in range(len(board[0])):
if visited[i][j] == 0 and board[i][j] in cur.children:
visited[i][j] = 1
self.dfs(i, j, board, cur.children[board[i][j]], visited, word, ans)
visited[i][j] = 0
word = ""
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FUNC_DEF VAR VAR VAR VAR IF VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST LIST BIN_OP VAR NUMBER VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER LIST VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER VAR LIST VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN FUNC_DEF ASSIGN VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR 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 NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR STRING RETURN VAR |
Given a dictionary of words and an M x N board where every cell has one character. Find all possible different words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You don’t need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board (NOTE: All words returned must be different even it occurs multiple times in the dictionary).
Expected Time Complexity: O(4^(N^2))
Expected Auxiliary Space: O(N^2)
Constraints:
1 ≤ N ≤ 15
1 ≤ R, C ≤ 50
1 ≤ length of Word ≤ 60
All words of Dictionary and all characters of board are in uppercase letters from 'A' to 'Z' | class Solution:
dirs = [[1, 1], [1, 0], [1, -1], [0, -1], [-1, -1], [-1, 0], [-1, 1], [0, 1]]
def on_board(self, i, j, board):
return 0 <= i < len(board) and 0 <= j < len(board[0])
def removeFromDict(self, dictionary, i, ch):
new_dict = []
for w in dictionary:
if w[i] != ch:
continue
elif len(w) - 1 == i:
self.ans.add(w)
else:
new_dict.append(w)
return new_dict
def dfs(self, i, j, dictionary, index):
if len(dictionary) == 0:
return
for d in self.dirs:
ni = i + d[0]
nj = j + d[1]
if self.on_board(ni, nj, self.board) and not self.visited[ni][nj]:
self.visited[ni][nj] = 1
self.dfs(
ni,
nj,
self.removeFromDict(dictionary, index, self.board[ni][nj]),
index + 1,
)
self.visited[ni][nj] = 0
def wordBoggle(self, board, dictionary):
self.board = board
self.visited = [[(0) for j in range(len(board[0]))] for i in range(len(board))]
self.ans = set()
for i in range(len(board)):
for j in range(len(board[0])):
self.visited[i][j] = 1
ans = []
self.dfs(i, j, self.removeFromDict(dictionary, 0, board[i][j]), 1)
self.visited[i][j] = 0
return list(self.ans) | CLASS_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 FUNC_DEF RETURN NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.