description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look. [Image] Polina came up with a new game called "Hyperset". In her game, there are $n$ cards with $k$ features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with $k = 4$. Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set. Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table. -----Input----- The first line of each test contains two integers $n$ and $k$ ($1 \le n \le 1500$, $1 \le k \le 30$) — number of cards and number of features. Each of the following $n$ lines contains a card description: a string consisting of $k$ letters "S", "E", "T". The $i$-th character of this string decribes the $i$-th feature of that card. All cards are distinct. -----Output----- Output a single integer — the number of ways to choose three cards that form a set. -----Examples----- Input 3 3 SET ETS TSE Output 1 Input 3 4 SETE ETSE TSES Output 0 Input 5 4 SETT TEST EEET ESTE STES Output 2 -----Note----- In the third example test, these two triples of cards are sets: "SETT", "TEST", "EEET" "TEST", "ESTE", "STES"
a = input().split() n = int(a[0]) k = int(a[1]) l = [] t = 0 for i in range(n): a = input() l.append(a) X = set(l) for i in range(0, n - 1): for j in range(i + 1, n): r = "" for u in range(k): if l[i][u] == l[j][u]: r = r + l[i][u] else: r = r + chr( ord("S") + ord("T") + ord("E") - ord(l[i][u]) - ord(l[j][u]) ) if r in X: t += 1 print(int(t / 3))
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER
Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look. [Image] Polina came up with a new game called "Hyperset". In her game, there are $n$ cards with $k$ features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with $k = 4$. Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set. Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table. -----Input----- The first line of each test contains two integers $n$ and $k$ ($1 \le n \le 1500$, $1 \le k \le 30$) — number of cards and number of features. Each of the following $n$ lines contains a card description: a string consisting of $k$ letters "S", "E", "T". The $i$-th character of this string decribes the $i$-th feature of that card. All cards are distinct. -----Output----- Output a single integer — the number of ways to choose three cards that form a set. -----Examples----- Input 3 3 SET ETS TSE Output 1 Input 3 4 SETE ETSE TSES Output 0 Input 5 4 SETT TEST EEET ESTE STES Output 2 -----Note----- In the third example test, these two triples of cards are sets: "SETT", "TEST", "EEET" "TEST", "ESTE", "STES"
def binarySearch(words, low, high, key): if high < low: return -1 mid = (low + high) // 2 if words[mid] < key: return binarySearch(words, mid + 1, high, key) elif words[mid] > key: return binarySearch(words, low, mid - 1, key) else: return mid a = input().split() n = int(a[0]) k = int(a[1]) l = [] t = 0 for i in range(n): a = input() l.append(a) l.sort() for i in range(0, n - 1): for j in range(i + 1, n): r = "" for u in range(k): if l[i][u] == l[j][u]: r = r + l[i][u] else: r = r + chr( ord("S") + ord("T") + ord("E") - ord(l[i][u]) - ord(l[j][u]) ) if binarySearch(l, 0, n - 1, r) != -1: t += 1 print(int(t / 3))
FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER
Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look. [Image] Polina came up with a new game called "Hyperset". In her game, there are $n$ cards with $k$ features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with $k = 4$. Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set. Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table. -----Input----- The first line of each test contains two integers $n$ and $k$ ($1 \le n \le 1500$, $1 \le k \le 30$) — number of cards and number of features. Each of the following $n$ lines contains a card description: a string consisting of $k$ letters "S", "E", "T". The $i$-th character of this string decribes the $i$-th feature of that card. All cards are distinct. -----Output----- Output a single integer — the number of ways to choose three cards that form a set. -----Examples----- Input 3 3 SET ETS TSE Output 1 Input 3 4 SETE ETSE TSES Output 0 Input 5 4 SETT TEST EEET ESTE STES Output 2 -----Note----- In the third example test, these two triples of cards are sets: "SETT", "TEST", "EEET" "TEST", "ESTE", "STES"
import sys def main(): s = list() n, k = (int(e) for e in sys.stdin.readline().strip().split()) q = set() for i in range(n): s.append(sys.stdin.readline().strip()) q.add(s[i]) m = ord("S") + ord("E") + ord("T") r = 0 for i in range(n): for j in range(i + 1, n): t = list() b = 1 for e in range(k): if s[i][e] == s[j][e]: t.append(s[i][e]) else: t.append(chr(m - ord(s[i][e]) - ord(s[j][e]))) if "".join(t) in q: r += 1 print(r // 3) main()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL STRING VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look. [Image] Polina came up with a new game called "Hyperset". In her game, there are $n$ cards with $k$ features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with $k = 4$. Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set. Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table. -----Input----- The first line of each test contains two integers $n$ and $k$ ($1 \le n \le 1500$, $1 \le k \le 30$) — number of cards and number of features. Each of the following $n$ lines contains a card description: a string consisting of $k$ letters "S", "E", "T". The $i$-th character of this string decribes the $i$-th feature of that card. All cards are distinct. -----Output----- Output a single integer — the number of ways to choose three cards that form a set. -----Examples----- Input 3 3 SET ETS TSE Output 1 Input 3 4 SETE ETSE TSES Output 0 Input 5 4 SETT TEST EEET ESTE STES Output 2 -----Note----- In the third example test, these two triples of cards are sets: "SETT", "TEST", "EEET" "TEST", "ESTE", "STES"
mp = {} e = [1] S = ord("S") + ord("T") + ord("E") n, k = map(int, input().split()) ans = 0 for i in range(1, n + 1): e.append(input()) if e[i] in mp: mp[e[i]] += 1 else: mp[e[i]] = 1 for i in range(1, n + 1): for j in range(i + 1, n + 1): if i == j: continue T = "" for x in range(k): if e[i][x] == e[j][x]: T += e[i][x] else: T += chr(S - ord(e[i][x]) - ord(e[j][x])) if T in mp: ans += mp[T] else: ans += 0 print(int(ans / 3))
ASSIGN VAR DICT ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER
Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look. [Image] Polina came up with a new game called "Hyperset". In her game, there are $n$ cards with $k$ features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with $k = 4$. Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set. Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table. -----Input----- The first line of each test contains two integers $n$ and $k$ ($1 \le n \le 1500$, $1 \le k \le 30$) — number of cards and number of features. Each of the following $n$ lines contains a card description: a string consisting of $k$ letters "S", "E", "T". The $i$-th character of this string decribes the $i$-th feature of that card. All cards are distinct. -----Output----- Output a single integer — the number of ways to choose three cards that form a set. -----Examples----- Input 3 3 SET ETS TSE Output 1 Input 3 4 SETE ETSE TSES Output 0 Input 5 4 SETT TEST EEET ESTE STES Output 2 -----Note----- In the third example test, these two triples of cards are sets: "SETT", "TEST", "EEET" "TEST", "ESTE", "STES"
n, k = map(int, input().split()) array = [input() for i in range(n)] set_array = set(array) for el in array: new = "" for x in el: new += x set_array.add(new) count = 0 def is_vzm(x, y): if x == y: return x for el in ["S", "E", "T"]: if el != x and el != y: return el visited = set() for i in range(n): for j in range(i + 1, n): if (array[i], array[j]) in visited or (array[j], array[i]) in visited: continue vzm = "" for m in range(k): vzm += is_vzm(array[i][m], array[j][m]) if vzm not in set_array: break if vzm in set_array: visited.add((array[j], vzm)) visited.add((vzm, array[j])) visited.add((array[i], vzm)) count += 1 print(count)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR STRING FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR RETURN VAR FOR VAR LIST STRING STRING STRING IF VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look. [Image] Polina came up with a new game called "Hyperset". In her game, there are $n$ cards with $k$ features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with $k = 4$. Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set. Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table. -----Input----- The first line of each test contains two integers $n$ and $k$ ($1 \le n \le 1500$, $1 \le k \le 30$) — number of cards and number of features. Each of the following $n$ lines contains a card description: a string consisting of $k$ letters "S", "E", "T". The $i$-th character of this string decribes the $i$-th feature of that card. All cards are distinct. -----Output----- Output a single integer — the number of ways to choose three cards that form a set. -----Examples----- Input 3 3 SET ETS TSE Output 1 Input 3 4 SETE ETSE TSES Output 0 Input 5 4 SETT TEST EEET ESTE STES Output 2 -----Note----- In the third example test, these two triples of cards are sets: "SETT", "TEST", "EEET" "TEST", "ESTE", "STES"
ltr_xor = ord("S") ^ ord("E") ^ ord("T") def get_matching_card(c1: str, c2: str) -> str: c3 = [] n = len(c1) for i in range(n): if c1[i] == c2[i]: c3.append(c1[i]) else: c3.append(chr(ltr_xor ^ ord(c1[i]) ^ ord(c2[i]))) return "".join(c3) n, k = map(int, input().split(" ")) cards = [] card_set = set() ways = 0 for i in range(n): nc = input() cards.append(nc) card_set.add(nc) for i in range(n - 1): for j in range(i + 1, n): if get_matching_card(cards[i], cards[j]) in card_set: ways += 1 print(int(ways / 3))
ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL STRING VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER
Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look. [Image] Polina came up with a new game called "Hyperset". In her game, there are $n$ cards with $k$ features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with $k = 4$. Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set. Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table. -----Input----- The first line of each test contains two integers $n$ and $k$ ($1 \le n \le 1500$, $1 \le k \le 30$) — number of cards and number of features. Each of the following $n$ lines contains a card description: a string consisting of $k$ letters "S", "E", "T". The $i$-th character of this string decribes the $i$-th feature of that card. All cards are distinct. -----Output----- Output a single integer — the number of ways to choose three cards that form a set. -----Examples----- Input 3 3 SET ETS TSE Output 1 Input 3 4 SETE ETSE TSES Output 0 Input 5 4 SETT TEST EEET ESTE STES Output 2 -----Note----- In the third example test, these two triples of cards are sets: "SETT", "TEST", "EEET" "TEST", "ESTE", "STES"
n, k = input().split(" ") n = int(n) k = int(k) cards = dict() orderLatter = {"S": 1, "E": 2, "T": 3} for i in range(n): card = input() card = tuple(orderLatter[card[i]] for i in range(k)) cards[card] = i def getThirdNumber(a, b): if a == b: return a return 6 - a - b def getThird(c1, c2): return tuple(getThirdNumber(c1[i], c2[i]) for i in range(k)) sum = 0 for c1 in cards.keys(): for c2 in cards.keys(): if cards[c1] >= cards[c2]: continue c3 = getThird(c1, c2) if c3 in cards.keys() and cards[c3] > cards[c2]: sum += 1 print(sum)
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT STRING STRING STRING NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_DEF IF VAR VAR RETURN VAR RETURN BIN_OP BIN_OP NUMBER VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look. [Image] Polina came up with a new game called "Hyperset". In her game, there are $n$ cards with $k$ features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with $k = 4$. Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set. Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table. -----Input----- The first line of each test contains two integers $n$ and $k$ ($1 \le n \le 1500$, $1 \le k \le 30$) — number of cards and number of features. Each of the following $n$ lines contains a card description: a string consisting of $k$ letters "S", "E", "T". The $i$-th character of this string decribes the $i$-th feature of that card. All cards are distinct. -----Output----- Output a single integer — the number of ways to choose three cards that form a set. -----Examples----- Input 3 3 SET ETS TSE Output 1 Input 3 4 SETE ETSE TSES Output 0 Input 5 4 SETT TEST EEET ESTE STES Output 2 -----Note----- In the third example test, these two triples of cards are sets: "SETT", "TEST", "EEET" "TEST", "ESTE", "STES"
def calc(a, b): ans = "" for i in range(k): if a[i] == b[i]: ans += a[i] elif a[i] != "T" and b[i] != "T": ans += "T" elif a[i] != "S" and b[i] != "S": ans += "S" else: ans += "E" return ans n, k = map(int, input().split()) d = {} a = [] for i in range(n): s = input() a.append(s) d[s] = 1 count = 0 for i in range(n): for j in range(i + 1, n): if calc(a[i], a[j]) in d: count += 1 print(count // 3)
FUNC_DEF ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR IF VAR VAR STRING VAR VAR STRING VAR STRING IF VAR VAR STRING VAR VAR STRING VAR STRING VAR STRING RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look. [Image] Polina came up with a new game called "Hyperset". In her game, there are $n$ cards with $k$ features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with $k = 4$. Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set. Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table. -----Input----- The first line of each test contains two integers $n$ and $k$ ($1 \le n \le 1500$, $1 \le k \le 30$) — number of cards and number of features. Each of the following $n$ lines contains a card description: a string consisting of $k$ letters "S", "E", "T". The $i$-th character of this string decribes the $i$-th feature of that card. All cards are distinct. -----Output----- Output a single integer — the number of ways to choose three cards that form a set. -----Examples----- Input 3 3 SET ETS TSE Output 1 Input 3 4 SETE ETSE TSES Output 0 Input 5 4 SETT TEST EEET ESTE STES Output 2 -----Note----- In the third example test, these two triples of cards are sets: "SETT", "TEST", "EEET" "TEST", "ESTE", "STES"
import sys input = sys.stdin.readline n, k = map(int, input().split()) l = [] for i in range(k): o = [] for j in range(3): o.append(0) l.append(o) d = set() da = [] for i in range(n): s = input() h = [] for i in range(k): h.append(s[i]) da.append(h) b = tuple(h) d.add(b) i = 0 ans = 0 while i < n: j = i + 1 while j < n: p = ["S"] * k c = k - 1 while c > -1: if da[i][c] == da[j][c]: p[c] = da[i][c] else: q = {"S", "E", "T"} q.remove(da[i][c]) q.remove(da[j][c]) for r in q: p[c] = r c -= 1 w = j + 1 u = tuple(p) if u in d: ans += 1 j = j + 1 i = i + 1 sys.stdout.write(str(ans // 3) + "\n")
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR STRING STRING STRING EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING
Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look. [Image] Polina came up with a new game called "Hyperset". In her game, there are $n$ cards with $k$ features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with $k = 4$. Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set. Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table. -----Input----- The first line of each test contains two integers $n$ and $k$ ($1 \le n \le 1500$, $1 \le k \le 30$) — number of cards and number of features. Each of the following $n$ lines contains a card description: a string consisting of $k$ letters "S", "E", "T". The $i$-th character of this string decribes the $i$-th feature of that card. All cards are distinct. -----Output----- Output a single integer — the number of ways to choose three cards that form a set. -----Examples----- Input 3 3 SET ETS TSE Output 1 Input 3 4 SETE ETSE TSES Output 0 Input 5 4 SETT TEST EEET ESTE STES Output 2 -----Note----- In the third example test, these two triples of cards are sets: "SETT", "TEST", "EEET" "TEST", "ESTE", "STES"
def get_complement(s, t): k = len(s) c = list() for i in range(k): if s[i] == t[i]: c.append(s[i]) elif s[i] == "S" and t[i] == "E" or s[i] == "E" and t[i] == "S": c.append("T") elif s[i] == "S" and t[i] == "T" or s[i] == "T" and t[i] == "S": c.append("E") else: c.append("S") return "".join(c) n, k = map(int, input().split()) cards = list() cards_set = set() for _ in range(n): s = input() cards.append(s) cards_set.add(s) counter = 0 for i in range(len(cards)): for j in range(i + 1, len(cards)): c = get_complement(cards[i], cards[j]) if c in cards_set: counter += 1 print(counter // 3)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR STRING IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING RETURN FUNC_CALL STRING VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look. [Image] Polina came up with a new game called "Hyperset". In her game, there are $n$ cards with $k$ features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with $k = 4$. Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set. Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table. -----Input----- The first line of each test contains two integers $n$ and $k$ ($1 \le n \le 1500$, $1 \le k \le 30$) — number of cards and number of features. Each of the following $n$ lines contains a card description: a string consisting of $k$ letters "S", "E", "T". The $i$-th character of this string decribes the $i$-th feature of that card. All cards are distinct. -----Output----- Output a single integer — the number of ways to choose three cards that form a set. -----Examples----- Input 3 3 SET ETS TSE Output 1 Input 3 4 SETE ETSE TSES Output 0 Input 5 4 SETT TEST EEET ESTE STES Output 2 -----Note----- In the third example test, these two triples of cards are sets: "SETT", "TEST", "EEET" "TEST", "ESTE", "STES"
n, m = map(int, input().split()) l = [] s = set() for i in range(n): l.append(input()) s.add(l[i]) l1 = set() s1 = set() ans = 0 for i in range(n): for j in range(i + 1, n): st = "" for k in range(m): li = ["S", "E", "T"] if l[i][k] == l[j][k]: st += l[i][k] else: li.remove(l[i][k]) li.remove(l[j][k]) st += li[0] p = [l[i], l[j], st] p.sort() p = tuple(p) if p not in l1 and st in s: ans += 1 l1.add(p) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST STRING STRING STRING IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look. [Image] Polina came up with a new game called "Hyperset". In her game, there are $n$ cards with $k$ features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with $k = 4$. Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set. Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table. -----Input----- The first line of each test contains two integers $n$ and $k$ ($1 \le n \le 1500$, $1 \le k \le 30$) — number of cards and number of features. Each of the following $n$ lines contains a card description: a string consisting of $k$ letters "S", "E", "T". The $i$-th character of this string decribes the $i$-th feature of that card. All cards are distinct. -----Output----- Output a single integer — the number of ways to choose three cards that form a set. -----Examples----- Input 3 3 SET ETS TSE Output 1 Input 3 4 SETE ETSE TSES Output 0 Input 5 4 SETT TEST EEET ESTE STES Output 2 -----Note----- In the third example test, these two triples of cards are sets: "SETT", "TEST", "EEET" "TEST", "ESTE", "STES"
def solve(a): b = "SET" for i in b: if i not in a: return i n, k = map(int, input().split()) b = [] a = set() for i in range(n): b.append(input().rstrip()) a.add(b[-1]) ans = 0 for i in range(n): for j in range(i + 1, n): z = [] for l in range(k): if b[i][l] == b[j][l]: z.append(b[i][l]) else: z.append(solve(b[i][l] + b[j][l])) if "".join(z) in a: ans += 1 print(ans // 3)
FUNC_DEF ASSIGN VAR STRING FOR VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR IF FUNC_CALL STRING VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look. [Image] Polina came up with a new game called "Hyperset". In her game, there are $n$ cards with $k$ features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with $k = 4$. Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set. Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table. -----Input----- The first line of each test contains two integers $n$ and $k$ ($1 \le n \le 1500$, $1 \le k \le 30$) — number of cards and number of features. Each of the following $n$ lines contains a card description: a string consisting of $k$ letters "S", "E", "T". The $i$-th character of this string decribes the $i$-th feature of that card. All cards are distinct. -----Output----- Output a single integer — the number of ways to choose three cards that form a set. -----Examples----- Input 3 3 SET ETS TSE Output 1 Input 3 4 SETE ETSE TSES Output 0 Input 5 4 SETT TEST EEET ESTE STES Output 2 -----Note----- In the third example test, these two triples of cards are sets: "SETT", "TEST", "EEET" "TEST", "ESTE", "STES"
n, k = map(int, input().split()) d = {} d1 = {} for i in range(n): d[i] = input() for i in range(n - 1, -1, -1): if d1.get(d[i], -1) == -1: d1[d[i]] = [] d1[d[i]].append(i) ans = 0 r = k for i in range(n): for j in range(i + 1, n): s = "" cnt = 0 for k in range(r): if d[i][k] == d[j][k]: s += d[i][k] cnt += 1 elif abs(ord(d[i][k]) - ord(d[j][k])) == 14: s += "T" elif abs(ord(d[i][k]) - ord(d[j][k])) == 15: s += "S" else: s += "E" if d1.get(s, -1) != -1: if cnt == r: d1[s].pop() d1[s].pop() if d1.get(s, -1) != -1: for i1 in d1[s]: if i1 < j: break else: ans += 1 else: for i1 in d1[s]: if i1 <= j: break else: ans += 1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR STRING IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR STRING VAR STRING IF FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR VAR IF VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look. [Image] Polina came up with a new game called "Hyperset". In her game, there are $n$ cards with $k$ features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with $k = 4$. Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set. Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table. -----Input----- The first line of each test contains two integers $n$ and $k$ ($1 \le n \le 1500$, $1 \le k \le 30$) — number of cards and number of features. Each of the following $n$ lines contains a card description: a string consisting of $k$ letters "S", "E", "T". The $i$-th character of this string decribes the $i$-th feature of that card. All cards are distinct. -----Output----- Output a single integer — the number of ways to choose three cards that form a set. -----Examples----- Input 3 3 SET ETS TSE Output 1 Input 3 4 SETE ETSE TSES Output 0 Input 5 4 SETT TEST EEET ESTE STES Output 2 -----Note----- In the third example test, these two triples of cards are sets: "SETT", "TEST", "EEET" "TEST", "ESTE", "STES"
n, k = map(int, input().split()) deck = [] for _ in range(n): deck.append(input()) ans = 0 deck.sort() def thirdChar(a, b): if a == b: return a if a == "S" and b == "T" or a == "T" and b == "S": return "E" if a == "S" and b == "E" or a == "E" and b == "S": return "T" if a == "E" and b == "T" or a == "T" and b == "E": return "S" def findThird(c1, c2): l = [] for i in range(k): l.append(thirdChar(c1[i], c2[i])) return "".join(l) def binarySearch(sortedList, elem): l = 0 r = len(sortedList) - 1 while l <= r: m = (l + r) // 2 if elem == sortedList[m]: return m if elem > sortedList[m]: l = m + 1 else: r = m - 1 return -1 for i, c1 in enumerate(deck): for c2 in deck[i + 1 :]: c3 = findThird(c1, c2) if c3 > c2 and binarySearch(deck, c3) != -1: ans += 1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN VAR IF VAR STRING VAR STRING VAR STRING VAR STRING RETURN STRING IF VAR STRING VAR STRING VAR STRING VAR STRING RETURN STRING IF VAR STRING VAR STRING VAR STRING VAR STRING RETURN STRING FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL STRING VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look. [Image] Polina came up with a new game called "Hyperset". In her game, there are $n$ cards with $k$ features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with $k = 4$. Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set. Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table. -----Input----- The first line of each test contains two integers $n$ and $k$ ($1 \le n \le 1500$, $1 \le k \le 30$) — number of cards and number of features. Each of the following $n$ lines contains a card description: a string consisting of $k$ letters "S", "E", "T". The $i$-th character of this string decribes the $i$-th feature of that card. All cards are distinct. -----Output----- Output a single integer — the number of ways to choose three cards that form a set. -----Examples----- Input 3 3 SET ETS TSE Output 1 Input 3 4 SETE ETSE TSES Output 0 Input 5 4 SETT TEST EEET ESTE STES Output 2 -----Note----- In the third example test, these two triples of cards are sets: "SETT", "TEST", "EEET" "TEST", "ESTE", "STES"
n, k = input().split(" ") n = int(n) k = int(k) arr = [] set1 = set() count = 0 for i in range(n): arr.append(input()) set1.add(arr[-1]) for i in range(n): for j in range(i + 1, n): cons_str = "" for u in range(k): if arr[i][u] != arr[j][u]: if arr[i][u] != "S" and arr[j][u] != "S": cons_str += "S" elif arr[i][u] != "E" and arr[j][u] != "E": cons_str += "E" elif arr[i][u] != "T" and arr[j][u] != "T": cons_str += "T" else: cons_str += arr[i][u] if cons_str in set1: count += 1 print(count // 3)
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR IF VAR VAR VAR STRING VAR VAR VAR STRING VAR STRING IF VAR VAR VAR STRING VAR VAR VAR STRING VAR STRING IF VAR VAR VAR STRING VAR VAR VAR STRING VAR STRING VAR VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look. [Image] Polina came up with a new game called "Hyperset". In her game, there are $n$ cards with $k$ features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with $k = 4$. Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set. Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table. -----Input----- The first line of each test contains two integers $n$ and $k$ ($1 \le n \le 1500$, $1 \le k \le 30$) — number of cards and number of features. Each of the following $n$ lines contains a card description: a string consisting of $k$ letters "S", "E", "T". The $i$-th character of this string decribes the $i$-th feature of that card. All cards are distinct. -----Output----- Output a single integer — the number of ways to choose three cards that form a set. -----Examples----- Input 3 3 SET ETS TSE Output 1 Input 3 4 SETE ETSE TSES Output 0 Input 5 4 SETT TEST EEET ESTE STES Output 2 -----Note----- In the third example test, these two triples of cards are sets: "SETT", "TEST", "EEET" "TEST", "ESTE", "STES"
import itertools def go(): n, k = map(int, input().split()) s = [input() for _ in range(n)] dic = {"S": 1, "E": 2, "T": 3} s2 = [[dic[let] for let in ss] for ss in s] ts = {s[i]: i for i in range(n)} dif = {} alp = "SET" for aa, bb, cc in itertools.permutations(alp): dif[dic[aa] * dic[bb]] = cc for aa in alp: dif[dic[aa] * dic[aa]] = aa done = [-1] * n donep = [-1] * (n * n) answer = 0 for i in range(n - 2): a = s2[i] for j in range(i + 1, n - 1): if done[j] == i or donep[i * n + j] == 1: continue b = s2[j] c = "".join(dif[a[kk] * b[kk]] for kk in range(k)) if c in ts: l = ts[c] if l > j: answer += 1 done[l] = i donep[j * n + l] = 1 print(answer) go()
IMPORT FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT STRING STRING STRING NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look. [Image] Polina came up with a new game called "Hyperset". In her game, there are $n$ cards with $k$ features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with $k = 4$. Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set. Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table. -----Input----- The first line of each test contains two integers $n$ and $k$ ($1 \le n \le 1500$, $1 \le k \le 30$) — number of cards and number of features. Each of the following $n$ lines contains a card description: a string consisting of $k$ letters "S", "E", "T". The $i$-th character of this string decribes the $i$-th feature of that card. All cards are distinct. -----Output----- Output a single integer — the number of ways to choose three cards that form a set. -----Examples----- Input 3 3 SET ETS TSE Output 1 Input 3 4 SETE ETSE TSES Output 0 Input 5 4 SETT TEST EEET ESTE STES Output 2 -----Note----- In the third example test, these two triples of cards are sets: "SETT", "TEST", "EEET" "TEST", "ESTE", "STES"
n, k = map(int, input().split()) l = [] for i in range(n): l.append(input()) ans = 0 for i in range(n): s = set() for jj in range(i + 1, n): s.add(l[jj]) for j in range(i + 1, n): to_find = "" for kk in range(k): if l[i][kk] == l[j][kk]: to_find += l[i][kk] else: for ss in ["S", "E", "T"]: if ss not in l[i][kk] and ss not in l[j][kk]: to_find += ss break s.remove(l[j]) if to_find in s: ans += 1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR LIST STRING STRING STRING IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look. [Image] Polina came up with a new game called "Hyperset". In her game, there are $n$ cards with $k$ features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with $k = 4$. Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set. Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table. -----Input----- The first line of each test contains two integers $n$ and $k$ ($1 \le n \le 1500$, $1 \le k \le 30$) — number of cards and number of features. Each of the following $n$ lines contains a card description: a string consisting of $k$ letters "S", "E", "T". The $i$-th character of this string decribes the $i$-th feature of that card. All cards are distinct. -----Output----- Output a single integer — the number of ways to choose three cards that form a set. -----Examples----- Input 3 3 SET ETS TSE Output 1 Input 3 4 SETE ETSE TSES Output 0 Input 5 4 SETT TEST EEET ESTE STES Output 2 -----Note----- In the third example test, these two triples of cards are sets: "SETT", "TEST", "EEET" "TEST", "ESTE", "STES"
import sys input = sys.stdin.readline def getInt(): return int(input()) def getVars(): return map(int, input().split()) def getList(): return list(map(int, input().split())) def getStr(): return input().strip() n, k = getVars() res = 0 s = [] for i in range(n): s.append(getStr()) d = {} for i in range(n): srez = "" for j in range(k): srez += s[i][j] if srez not in d: d[srez] = [] d[srez].append(i) for i in range(n - 2): for j in range(i + 1, n - 1): ss = "" for p in range(k): if s[i][p] == s[j][p]: ss += s[i][p] else: if s[i][p] == "S" and s[j][p] == "E": ss += "T" if s[i][p] == "S" and s[j][p] == "T": ss += "E" if s[i][p] == "E" and s[j][p] == "S": ss += "T" if s[i][p] == "E" and s[j][p] == "T": ss += "S" if s[i][p] == "T" and s[j][p] == "S": ss += "E" if s[i][p] == "T" and s[j][p] == "E": ss += "S" if ss not in d: break if len(ss) == k and ss in d: for p in d[ss]: if p > i and p > j: res += 1 print(res)
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR STRING VAR VAR VAR STRING VAR STRING IF VAR VAR VAR STRING VAR VAR VAR STRING VAR STRING IF VAR VAR VAR STRING VAR VAR VAR STRING VAR STRING IF VAR VAR VAR STRING VAR VAR VAR STRING VAR STRING IF VAR VAR VAR STRING VAR VAR VAR STRING VAR STRING IF VAR VAR VAR STRING VAR VAR VAR STRING VAR STRING IF VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look. [Image] Polina came up with a new game called "Hyperset". In her game, there are $n$ cards with $k$ features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with $k = 4$. Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set. Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table. -----Input----- The first line of each test contains two integers $n$ and $k$ ($1 \le n \le 1500$, $1 \le k \le 30$) — number of cards and number of features. Each of the following $n$ lines contains a card description: a string consisting of $k$ letters "S", "E", "T". The $i$-th character of this string decribes the $i$-th feature of that card. All cards are distinct. -----Output----- Output a single integer — the number of ways to choose three cards that form a set. -----Examples----- Input 3 3 SET ETS TSE Output 1 Input 3 4 SETE ETSE TSES Output 0 Input 5 4 SETT TEST EEET ESTE STES Output 2 -----Note----- In the third example test, these two triples of cards are sets: "SETT", "TEST", "EEET" "TEST", "ESTE", "STES"
n, k = map(int, input().split(" ")) cards_set = set() cards_l = [] for _ in range(n): card = str(input()) cards_set.add(card) cards_l.append(card) dict = {"S": 0, "E": 1, "T": 2} my_list = ["S", "E", "T"] ans = 0 for i in range(n): for j in range(i + 1, n): s = str() for c in range(k): if cards_l[i][c] != cards_l[j][c]: s = s + my_list[3 - dict[cards_l[i][c]] - dict[cards_l[j][c]]] else: s = s + cards_l[i][c] if s in cards_set: ans += 1 print(ans // 3)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT STRING STRING STRING NUMBER NUMBER NUMBER ASSIGN VAR LIST STRING STRING STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look. [Image] Polina came up with a new game called "Hyperset". In her game, there are $n$ cards with $k$ features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with $k = 4$. Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set. Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table. -----Input----- The first line of each test contains two integers $n$ and $k$ ($1 \le n \le 1500$, $1 \le k \le 30$) — number of cards and number of features. Each of the following $n$ lines contains a card description: a string consisting of $k$ letters "S", "E", "T". The $i$-th character of this string decribes the $i$-th feature of that card. All cards are distinct. -----Output----- Output a single integer — the number of ways to choose three cards that form a set. -----Examples----- Input 3 3 SET ETS TSE Output 1 Input 3 4 SETE ETSE TSES Output 0 Input 5 4 SETT TEST EEET ESTE STES Output 2 -----Note----- In the third example test, these two triples of cards are sets: "SETT", "TEST", "EEET" "TEST", "ESTE", "STES"
from sys import stdin, stdout input = stdin.readline def conv2(l1, l2): if l1 == l2: return l1 if l1 == "S": if l2 == "E": return "T" return "E" if l1 == "E": if l2 == "S": return "T" return "S" if l1 == "T": if l2 == "E": return "S" return "E" n, k = map(int, input().split()) sets = [""] * n g = {} tot = 0 for i in range(n): sets[i] = input().rstrip() if g.get(sets[i], 0) > 0: tot += g[sets[i]] for j in range(0, i): string = "".join([conv2(sets[i][m], sets[j][m]) for m in range(k)]) g[string] = g.get(string, 0) + 1 stdout.write(str(tot))
ASSIGN VAR VAR FUNC_DEF IF VAR VAR RETURN VAR IF VAR STRING IF VAR STRING RETURN STRING RETURN STRING IF VAR STRING IF VAR STRING RETURN STRING RETURN STRING IF VAR STRING IF VAR STRING RETURN STRING RETURN STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look. [Image] Polina came up with a new game called "Hyperset". In her game, there are $n$ cards with $k$ features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with $k = 4$. Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set. Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table. -----Input----- The first line of each test contains two integers $n$ and $k$ ($1 \le n \le 1500$, $1 \le k \le 30$) — number of cards and number of features. Each of the following $n$ lines contains a card description: a string consisting of $k$ letters "S", "E", "T". The $i$-th character of this string decribes the $i$-th feature of that card. All cards are distinct. -----Output----- Output a single integer — the number of ways to choose three cards that form a set. -----Examples----- Input 3 3 SET ETS TSE Output 1 Input 3 4 SETE ETSE TSES Output 0 Input 5 4 SETT TEST EEET ESTE STES Output 2 -----Note----- In the third example test, these two triples of cards are sets: "SETT", "TEST", "EEET" "TEST", "ESTE", "STES"
import sys ip = lambda: sys.stdin.readline().rstrip() n, k = map(int, ip().split()) a = [] s = set() for _ in range(n): a.append(ip()) s.add(a[-1]) ans = 0 for i in range(n): for j in range(i + 1, n): s1, s2 = a[i], a[j] s3 = "" for K in range(k): if s1[K] == s2[K]: s3 += s1[K] else: v1, v2 = s1[K], s2[K] if v1 == "S" and v2 == "E" or v1 == "E" and v2 == "S": s3 += "T" elif v1 == "S" and v2 == "T" or v1 == "T" and v2 == "S": s3 += "E" elif v1 == "T" and v2 == "E" or v1 == "E" and v2 == "T": s3 += "S" if s3 in s: ans += 1 print(ans // 3)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING IF VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING IF VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def isWordExist(self, board, word): n, m = len(board), len(board[0]) seen = set() def helper(i, j, idx): if idx == len(word): return True if ( (i >= n or j >= m or i < 0 or j < 0) or (i, j) in seen or board[i][j] != word[idx] ): return False seen.add((i, j)) res = ( helper(i + 1, j, idx + 1) or helper(i - 1, j, idx + 1) or helper(i, j - 1, idx + 1) or helper(i, j + 1, idx + 1) ) seen.remove((i, j)) return res for i in range(n): for j in range(m): if helper(i, j, 0): return True return False
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER 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 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 EXPR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def isWordExist(self, board, word): n = len(board) m = len(board[0]) def dfs(i, j, n, m, L, vis, ind): vis.add((i, j)) if ind == L: return True for u, v in [[0, 1], [1, 0], [0, -1], [-1, 0]]: ro = i + u co = j + v if ( ro > -1 and co > -1 and ro < n and co < m and board[ro][co] == word[ind] and (ro, co) not in vis ): if dfs(ro, co, n, m, L, vis, ind + 1): return True vis.discard((i, j)) return False for i in range(n): for j in range(m): if board[i][j] == word[0]: if dfs(i, j, n, m, len(word), set(), 1): return True return False
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN NUMBER FOR VAR VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER 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 VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER RETURN NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
def visit(board, word, curr, d, done, n, m, i, j): if curr == len(word): done[0] = True return dirs = [(-1, 0), (1, 0), (0, -1), (0, 1)] for a, b in dirs: if ( i + a >= 0 and j + b >= 0 and i + a < n and j + b < m and word[curr] == board[i + a][j + b] ): c = (i + a) * m + j + b if c not in d: d[c] = True visit(board, word, curr + 1, d, done, n, m, i + a, j + b) del d[c] class Solution: def isWordExist(self, board, word): n, m = len(board), len(board[0]) for i in range(n): for j in range(m): if board[i][j] == word[0]: d = {} done = [False] d[i * m + j] = True visit(board, word, 1, d, done, n, m, i, j) if done[0]: return True return False
FUNC_DEF IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER RETURN ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR CLASS_DEF 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 DICT ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def DFSBackTracking(self, board, visited, row, col, word, index, n, m): if index == len(word): return True if ( row < 0 or row >= n or col < 0 or col >= m or board[row][col] != word[index] or visited[row][col] ): return False visited[row][col] = True right = self.DFSBackTracking( board, visited, row, col + 1, word, index + 1, n, m ) left = self.DFSBackTracking(board, visited, row, col - 1, word, index + 1, n, m) up = self.DFSBackTracking(board, visited, row - 1, col, word, index + 1, n, m) down = self.DFSBackTracking(board, visited, row + 1, col, word, index + 1, n, m) visited[row][col] = False return right or left or up or down def isWordExist(self, board, word): n = len(board) m = len(board[0]) visited = [[(False) for col in range(m)] for row in range(n)] for i in range(n): for j in range(m): if board[i][j] == word[0] and visited[i][j] == False: if ( self.DFSBackTracking(board, visited, i, j, word, 0, n, m) == True ): return True return False
CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR 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 VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER RETURN NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def isWordExist(self, board, word): def dfs(x, y, i): if i == len(word): return True if ( x < 0 or y < 0 or x == len(board) or y == len(board[0]) or board[x][y] != word[i] ): return False board[x][y] = "_" for k in range(-1, 2, 2): if dfs(x + k, y, i + 1) or dfs(x, y + k, i + 1): return True board[x][y] = word[i] return False for i in range(len(board)): for j in range(len(board[0])): if board[i][j] == word[0] and dfs(i, j, 0): return True return False
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def isWordExist(self, board, word): l = len(word) grid = board n, m = len(grid), len(grid[0]) def dfs(i, j, l, ind): nonlocal vis if ind == l: return 1 x, y, z, w = 0, 0, 0, 0 vis[i][j] = 1 if i + 1 < n and grid[i + 1][j] == word[ind] and vis[i + 1][j] == 0: x = dfs(i + 1, j, l, ind + 1) if j + 1 < m and grid[i][j + 1] == word[ind] and vis[i][j + 1] == 0: y = dfs(i, j + 1, l, ind + 1) if i - 1 >= 0 and grid[i - 1][j] == word[ind] and vis[i - 1][j] == 0: z = dfs(i - 1, j, l, ind + 1) if j - 1 >= 0 and grid[i][j - 1] == word[ind] and vis[i][j - 1] == 0: w = dfs(i, j - 1, l, ind + 1) return x or z or y or w for i in range(n): for j in range(m): if grid[i][j] == word[0]: vis = [[(0) for x in range(m)] for j in range(n)] if dfs(i, j, l, 1): return True return False
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def isWordExist(self, board, word): visited = set() ROW, COL, N = len(board), len(board[0]), len(word) for i in range(ROW): for j in range(COL): if self.helper(i, j, ROW, COL, 0, board, word, N, visited): return True return False def helper(self, i, j, row, column, index, board, word, N, visited): if index == N: return True if ( not 0 <= i < row or not 0 <= j < column or not board[i][j] == word[index] or (i, j) in visited ): return False visited.add((i, j)) possible = ( self.helper(i + 1, j, row, column, index + 1, board, word, N, visited) or self.helper(i - 1, j, row, column, index + 1, board, word, N, visited) or self.helper(i, j + 1, row, column, index + 1, board, word, N, visited) or self.helper(i, j - 1, row, column, index + 1, board, word, N, visited) ) visited.remove((i, j)) return possible
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF NUMBER VAR VAR NUMBER 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 VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def isWordExist(self, board, word): l = len(word) n = len(board) m = len(board[0]) dic = {} mat = [[-1, 0], [0, -1], [1, 0], [0, 1]] for i in range(n): for j in range(m): if board[i][j] == word[0]: dic[str(i) + str(j)] = 1 ans = self.recurse(board, i, j, 1, dic, l, mat, n, m) if ans: return True else: dic.pop(str(i) + str(j)) return False def recurse(self, board, row, col, w, dic, l, mat, n, m): if w >= l: return True for el in mat: x, y = row + el[0], col + el[1] s = str(x) + str(y) if ( x >= 0 and x < n and y >= 0 and y < m and board[x][y] == word[w] and s not in dic ): dic[s] = 1 ans = self.recurse(board, x, y, w + 1, dic, l, mat, n, m) if ans: return True else: dic.pop(s) return False
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR IF VAR RETURN NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR IF VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def isWordExist(self, board, word): rw = len(board) cl = len(board[0]) n = len(word) vis = {} res = [0] def dfs(r, c, i, cur): if i == n: if cur == word: return 1 if ( r < 0 or r == rw or c < 0 or c == cl or board[r][c] != word[i] or vis.get((r, c)) == 1 ): return 0 vis[r, c] = 1 x = ( dfs(r + 1, c, i + 1, cur + board[r][c]) or dfs(r - 1, c, i + 1, cur + board[r][c]) or dfs(r, c + 1, i + 1, cur + board[r][c]) or dfs(r, c - 1, i + 1, cur + board[r][c]) ) vis[r, c] = 0 return x for i in range(rw): for j in range(cl): if board[i][j] == word[0]: if dfs(i, j, 0, ""): return 1 return 0
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST NUMBER FUNC_DEF IF VAR VAR IF VAR VAR RETURN NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN 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 STRING RETURN NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def isWordExist(self, board, word): def dfs(board, i, j, word): if not word: return True if ( i < 0 or j < 0 or i == len(board) or j == len(board[0]) or board[i][j] != word[0] ): return False tmp = board[i][j] board[i][j] = "#" res = ( dfs(board, i + 1, j, word[1:]) or dfs(board, i - 1, j, word[1:]) or dfs(board, i, j + 1, word[1:]) or dfs(board, i, j - 1, word[1:]) ) board[i][j] = tmp return res for i in range(len(board)): for j in range(len(board[0])): if dfs(board, i, j, word): return True return False
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN 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 VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def isWordExist(self, board, word): m = len(board) n = len(board[0]) ind = 0 for i in range(m): for j in range(n): if board[i][j] == word[ind]: if self.search(board, word, i, j, ind, m, n): return True return False def search(self, board, word, row, col, ind, m, n): if ind == len(word): return True if ( row < 0 or col < 0 or row == m or col == n or board[row][col] != word[ind] or board[row][col] == "!" ): return False c = board[row][col] board[row][col] = "!" top = self.search(board, word, row - 1, col, ind + 1, m, n) left = self.search(board, word, row, col - 1, ind + 1, m, n) right = self.search(board, word, row, col + 1, ind + 1, m, n) down = self.search(board, word, row + 1, col, ind + 1, m, n) board[row][col] = c return top or left or right or down
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN 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 STRING RETURN NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR VAR
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def isWordExist(self, board, word): def find(i, j, pos, board): if pos >= len(word): return True if i < 0 or j < 0 or i >= len(board) or j >= len(board[0]): return False if board[i][j] != word[pos]: return False temp = board[i][j] board[i][j] = "$" top = find(i - 1, j, pos + 1, board) left = find(i, j - 1, pos + 1, board) bottom = find(i + 1, j, pos + 1, board) right = find(i, j + 1, pos + 1, board) board[i][j] = temp return top or left or right or bottom for i in range(len(board)): for j in range(len(board[i])): if find(i, j, 0, board): return True return False
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR RETURN NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def isWordExist(self, board, word): n, m = len(board), len(board[0]) x = set() def dfs(i, j, n, m, ind, s): if i < 0 or j < 0 or i >= n or j >= m or (i, j) in v: return if s[ind] != board[i][j]: return False if len(s) - 1 == ind: return True v.add((i, j)) a = dfs(i + 1, j, n, m, ind + 1, s) b = dfs(i - 1, j, n, m, ind + 1, s) c = dfs(i, j + 1, n, m, ind + 1, s) d = dfs(i, j - 1, n, m, ind + 1, s) return a or b or c or d for i in range(n): for j in range(m): if board[i][j] == word[0]: v = set() if dfs(i, j, n, m, 0, word): return True return False
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR RETURN IF VAR VAR VAR VAR VAR RETURN NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR RETURN NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def isWordExist(self, board, word): def calc(r, c, pos): if pos == len(word): return True for nr, nc in [[r - 1, c], [r + 1, c], [r, c - 1], [r, c + 1]]: if ( nr >= 0 and nr < len(board) and nc >= 0 and nc < len(board[0]) and board[nr][nc] == word[pos] and vis[nr][nc] == 0 ): vis[nr][nc] = 1 if calc(nr, nc, pos + 1): return True vis[nr][nc] = 0 return False vis = [[(0) for i in range(len(board[0]))] for j in range(len(board))] for i in range(len(board)): for j in range(len(board[0])): if board[i][j] == word[0]: vis[i][j] = 1 if calc(i, j, 1): return True vis[i][j] = 0 return False
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER FOR VAR VAR LIST LIST BIN_OP VAR NUMBER VAR LIST BIN_OP VAR NUMBER VAR LIST VAR BIN_OP VAR NUMBER LIST VAR BIN_OP VAR 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 NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def isWordExist(self, a, word): for r in range(len(a)): for c in range(len(a[0])): if a[r][c] == word[0] and self.search(a, r, c, word, 0): return True return False def search(self, a, r, c, word, ind): if ind == len(word): return True if r < 0 or r >= len(a) or c < 0 or c >= len(a[0]) or a[r][c] != word[ind]: return False temp = a[r][c] a[r][c] = " " found = ( self.search(a, r + 1, c, word, ind + 1) or self.search(a, r - 1, c, word, ind + 1) or self.search(a, r, c + 1, word, ind + 1) or self.search(a, r, c - 1, word, ind + 1) ) a[r][c] = temp return found
CLASS_DEF FUNC_DEF 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 FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def isWordExist(self, l, s): n, m = len(l), len(l[0]) v = set() dir = [(-1, 0), (1, 0), (0, -1), (0, 1)] def dfs(i, j, x): if x is "": return True if i < 0 or j < 0 or i == n or j == m or (i, j) in v or l[i][j] != x[0]: return False f = False v.add((i, j)) for p, q in dir: f = f or dfs(i + p, j + q, x[1:]) v.remove((i, j)) return f for i in range(n): for j in range(m): if l[i][j] == s[0]: if dfs(i, j, s): return True return False
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF IF VAR STRING RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN 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 RETURN NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def __init__(self): self.ans = False def solve(self, word, pos, board, i, j, n, m, vis, ind): if pos == ind - 1: self.ans = True return vis[i][j] = True for x1, y1 in self.next: x, y = i + x1, j + y1 if ( -1 < x < n and -1 < y < m and word[pos + 1] == board[x][y] and vis[x][y] == False ): self.solve(word, pos + 1, board, x, y, n, m, vis, ind) vis[i][j] = False def isWordExist(self, board, word): n = len(board) m = len(board[0]) self.next = [[-1, 0], [0, -1], [1, 0], [0, 1]] vis = [[(False) for _ in range(m)] for _ in range(n)] for i in range(n): for j in range(m): if word[0] == board[i][j]: self.solve(word, 0, board, i, j, n, m, vis, len(word)) if self.ans == True: return True return self.ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER RETURN ASSIGN VAR VAR VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER 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 EXPR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER RETURN VAR
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def findword(self, board, word, i, j, k, vis): if k == len(word) - 1 and word[k] == board[i][j]: vis[i][j] = False return True ans = False if word[k] == board[i][j]: vis[i][j] = False li = vis if i - 1 >= 0 and vis[i - 1][j]: ans = ans or self.findword(board, word, i - 1, j, k + 1, li) if i + 1 < len(board) and vis[i + 1][j]: ans = ans or self.findword(board, word, i + 1, j, k + 1, li) if j - 1 >= 0 and vis[i][j - 1]: ans = ans or self.findword(board, word, i, j - 1, k + 1, li) if j + 1 < len(board[0]) and vis[i][j + 1]: ans = ans or self.findword(board, word, i, j + 1, k + 1, li) return ans def isWordExist(self, board, word): n = len(board) m = len(board[0]) for i in range(n): for j in range(m): if board[i][j] == word[0]: vis = [[(True) for j in range(m)] for i in range(n)] if self.findword(board, word, i, j, 0, vis): return True return False
CLASS_DEF FUNC_DEF IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR RETURN NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def dfs(self, row, col, idx, board, word, visited, R, C): if ( row >= 0 and row < R and col >= 0 and col < C and board[row][col] == word[idx] and not visited[row][col] ): visited[row][col] = True dxy = [[-1, 0], [1, 0], [0, -1], [0, 1]] for dx, dy in dxy: n_row = row + dx n_col = col + dy n_idx = idx + 1 if n_idx == len(word): return True if self.dfs(n_row, n_col, n_idx, board, word, visited, R, C): return True visited[row][col] = False return False def isWordExist(self, board, word): R = len(board) C = len(board[0]) visited = [[(False) for _ in range(C)] for _ in range(R)] for row in range(R): for col in range(C): if board[row][col] == word[0]: if self.dfs(row, col, 0, board, word, visited, R, C): return True return False
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR 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 NUMBER VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def adjacentSearch(self, board, word, i, j, l): if l == len(word): return True if i < 0 or j < 0 or i >= len(board) or j >= len(board[0]): return False if board[i][j] != word[l]: return False board[i][j] = "*" ans = ( self.adjacentSearch(board, word, i - 1, j, l + 1) or self.adjacentSearch(board, word, i + 1, j, l + 1) or self.adjacentSearch(board, word, i, j - 1, l + 1) or self.adjacentSearch(board, word, i, j + 1, l + 1) ) board[i][j] = word[l] return ans def isWordExist(self, board, word): for i in range(len(board)): for j in range(len(board[0])): if board[i][j] == word[0] and self.adjacentSearch(board, word, i, j, 0): return True return False
CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL 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 FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF 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 FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def isWordExist(self, board, word): n = len(board) m = len(board[0]) starts = [] for i in range(n): for j in range(m): if board[i][j] == word[0]: starts.append((i, j)) output = [] for x, y in starts: output += self.helper(word, 1, x, y, n, m, board, set()) return int(any(output)) def helper(self, word, i, x, y, n, m, board, path): if (x, y) in path: return [False] path.add((x, y)) if i == len(word): return [True] output = [False] if x > 0 and board[x - 1][y] == word[i]: output += self.helper(word, i + 1, x - 1, y, n, m, board, path.copy()) if x < n - 1 and board[x + 1][y] == word[i]: output += self.helper(word, i + 1, x + 1, y, n, m, board, path.copy()) if y > 0 and board[x][y - 1] == word[i]: output += self.helper(word, i + 1, x, y - 1, n, m, board, path.copy()) if y < m - 1 and board[x][y + 1] == word[i]: output += self.helper(word, i + 1, x, y + 1, n, m, board, path.copy()) return [any(output)]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR RETURN LIST NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN LIST NUMBER ASSIGN VAR LIST NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR RETURN LIST FUNC_CALL VAR VAR
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: ans = 0 def dfs(self, board, word, i, j, visited, ch): if ch == word: self.ans = 1 return if len(ch) > len(word): return visited[i][j] = 1 if i - 1 >= 0 and visited[i - 1][j] == 0 and word[len(ch)] == board[i - 1][j]: visited[i - 1][j] = 1 self.dfs(board, word, i - 1, j, visited, ch + board[i - 1][j]) if self.ans: return visited[i - 1][j] = 0 if j - 1 >= 0 and visited[i][j - 1] == 0 and word[len(ch)] == board[i][j - 1]: visited[i][j - 1] = 1 self.dfs(board, word, i, j - 1, visited, ch + board[i][j - 1]) if self.ans: return visited[i][j - 1] = 0 if ( j + 1 < len(board[0]) and visited[i][j + 1] == 0 and word[len(ch)] == board[i][j + 1] ): visited[i][j + 1] = 1 self.dfs(board, word, i, j + 1, visited, ch + board[i][j + 1]) if self.ans: return visited[i][j + 1] = 0 if ( i + 1 < len(board) and visited[i + 1][j] == 0 and word[len(ch)] == board[i + 1][j] ): visited[i + 1][j] = 1 self.dfs(board, word, i + 1, j, visited, ch + board[i + 1][j]) if self.ans: return visited[i + 1][j] = 0 visited[i][j] = 0 def isWordExist(self, board, word): self.ans = 0 visited = [] for i in range(len(board) + 1): arr = [] for j in range(len(board[0]) + 1): arr.append(0) visited.append(arr) for i in range(len(board)): for j in range(len(board[0])): if board[i][j] == word[0]: self.dfs(board, word, i, j, visited, board[i][j]) if self.ans: return 1 return 0
CLASS_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR NUMBER RETURN IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR IF VAR RETURN ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR RETURN ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR RETURN ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR IF VAR RETURN ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER 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 VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR RETURN NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
def func(pos, board, word, visted, ans): i, j = pos if ( i < 0 or j < 0 or i >= len(board) or j >= len(board[0]) or visted[i][j] == True or len(word) == 0 or board[i][j] != word[0] ): return visted[i][j] = True if len(word) == 1: ans[0] = 1 return func((i, j + 1), board, word[1:], visted, ans) func((i + 1, j), board, word[1:], visted, ans) func((i, j - 1), board, word[1:], visted, ans) func((i - 1, j), board, word[1:], visted, ans) visted[i][j] = False class Solution: def isWordExist(self, board, word): visted = [[(False) for i in range(len(board[0]))] for i in range(len(board))] ans = [0] for i in range(len(board)): for j in range(len(board[0])): if board[i][j] == word[0]: func((i, j), board, word, visted, ans) return ans[0]
FUNC_DEF ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER RETURN EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST 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 RETURN VAR NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def isWordExist(self, board, word): def recurs(c, i, j, grid): if c == "": return True elif i < 0 or i == len(board) or j < 0 or j == len(board[0]): return False else: if grid[i][j] == c[0]: a = c[1:] m = grid[i][j] grid[i][j] = 0 if ( recurs(a, i, j - 1, grid) or recurs(a, i, j + 1, grid) or recurs(a, i - 1, j, grid) or recurs(a, i + 1, j, grid) ): return True else: grid[i][j] = m return False return False for i in range(len(board)): for j in range(len(board[0])): if recurs(word, i, j, board): return True return False
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR STRING RETURN NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def is_valid(self, x, y, visited, N, M): if x < 0 or y < 0 or x >= N or y >= M or visited[x][y] == True: return False return True def DFS(self, node, graph, visited, word, letter): u, v = node[0], node[1] if letter >= len(word): return True visited[node[0]][node[1]] = True for x, y in [[0, 1], [0, -1], [1, 0], [-1, 0]]: ch_x = x + u ch_y = y + v if ( self.is_valid(ch_x, ch_y, visited, len(graph), len(graph[0])) and graph[ch_x][ch_y] == word[letter] ): if self.DFS([ch_x, ch_y], graph, visited, word, letter + 1): return True visited[node[0]][node[1]] = False return False def isWordExist(self, board, word): visited = [[(False) for i in range(len(board[0]))] for j in range(len(board))] for i in range(0, len(board)): for j in range(0, len(board[0])): if board[i][j] == word[0]: if self.DFS([i, j], board, visited, word, 1): return True return False
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER FOR VAR VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR IF FUNC_CALL VAR LIST VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR LIST VAR VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
def wordSearch(board, word): n, m = len(board), len(board[0]) for i in range(n): for j in range(m): if board[i][j] == word[0]: vis = set() vis.add((i, j)) text = board[i][j] ind = 1 res = helper(i, j, n, m, board, vis, text, ind, word) if res: return True return False def helper(i, j, n, m, board, vis, text, ind, word): if text == word: return True for ti, tj in zip([1, -1, 0, 0], [0, 0, 1, -1]): ni, nj = ti + i, tj + j if isValid(ni, nj, n, m, board, ind, word) and (ni, nj) not in vis: vis.add((ni, nj)) if helper(ni, nj, n, m, board, vis, text + board[ni][nj], ind + 1, word): return True vis.remove((ni, nj)) def isValid(i, j, n, m, board, ind, word): if ( i < n and i >= 0 and j >= 0 and j < m and ind < len(word) and board[i][j] == word[ind] ): return True return False class Solution: def isWordExist(self, board, word): return wordSearch(board, word)
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 FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER FOR VAR VAR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def isWordExist(self, board, word): N = len(board) M = len(board[0]) visited = [[(False) for j in range(M)] for i in range(N)] for i in range(N): for j in range(M): if board[i][j] == word[0] and not visited[i][j]: if self.DFS(board, i, j, word, visited, 0): return True return False def DFS(self, board, i, j, word, visited, start): border_x = [-1, 0, 0, 1] border_y = [0, -1, 1, 0] visited[i][j] = True if word[start] != board[i][j] or start > len(word): visited[i][j] = False return False if word[start] == board[i][j] and start == len(word) - 1: return True for k in range(4): new_i = i + border_x[k] new_j = j + border_y[k] if self.isSafe(board, new_i, new_j, visited): ans = self.DFS(board, new_i, new_j, word, visited, start + 1) if ans: return True visited[i][j] = False return False def isSafe(self, board, i, j, visited): return 0 <= i < len(board) and 0 <= j < len(board[0]) and not visited[i][j]
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 FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF RETURN NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def explore(self, board, word, i, j, idx): n = len(board) m = len(board[0]) if idx == len(word): return True if ( i < 0 or i >= n or j < 0 or j >= m or board[i][j] == "." or board[i][j] != word[idx] ): return False temp_c = board[i][j] board[i][j] = "." top = self.explore(board, word, i - 1, j, idx + 1) bottom = self.explore(board, word, i + 1, j, idx + 1) left = self.explore(board, word, i, j - 1, idx + 1) right = self.explore(board, word, i, j + 1, idx + 1) board[i][j] = temp_c return top or bottom or left or right def isWordExist(self, board, word): n = len(board) m = len(board[0]) idx = 0 for i in range(n): for j in range(m): if board[i][j] == word[idx]: if self.explore(board, word, i, j, idx): return 1 return 0
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR STRING VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR STRING 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 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 VAR VAR VAR RETURN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def isWordExist(self, board, word): for i in range(0, len(board)): for j in range(0, len(board[0])): visited = [] for k in range(0, len(board)): visited.append([0] * len(board[0])) start = 0 if board[i][j] == word[start]: flag = self.find(i, j, start + 1, board, word, visited) if flag == 1: return 1 return 0 def find(self, i, j, start, board, word, visited): visited[i][j] = 1 if start == len(word): return 1 if start > len(word): return 0 if i - 1 >= 0: if board[i - 1][j] == word[start]: if visited[i - 1][j] != 1: flag = self.find(i - 1, j, start + 1, board, word, visited) if flag == 1: return 1 if j - 1 >= 0: if board[i][j - 1] == word[start]: if visited[i][j - 1] != 1: flag = self.find(i, j - 1, start + 1, board, word, visited) if flag == 1: return 1 if j + 1 < len(board[0]): if board[i][j + 1] == word[start]: if visited[i][j + 1] != 1: flag = self.find(i, j + 1, start + 1, board, word, visited) if flag == 1: return 1 if j - 1 >= 0: if board[i][j - 1] == word[start]: if visited[i][j - 1] != 1: flag = self.find(i, j - 1, start + 1, board, word, visited) if flag == 1: return 1 if j + 1 < len(board[0]): if board[i][j + 1] == word[start]: if visited[i][j + 1] != 1: flag = self.find(i, j + 1, start + 1, board, word, visited) if flag == 1: return 1 if i + 1 < len(board): if board[i + 1][j] == word[start]: if visited[i + 1][j] != 1: flag = self.find(i + 1, j, start + 1, board, word, visited) if flag == 1: return 1 return 0
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def search(self, board, word, index, board_i, board_j): if index == len(word): return True if ( board_i < 0 or board_i == len(board) or board_j < 0 or board_j == len(board[0]) ): return False if board[board_i][board_j] != word[index]: return False board[board_i][board_j] = "!" for movi, movj in self.directions: found = self.search(board, word, index + 1, board_i + movi, board_j + movj) if found: return True board[board_i][board_j] = word[index] return False def isWordExist(self, board, word): self.directions = [(1, 0), (-1, 0), (0, 1), (0, -1)] for i in range(len(board)): for j in range(len(board[0])): found = self.search(board, word, 0, i, j) if found: return True return False
CLASS_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 RETURN NUMBER IF VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR STRING FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR IF VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR IF VAR RETURN NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def isWordExist(self, board, word): visited = [([0] * len(board[0])) for i in range(len(board))] delrow = [-1, 0, 1, 0] delcol = [0, 1, 0, -1] for i in range(len(board)): for j in range(len(board[0])): if board[i][j] == word[0]: if self.dfs(i, j, board, visited, 1, delrow, delcol): return True return False def dfs(self, row, col, board, visited, index, delrow, delcol): if index == len(word): return True visited[row][col] = 1 for i in range(4): nr = row + delrow[i] nc = col + delcol[i] if ( nr >= 0 and nr < len(board) and nc >= 0 and nc < len(board[0]) and visited[nr][nc] == 0 and board[nr][nc] == word[index] ): if self.dfs(nr, nc, board, visited, index + 1, delrow, delcol): return True visited[row][col] = 0 return False
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER 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 NUMBER VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR 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 FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def __init__(self): self.flag = False def dfs(self, board, i, j, n, m, curr_word, word): if self.flag: return if curr_word >= len(word): self.flag = True return True if ( i < 0 or i >= n or j < 0 or j >= m or board[i][j] == "#" or board[i][j] != word[curr_word] ): return ch = board[i][j] board[i][j] = "#" self.dfs(board, i + 1, j, n, m, curr_word + 1, word) self.dfs(board, i, j + 1, n, m, curr_word + 1, word) self.dfs(board, i - 1, j, n, m, curr_word + 1, word) self.dfs(board, i, j - 1, n, m, curr_word + 1, word) board[i][j] = ch def isWordExist(self, board, word): n = len(board) m = len(board[0]) for i in range(n): for j in range(m): if board[i][j] == word[0]: self.dfs(board, i, j, n, m, 0, word) if self.flag: return True return False
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR RETURN IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR STRING VAR VAR VAR VAR VAR RETURN ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR IF VAR RETURN NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
def func(pos, board, word, visted, ans, index): i, j = pos if ( 0 <= i < len(board) and 0 <= j < len(board[0]) and board[i][j] == word[index] and (i, j) not in visted and index < len(word) ): visted.add((i, j)) if index == len(word) - 1: ans[0] = True return func((i + 1, j), board, word, visted, ans, index + 1) func((i, j + 1), board, word, visted, ans, index + 1) func((i - 1, j), board, word, visted, ans, index + 1) func((i, j - 1), board, word, visted, ans, index + 1) visted.remove((i, j)) class Solution: def isWordExist(self, board, word): ans = [False] for i in range(len(board)): for j in range(len(board[0])): visted = set() if board[i][j] == word[0]: func((i, j), board, word, visted, ans, 0) if ans[0]: return True return False
FUNC_DEF ASSIGN VAR VAR VAR IF NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER RETURN EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def isWordExist(self, board, word): def search(i, j, m, n, board, word, k): if k == len(word): return True if i < 0 or j < 0 or i == m or j == n or board[i][j] != word[k]: return False c = board[i][j] board[i][j] = "#" op1 = search(i + 1, j, m, n, board, word, k + 1) op2 = search(i, j + 1, m, n, board, word, k + 1) op3 = search(i - 1, j, m, n, board, word, k + 1) op4 = search(i, j - 1, m, n, board, word, k + 1) board[i][j] = c return op1 or op2 or op3 or op4 m = len(board) n = len(board[0]) for i in range(m): for j in range(n): if board[i][j] == word[0]: if search(i, j, m, n, board, word, 0): return True return False
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 RETURN NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
poss = [[0, 1], [0, -1], [1, 0], [-1, 0]] class Solution: def isWordExist(self, board, word): visited = [[(False) for j in range(len(board[0]))] for i in range(len(board))] for i in range(len(board)): for j in range(len(board[0])): if self.dfs(i, j, board, visited, word, 0): return 1 return 0 def dfs(self, i, j, board, visited, word, ind): if ind == len(word): return True if ( not (i >= 0 and i < len(board) and j >= 0 and j < len(board[0])) or visited[i][j] or board[i][j] != word[ind] ): return False visited[i][j] = True for a, b in poss: if self.dfs(i + a, j + b, board, visited, word, ind + 1): return True visited[i][j] = False return False
ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR 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 VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def isWordExist(self, board, word): def isValid(i, j, m, n, visited, board): if i < 0 or j < 0 or i >= m or j >= n or visited[i][j] == 1: return 0 return 1 def help(m, n, i, j, idx, word, wlen, board, visited): if idx == wlen: return 1 if isValid(i, j, m, n, visited, board): visited[i][j] = 1 if word[idx] == board[i][j]: if help(m, n, i + 1, j, idx + 1, word, wlen, board, visited): return 1 if help(m, n, i, j + 1, idx + 1, word, wlen, board, visited): return 1 if help(m, n, i - 1, j, idx + 1, word, wlen, board, visited): return 1 if help(m, n, i, j - 1, idx + 1, word, wlen, board, visited): return 1 visited[i][j] = 0 return 0 return 0 m = len(board) n = len(board[0]) visited = [] for i in range(m): visited.append([0] * n) setboard = set() setstring = set() for i in range(m): for j in range(n): setboard.add(board[i][j]) for x in word: setstring.add(x) if not setstring.issubset(setboard): return 0 for i in range(m): for j in range(n): if board[i][j] == word[0] and help( m, n, i, j, 0, word, len(word), board, visited ): return 1 return 0
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def isWordExist(self, M, w): R, C = len(M), len(M[0]) for r in range(R): for c in range(C): if M[r][c] == w[0] and dfs(r, c, w, 0, set(), M, R, C): return True return False def dfs(r, c, w, i, v, M, R, C): if i == len(w): return True if r < 0 or r == R or c < 0 or c == C: return False if M[r][c] != w[i]: return False if (r, c) in v: return False v.add((r, c)) for nr, nc in ((r + 1, c), (r - 1, c), (r, c + 1), (r, c - 1)): if dfs(nr, nc, w, i + 1, v, M, R, C): return True v.remove((r, c)) return False
CLASS_DEF 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 FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def helper(self, r, c, rows, cols, board, word): positionList = [(0, -1), (0, 1), (-1, 0), (1, 0)] if len(word) == 0: self.result = 1 return temp = board[r][c] board[r][c] = -1 for pos in positionList: r = r + pos[0] c = c + pos[1] if 0 <= r < rows and 0 <= c < cols and board[r][c] == word[0]: self.helper(r, c, rows, cols, board, word[1:]) r -= pos[0] c -= pos[1] board[r][c] = temp def isWordExist(self, board, word): self.result = 0 index_of_word = 0 rows = len(board) cols = len(board[0]) for i in range(rows): for j in range(cols): if board[i][j] == word[0]: self.helper(i, j, rows, cols, board, word[1:]) if self.result == 1: return True return False
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER RETURN ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
def dfs(graph, x, y, word, i, visited): if i >= len(word): return True if ( x < 0 or y < 0 or x >= len(graph) or y >= len(graph[0]) or visited[x][y] or graph[x][y] != word[i] ): return False visited[x][y] = True ans = ( dfs(graph, x - 1, y, word, i + 1, visited) or dfs(graph, x + 1, y, word, i + 1, visited) or dfs(graph, x, y - 1, word, i + 1, visited) or dfs(graph, x, y + 1, word, i + 1, visited) ) visited[x][y] = False return ans class Solution: def isWordExist(self, board, word): visited = [([0] * len(board[0])) for i in range(len(board))] for i in range(len(board)): for j in range(len(board[0])): if dfs(board, i, j, word, 0, visited): return True return False
FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR 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 VAR VAR VAR NUMBER VAR RETURN NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def isWordExist(self, board, word): n = len(board) m = len(board[0]) seen = [[(False) for i in range(m)] for j in range(n)] dx = [1, 0, -1, 0] dy = [0, 1, 0, -1] def dfs(i, j, k): if k == len(word): return True for t in range(4): r = i + dx[t] c = j + dy[t] if ( 0 <= r < n and 0 <= c < m and board[r][c] == word[k] and not seen[r][c] ): seen[r][c] = True if dfs(r, c, k + 1): return True seen[r][c] = False return False for i in range(n): for j in range(m): if board[i][j] == word[0] and not seen[i][j]: seen[i][j] = True if dfs(i, j, 1): return True seen[i][j] = False return False
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 NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
def isExist(board, r, c, p, q, word, i, n, visit): if i == n: return True if r < 0 or r >= p or c < 0 or c >= q or visit[r][c] or board[r][c] != word[i]: return False dr = [0, 0, 1, -1] dc = [1, -1, 0, 0] visit[r][c] = True for k in range(4): r_, c_ = r + dr[k], c + dc[k] if isExist(board, r_, c_, p, q, word, i + 1, n, visit): return True visit[r][c] = False return False class Solution: def isWordExist(self, board, word): p, q = len(board), len(board[0]) n = len(word) visit = [[(False) for c in range(q)] for r in range(p)] for r in range(p): for c in range(q): if isExist(board, r, c, p, q, word, 0, n, visit): return True return False
FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR 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 BIN_OP VAR NUMBER VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL 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 FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def isWordExist(self, board, word): val = False def recurs(i, j, target, curr): nonlocal val if ( i >= len(board) or i < 0 or j >= len(board[0]) or j < 0 or (i, j) in curr ): if target >= len(word): val = True return False if target >= len(word): val = True return if board[i][j] == word[target]: curr.append((i, j)) recurs(i + 1, j, target + 1, curr) recurs(i - 1, j, target + 1, curr) recurs(i, j + 1, target + 1, curr) recurs(i, j - 1, target + 1, curr) curr.pop() return for i in range(len(board)): for j in range(len(board[0])): if val == True: return True if board[i][j] == word[0]: curr = [] recurs(i, j, 0, curr) if val: return True return False
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER RETURN IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR NUMBER VAR IF VAR RETURN NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def find(self, i, j, board, vi, word, c, n, m): if i >= n or j >= m or i < 0 or j < 0: return False if vi[i][j]: return False if c == len(word): return True vi[i][j] = True if board[i][j] == word[c]: if ( self.find(i, j + 1, board, vi, word, c + 1, n, m) or self.find(i, j - 1, board, vi, word, c + 1, n, m) or self.find(i + 1, j, board, vi, word, c + 1, n, m) or self.find(i - 1, j, board, vi, word, c + 1, n, m) ): return True vi[i][j] = False return False def isWordExist(self, board, word): vi = [] n = len(board) m = len(board[0]) for i in range(n): l = [False] * m vi.append(l) for i in range(n): for j in range(m): if board[i][j] == word[0]: if self.find(i, j, board, vi, word, 0, n, m): return True return False
CLASS_DEF FUNC_DEF IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR RETURN NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR 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 VAR VAR RETURN NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def solve(self, i, j, word, board, n1, n2, index): found = 0 if i < n1 and j < n2 and i >= 0 and j >= 0 and word[index] == board[i][j]: temp = word[index] board[i][j] = 0 index += 1 if index == len(word): found = 1 else: found += self.solve(i + 1, j, word, board, n1, n2, index) found += self.solve(i - 1, j, word, board, n1, n2, index) found += self.solve(i, j + 1, word, board, n1, n2, index) found += self.solve(i, j - 1, word, board, n1, n2, index) board[i][j] = temp return found def isWordExist(self, board, word): result = 0 n1 = len(board) n2 = len(board[0]) for i in range(n1): for j in range(n2): result += self.solve(i, j, word, board, n1, n2, 0) return result
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def helper(self, board, x, y, row, col, word, pos, len1): if pos == len1 - 1: self.res = 1 return temp, board[x][y] = board[x][y], -1 for x1, y1 in self.next: x2, y2 = x + x1, y + y1 if -1 < x2 < row and -1 < y2 < col and board[x2][y2] == word[pos + 1]: self.helper(board, x2, y2, row, col, word, pos + 1, len1) board[x][y] = temp def isWordExist(self, board, word): row, col = len(board), len(board[0]) self.next = [[-1, 0], [0, -1], [1, 0], [0, 1]] self.res = 0 for i in range(row): for j in range(col): if board[i][j] == word[0]: self.helper(board, i, j, row, col, word, 0, len(word)) if self.res: return 1 return 0
CLASS_DEF FUNC_DEF IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER RETURN ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR IF VAR RETURN NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
def validate(board, i, j, word): return ( i >= 0 and j >= 0 and i < len(board) and j < len(board[0]) and board[i][j] == word ) def sol(board, i, j, k, word): if k == len(word): return True if validate(board, i, j, word[k]): c = board[i][j] board[i][j] = "#" k = k + 1 i = i + 1 if sol(board, i, j, k, word): return True else: i = i - 1 i = i - 1 if sol(board, i, j, k, word): return True else: i = i + 1 j = j + 1 if sol(board, i, j, k, word): return True else: j = j - 1 j = j - 1 if sol(board, i, j, k, word): return True else: j = j + 1 board[i][j] = c return False class Solution: def isWordExist(self, board, word): for i in range(len(board)): for j in range(len(board[0])): if board[i][j] == word[0]: if sol(board, i, j, 0, word): return True return False
FUNC_DEF RETURN VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN NUMBER CLASS_DEF FUNC_DEF 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 NUMBER VAR RETURN NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def isWordExist(self, board, word): m = len(board) n = len(board[0]) N = len(word) def dfs(i, j, pos, vis): if ( i < 0 or j < 0 or i >= m or j >= n or vis[i][j] == True or board[i][j] != word[pos] ): return 0 if pos == N - 1: return True vis[i][j] = True ans = ( dfs(i + 1, j, pos + 1, vis) + dfs(i, j + 1, pos + 1, vis) + dfs(i, j - 1, pos + 1, vis) + dfs(i - 1, j, pos + 1, vis) ) vis[i][j] = False return ans for i in range(m): for j in range(n): if board[i][j] == word[0]: vis = [[(False) for _ in range(n)] for _ in range(m)] if dfs(i, j, 0, vis): return 1 return 0
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR RETURN NUMBER IF VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR RETURN NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def helper(self, board, word, i, j, n, m, k): if k > len(word) - 1: return True if ( i < 0 or i >= n or j < 0 or j >= m or board[i][j] == "." or word[k] != board[i][j] ): return False board[i][j] = "." x = [0, 0, -1, 1] y = [-1, 1, 0, 0] temp = False for ind in range(4): temp = temp or self.helper(board, word, i + x[ind], j + y[ind], n, m, k + 1) board[i][j] = word[k] return temp def isWordExist(self, board, word): n = len(board) m = len(board[0]) for i in range(len(board)): for j in range(len(board[0])): if self.helper(board, word, i, j, n, m, 0): return True return False
CLASS_DEF FUNC_DEF IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR STRING VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR STRING ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def isWordExist(self, board, word): if not board: return m = len(board) n = len(board[0]) dirs = [[1, 0], [-1, 0], [0, 1], [0, -1]] visited = set() def dfs(word, k, i, j): if word[k] == board[i][j]: visited.add((i, j)) if len(visited) == len(word): return True for dir in dirs: newi = i + dir[0] newj = j + dir[1] if ( newi < len(board) and newi >= 0 and newj < len(board[0]) and newj >= 0 and (newi, newj) not in visited ): if dfs(word, k + 1, newi, newj): return True visited.remove((i, j)) return False for i in range(m): for j in range(n): if dfs(word, 0, i, j): return 1 return 0
CLASS_DEF FUNC_DEF IF VAR RETURN 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 ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN 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 NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def isWordExist(self, board, word): ar1 = [-1, 0, 1, 0] ar2 = [0, 1, 0, -1] n = len(board) m = len(board[0]) visit = [([0] * m) for _ in range(n)] def dfs(i, j, visit, board, word, idx): if idx == len(word): return True if i < 0 or j < 0 or i >= n or j >= m: return False if board[i][j] != word[idx]: return False if visit[i][j] == 1: return False visit[i][j] = 1 for k in range(4): r1 = i + ar1[k] c1 = j + ar2[k] if dfs(r1, c1, visit, board, word, idx + 1): return True visit[i][j] = 0 return False for i in range(n): for j in range(m): if board[i][j] == word[0] and dfs(i, j, visit, board, word, 0): return True return False
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER 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 FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def isWordExist(self, board, word): path = set() rows = len(board) cols = len(board[0]) def solve(r, l, i, rows, cols, path, word, board): if i == len(word): return True if ( r < 0 or l < 0 or r >= rows or l >= cols or (r, l) in path or word[i] != board[r][l] ): return False path.add((r, l)) ans = ( solve(r + 1, l, i + 1, rows, cols, path, word, board) or solve(r - 1, l, i + 1, rows, cols, path, word, board) or solve(r, l + 1, i + 1, rows, cols, path, word, board) or solve(r, l - 1, i + 1, rows, cols, path, word, board) ) path.remove((r, l)) return ans for r in range(rows): for l in range(cols): if solve(r, l, 0, rows, cols, path, word, board) == True: return True return False
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR 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 VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def isWordExist(self, board, word): def funcall(row, col, rows, cols, temp): if temp == word: return True if ( row < 0 or row >= rows or col < 0 or col >= cols or board[row][col] == -1 or len(temp) > len(word) or temp != word[: len(temp)] ): return False vari = board[row][col] board[row][col] = -1 if funcall(row + 1, col, rows, cols, temp + vari): board[row][col] = vari return True if funcall(row - 1, col, rows, cols, temp + vari): board[row][col] = vari return True if funcall(row, col + 1, rows, cols, temp + vari): board[row][col] = vari return True if funcall(row, col - 1, rows, cols, temp + vari): board[row][col] = vari return True board[row][col] = vari return False rows = len(board) cols = len(board[0]) temp = "" for i in range(rows): for j in range(cols): ans = funcall(i, j, rows, cols, temp) if ans: return True return False
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR RETURN NUMBER RETURN NUMBER
Given a 2D board of letters and a word. Check if the word exists in the board. The word can be constructed from letters of adjacent cells only. ie - horizontal or vertical neighbors. The same letter cell can not be used more than once. Example 1: Input: board = {{a,g,b,c},{q,e,e,l},{g,b,k,s}}, word = "geeks" Output: 1 Explanation: The board is- a g b c q e e l g b k s The letters which are used to make the "geeks" are colored. Example 2: Input: board = {{a,b,c,e},{s,f,c,s},{a,d,e,e}}, word = "sabfs" Output: 0 Explanation: The board is- a b c e s f c s a d e e Same letter can not be used twice hence ans is 0 Your Task: You don't need to read or print anything. Your task is to complete the function isWordExist() which takes board and word as input parameter and returns boolean value true if word can be found otherwise returns false. Expected Time Complexity: O(N * M * 4^{L}) where N = No. of rows in board, M = No. of columns in board, L = Length of word Expected Space Compelxity: O(L), L is length of word. Constraints: 1 ≤ N, M ≤ 100 1 ≤ L ≤ N*M
class Solution: def isWordExist(self, board, word): def findword(board, word, visited, row, col, arr): visited[row][col] = True if len(word) == 0: return True for i in arr: if ( row + i[0] >= 0 and row + i[0] < len(board) and col + i[1] >= 0 and col + i[1] < len(board[0]) and visited[row + i[0]][col + i[1]] == False and board[row + i[0]][col + i[1]] == word[0] ): if findword(board, word[1:], visited, row + i[0], col + i[1], arr): return True else: visited[row + i[0]][col + i[1]] = False visited = [[(False) for i in range(len(board[0]))] for j in range(len(board))] arr = [[-1, 0], [1, 0], [0, -1], [0, 1]] for i in range(len(board)): for j in range(len(board[0])): if board[i][j] == word[0]: if findword(board, word[1:], visited, i, j, arr): return True else: visited[i][j] = False return False
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR 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 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 VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER
*"You have a reason to leave this place, but I don't."* In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows. Both of them are given 2 strings — S and P — of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?. First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation. Once the above step is done, you are allowed to perform the following two operations on P any number of times: Pick an index 1 ≤ i ≤ N such that P_{i} is a vowel, and change P_{i} to any consonant. Pick an index 1 ≤ i ≤ N such that P_{i} is a consonant, and change P_{i} to any vowel. Note: The vowels are the 5 characters \{a, e, i, o, u\} — the other 21 are consonants. Find the minimum number of operations required to convert P into S. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains an integer N, denoting the length of strings S and P. - The second and third lines contain strings S and P respectively. ------ Output Format ------ For each test case, output in a single line the minimum number of operations required to convert P into S. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ - The sum of all $N$ across all test cases does not exceed $5*10^{5}$. $|S| = |P|$ - Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$. - Every $?$ present in $S$ and $P$ should be assigned the same character. ----- Sample Input 1 ------ 3 5 ab?c? aeg?k 6 abcde? ehio?? 4 abcd abcd ----- Sample Output 1 ------ 4 6 0 ----- explanation 1 ------ Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows: 1. Convert $P_{2} = e$ to $b$ in 1 operation. 2. Convert $P_{3} = g$ to $u$ in 1 operation. 3. Convert $P_{4} = u$ to $c$ in 1 operation. 4. Convert $P_{5} = k$ to $u$ in 1 operation. Test Case 3: The strings are already equal, so no operations are required.
t = int(input()) li = ["a", "e", "i", "o", "u"] for z in range(t): n = int(input()) mi = 1000000 s = input() p = input() for j in range(26): cha = chr(j + 97) c = 0 s1 = s.replace("?", cha) p1 = p.replace("?", cha) for i in range(n): if s1[i] != p1[i]: if (s1[i] in li) ^ (p1[i] in li): c = c + 1 else: c = c + 2 if c < mi: mi = c print(mi)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
*"You have a reason to leave this place, but I don't."* In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows. Both of them are given 2 strings — S and P — of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?. First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation. Once the above step is done, you are allowed to perform the following two operations on P any number of times: Pick an index 1 ≤ i ≤ N such that P_{i} is a vowel, and change P_{i} to any consonant. Pick an index 1 ≤ i ≤ N such that P_{i} is a consonant, and change P_{i} to any vowel. Note: The vowels are the 5 characters \{a, e, i, o, u\} — the other 21 are consonants. Find the minimum number of operations required to convert P into S. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains an integer N, denoting the length of strings S and P. - The second and third lines contain strings S and P respectively. ------ Output Format ------ For each test case, output in a single line the minimum number of operations required to convert P into S. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ - The sum of all $N$ across all test cases does not exceed $5*10^{5}$. $|S| = |P|$ - Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$. - Every $?$ present in $S$ and $P$ should be assigned the same character. ----- Sample Input 1 ------ 3 5 ab?c? aeg?k 6 abcde? ehio?? 4 abcd abcd ----- Sample Output 1 ------ 4 6 0 ----- explanation 1 ------ Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows: 1. Convert $P_{2} = e$ to $b$ in 1 operation. 2. Convert $P_{3} = g$ to $u$ in 1 operation. 3. Convert $P_{4} = u$ to $c$ in 1 operation. 4. Convert $P_{5} = k$ to $u$ in 1 operation. Test Case 3: The strings are already equal, so no operations are required.
for _ in range(int(input())): n = int(input()) s1 = input() p1 = input() k = [] vowel = "aeiou" alpha = "abcdefghijklmnopqrstuvwxyz" for x in alpha: s, p = s1, p1 s = s.replace("?", x) p = p.replace("?", x) c = 0 for i in range(n): if s[i] != p[i]: if ( s[i] in vowel and p[i] in vowel or s[i] not in vowel and p[i] not in vowel ): c += 2 else: c += 1 else: continue k.append(c) print(min(k))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
*"You have a reason to leave this place, but I don't."* In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows. Both of them are given 2 strings — S and P — of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?. First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation. Once the above step is done, you are allowed to perform the following two operations on P any number of times: Pick an index 1 ≤ i ≤ N such that P_{i} is a vowel, and change P_{i} to any consonant. Pick an index 1 ≤ i ≤ N such that P_{i} is a consonant, and change P_{i} to any vowel. Note: The vowels are the 5 characters \{a, e, i, o, u\} — the other 21 are consonants. Find the minimum number of operations required to convert P into S. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains an integer N, denoting the length of strings S and P. - The second and third lines contain strings S and P respectively. ------ Output Format ------ For each test case, output in a single line the minimum number of operations required to convert P into S. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ - The sum of all $N$ across all test cases does not exceed $5*10^{5}$. $|S| = |P|$ - Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$. - Every $?$ present in $S$ and $P$ should be assigned the same character. ----- Sample Input 1 ------ 3 5 ab?c? aeg?k 6 abcde? ehio?? 4 abcd abcd ----- Sample Output 1 ------ 4 6 0 ----- explanation 1 ------ Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows: 1. Convert $P_{2} = e$ to $b$ in 1 operation. 2. Convert $P_{3} = g$ to $u$ in 1 operation. 3. Convert $P_{4} = u$ to $c$ in 1 operation. 4. Convert $P_{5} = k$ to $u$ in 1 operation. Test Case 3: The strings are already equal, so no operations are required.
t = int(input()) for i in range(t): r = int(input()) a = input() b = input() ans = 0 up = 0 x = 0 mv = 0 mc = 0 v = ["a", "e", "i", "o", "u"] hh = [0] * 26 for i in range(r): if a[i] == "?" or b[i] == "?": if a[i] == "?" and b[i] == "?": up -= 1 up += 1 if a[i] in v or b[i] in v: x += 1 if a[i] != "?": hh[ord(a[i]) - ord("a")] += 1 if a[i] in v: mv = max(mv, hh[ord(a[i]) - ord("a")]) else: mc = max(mc, hh[ord(a[i]) - ord("a")]) if b[i] != "?": hh[ord(b[i]) - ord("a")] += 1 if b[i] in v: mv = max(mv, hh[ord(b[i]) - ord("a")]) else: mc = max(mc, hh[ord(b[i]) - ord("a")]) elif a[i] == b[i]: ans += 0 elif a[i] in v and b[i] in v: ans += 2 elif a[i] not in v and b[i] not in v: ans += 2 else: ans += 1 ans = ans + min(2 * (x - mv) + (up - x), 2 * (up - x - mc) + x) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST STRING STRING STRING STRING STRING ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING IF VAR VAR STRING VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR STRING VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF VAR VAR STRING VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
*"You have a reason to leave this place, but I don't."* In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows. Both of them are given 2 strings — S and P — of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?. First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation. Once the above step is done, you are allowed to perform the following two operations on P any number of times: Pick an index 1 ≤ i ≤ N such that P_{i} is a vowel, and change P_{i} to any consonant. Pick an index 1 ≤ i ≤ N such that P_{i} is a consonant, and change P_{i} to any vowel. Note: The vowels are the 5 characters \{a, e, i, o, u\} — the other 21 are consonants. Find the minimum number of operations required to convert P into S. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains an integer N, denoting the length of strings S and P. - The second and third lines contain strings S and P respectively. ------ Output Format ------ For each test case, output in a single line the minimum number of operations required to convert P into S. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ - The sum of all $N$ across all test cases does not exceed $5*10^{5}$. $|S| = |P|$ - Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$. - Every $?$ present in $S$ and $P$ should be assigned the same character. ----- Sample Input 1 ------ 3 5 ab?c? aeg?k 6 abcde? ehio?? 4 abcd abcd ----- Sample Output 1 ------ 4 6 0 ----- explanation 1 ------ Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows: 1. Convert $P_{2} = e$ to $b$ in 1 operation. 2. Convert $P_{3} = g$ to $u$ in 1 operation. 3. Convert $P_{4} = u$ to $c$ in 1 operation. 4. Convert $P_{5} = k$ to $u$ in 1 operation. Test Case 3: The strings are already equal, so no operations are required.
def main(): t = int(input()) while t > 0: n = int(input()) s = str(input()) p = str(input()) ans = n * 2 + 1 vowels = ["a", "e", "i", "o", "u"] for i in range(97, 123): Operation = 0 temp_s, temp_p = "", "" temp_s = s.replace("?", chr(i)) temp_p = p.replace("?", chr(i)) for j in range(n): if temp_s[j] != temp_p[j]: if temp_s[j] in vowels and temp_p[j] in vowels: Operation += 2 elif temp_s[j] not in vowels and temp_p[j] not in vowels: Operation += 2 else: Operation += 1 ans = min(ans, Operation) print(ans) t -= 1 main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST STRING STRING STRING STRING STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR
*"You have a reason to leave this place, but I don't."* In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows. Both of them are given 2 strings — S and P — of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?. First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation. Once the above step is done, you are allowed to perform the following two operations on P any number of times: Pick an index 1 ≤ i ≤ N such that P_{i} is a vowel, and change P_{i} to any consonant. Pick an index 1 ≤ i ≤ N such that P_{i} is a consonant, and change P_{i} to any vowel. Note: The vowels are the 5 characters \{a, e, i, o, u\} — the other 21 are consonants. Find the minimum number of operations required to convert P into S. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains an integer N, denoting the length of strings S and P. - The second and third lines contain strings S and P respectively. ------ Output Format ------ For each test case, output in a single line the minimum number of operations required to convert P into S. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ - The sum of all $N$ across all test cases does not exceed $5*10^{5}$. $|S| = |P|$ - Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$. - Every $?$ present in $S$ and $P$ should be assigned the same character. ----- Sample Input 1 ------ 3 5 ab?c? aeg?k 6 abcde? ehio?? 4 abcd abcd ----- Sample Output 1 ------ 4 6 0 ----- explanation 1 ------ Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows: 1. Convert $P_{2} = e$ to $b$ in 1 operation. 2. Convert $P_{3} = g$ to $u$ in 1 operation. 3. Convert $P_{4} = u$ to $c$ in 1 operation. 4. Convert $P_{5} = k$ to $u$ in 1 operation. Test Case 3: The strings are already equal, so no operations are required.
t = int(input()) while t != 0: t = t - 1 n = int(input()) s = input() p = input() ov = "aeiou" c = 0 v = 0 d = dict() kk = 0 for i in range(n): if s[i] == "?" and p[i] == "?": continue if s[i] == "?" or p[i] == "?": if s[i] != "?": if d.get(s[i]): d[s[i]] += 1 else: d[s[i]] = 1 if s[i] in ov: v = v + 1 else: c = c + 1 else: if d.get(p[i]): d[p[i]] += 1 else: d[p[i]] = 1 if p[i] in ov: v = v + 1 else: c = c + 1 elif s[i] != p[i]: if s[i] in ov and p[i] in ov: kk = kk + 2 elif s[i] in ov and p[i] not in ov: kk = kk + 1 elif s[i] not in ov and p[i] in ov: kk = kk + 1 else: kk = kk + 2 mv = 0 mc = 0 for i in d: if i in ov: if mv < d[i]: mv = d[i] elif mc < d[i]: mc = d[i] print(min((v - mv) * 2 + c, (c - mc) * 2 + v) + kk)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING IF VAR VAR STRING VAR VAR STRING IF VAR VAR STRING IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR
*"You have a reason to leave this place, but I don't."* In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows. Both of them are given 2 strings — S and P — of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?. First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation. Once the above step is done, you are allowed to perform the following two operations on P any number of times: Pick an index 1 ≤ i ≤ N such that P_{i} is a vowel, and change P_{i} to any consonant. Pick an index 1 ≤ i ≤ N such that P_{i} is a consonant, and change P_{i} to any vowel. Note: The vowels are the 5 characters \{a, e, i, o, u\} — the other 21 are consonants. Find the minimum number of operations required to convert P into S. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains an integer N, denoting the length of strings S and P. - The second and third lines contain strings S and P respectively. ------ Output Format ------ For each test case, output in a single line the minimum number of operations required to convert P into S. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ - The sum of all $N$ across all test cases does not exceed $5*10^{5}$. $|S| = |P|$ - Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$. - Every $?$ present in $S$ and $P$ should be assigned the same character. ----- Sample Input 1 ------ 3 5 ab?c? aeg?k 6 abcde? ehio?? 4 abcd abcd ----- Sample Output 1 ------ 4 6 0 ----- explanation 1 ------ Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows: 1. Convert $P_{2} = e$ to $b$ in 1 operation. 2. Convert $P_{3} = g$ to $u$ in 1 operation. 3. Convert $P_{4} = u$ to $c$ in 1 operation. 4. Convert $P_{5} = k$ to $u$ in 1 operation. Test Case 3: The strings are already equal, so no operations are required.
for _ in range(int(input())): n = int(input()) s = input() p = input() if s == p: print(0) continue def func(c, s, p, n): v = set(["a", "e", "i", "o", "u"]) ans = 0 for i in range(n): a = s[i] b = p[i] if s[i] == "?": a = c if p[i] == "?": b = c if a == b: continue if a in v and b in v: ans += 2 elif a not in v and b not in v: ans += 2 elif a in v and b not in v: ans += 1 elif a not in v and b in v: ans += 1 return ans val = 1000000000 for i in range(0, 26): val = min(val, func(chr(ord("a") + i), s, p, n)) print(val)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR LIST STRING STRING STRING STRING STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR IF VAR VAR STRING ASSIGN VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
*"You have a reason to leave this place, but I don't."* In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows. Both of them are given 2 strings — S and P — of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?. First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation. Once the above step is done, you are allowed to perform the following two operations on P any number of times: Pick an index 1 ≤ i ≤ N such that P_{i} is a vowel, and change P_{i} to any consonant. Pick an index 1 ≤ i ≤ N such that P_{i} is a consonant, and change P_{i} to any vowel. Note: The vowels are the 5 characters \{a, e, i, o, u\} — the other 21 are consonants. Find the minimum number of operations required to convert P into S. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains an integer N, denoting the length of strings S and P. - The second and third lines contain strings S and P respectively. ------ Output Format ------ For each test case, output in a single line the minimum number of operations required to convert P into S. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ - The sum of all $N$ across all test cases does not exceed $5*10^{5}$. $|S| = |P|$ - Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$. - Every $?$ present in $S$ and $P$ should be assigned the same character. ----- Sample Input 1 ------ 3 5 ab?c? aeg?k 6 abcde? ehio?? 4 abcd abcd ----- Sample Output 1 ------ 4 6 0 ----- explanation 1 ------ Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows: 1. Convert $P_{2} = e$ to $b$ in 1 operation. 2. Convert $P_{3} = g$ to $u$ in 1 operation. 3. Convert $P_{4} = u$ to $c$ in 1 operation. 4. Convert $P_{5} = k$ to $u$ in 1 operation. Test Case 3: The strings are already equal, so no operations are required.
from sys import stdin, stdout inp_num = lambda: int(input()) inp_lis = lambda: list(map(int, input().split())) vow = {"a", "e", "i", "o", "u"} for _ in range(inp_num()): n = inp_num() s = input() s1 = input() d = {} nv = 0 nc = 0 sc = 0 ma = 0 li = list(s + s1) for i in li: if i != "?": d[i] = 0 for i in range(n): a = s[i] == "a" or s[i] == "e" or s[i] == "i" or s[i] == "o" or s[i] == "u" b = s1[i] == "a" or s1[i] == "e" or s1[i] == "i" or s1[i] == "o" or s1[i] == "u" if s[i] == "?" and s1[i] == "?": continue elif s[i] == "?" or s1[i] == "?": if s[i] == "?": if b: nv += 1 else: nc += 1 d[s1[i]] += 1 else: if a: nv += 1 else: nc += 1 d[s[i]] += 1 elif a: if b: if s[i] != s1[i]: sc += 2 else: continue else: sc += 1 elif b: sc += 1 elif s1[i] == s[i]: continue else: sc += 2 p = q = 0 for i in d.keys(): if i in vow: if p < d[i]: p = d[i] elif q < d[i]: q = d[i] vowe = (nv - p) * 2 + nc cons = nv + (nc - q) * 2 print(sc + min(vowe, cons))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING STRING STRING STRING STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR VAR IF VAR STRING ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR VAR STRING IF VAR VAR STRING VAR VAR STRING IF VAR VAR STRING VAR VAR STRING IF VAR VAR STRING IF VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR IF VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR
*"You have a reason to leave this place, but I don't."* In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows. Both of them are given 2 strings — S and P — of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?. First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation. Once the above step is done, you are allowed to perform the following two operations on P any number of times: Pick an index 1 ≤ i ≤ N such that P_{i} is a vowel, and change P_{i} to any consonant. Pick an index 1 ≤ i ≤ N such that P_{i} is a consonant, and change P_{i} to any vowel. Note: The vowels are the 5 characters \{a, e, i, o, u\} — the other 21 are consonants. Find the minimum number of operations required to convert P into S. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains an integer N, denoting the length of strings S and P. - The second and third lines contain strings S and P respectively. ------ Output Format ------ For each test case, output in a single line the minimum number of operations required to convert P into S. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ - The sum of all $N$ across all test cases does not exceed $5*10^{5}$. $|S| = |P|$ - Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$. - Every $?$ present in $S$ and $P$ should be assigned the same character. ----- Sample Input 1 ------ 3 5 ab?c? aeg?k 6 abcde? ehio?? 4 abcd abcd ----- Sample Output 1 ------ 4 6 0 ----- explanation 1 ------ Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows: 1. Convert $P_{2} = e$ to $b$ in 1 operation. 2. Convert $P_{3} = g$ to $u$ in 1 operation. 3. Convert $P_{4} = u$ to $c$ in 1 operation. 4. Convert $P_{5} = k$ to $u$ in 1 operation. Test Case 3: The strings are already equal, so no operations are required.
def solution(): N = int(input()) S = input() P = input() vowels = {"a", "e", "i", "o", "u"} minO = float("inf") for char in range(97, 122): alpha = chr(char) curO = 0 for s, p in zip(S, P): if s == "?": s = alpha if p == "?": p = alpha if s == p: continue curO += 1 if (s in vowels) != (p in vowels) else 2 minO = min(curO, minO) print(minO) for _ in range(int(input())): solution()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING STRING STRING STRING STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR STRING ASSIGN VAR VAR IF VAR STRING ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
*"You have a reason to leave this place, but I don't."* In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows. Both of them are given 2 strings — S and P — of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?. First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation. Once the above step is done, you are allowed to perform the following two operations on P any number of times: Pick an index 1 ≤ i ≤ N such that P_{i} is a vowel, and change P_{i} to any consonant. Pick an index 1 ≤ i ≤ N such that P_{i} is a consonant, and change P_{i} to any vowel. Note: The vowels are the 5 characters \{a, e, i, o, u\} — the other 21 are consonants. Find the minimum number of operations required to convert P into S. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains an integer N, denoting the length of strings S and P. - The second and third lines contain strings S and P respectively. ------ Output Format ------ For each test case, output in a single line the minimum number of operations required to convert P into S. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ - The sum of all $N$ across all test cases does not exceed $5*10^{5}$. $|S| = |P|$ - Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$. - Every $?$ present in $S$ and $P$ should be assigned the same character. ----- Sample Input 1 ------ 3 5 ab?c? aeg?k 6 abcde? ehio?? 4 abcd abcd ----- Sample Output 1 ------ 4 6 0 ----- explanation 1 ------ Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows: 1. Convert $P_{2} = e$ to $b$ in 1 operation. 2. Convert $P_{3} = g$ to $u$ in 1 operation. 3. Convert $P_{4} = u$ to $c$ in 1 operation. 4. Convert $P_{5} = k$ to $u$ in 1 operation. Test Case 3: The strings are already equal, so no operations are required.
for _ in range(int(input())): n = int(input()) s1 = input() s2 = input() l = ["a", "e", "i", "o", "u"] c1, c2, l1, c = 0, 0, [], 0 for i in range(n): if s1[i] != s2[i]: if s1[i] == "?": l1.append(ord(s2[i])) if s2[i] in l: c1 += 1 else: c2 += 1 elif s2[i] == "?": l1.append(ord(s1[i])) if s1[i] in l: c1 += 1 else: c2 += 1 elif s1[i] in l and s2[i] in l or s1[i] not in l and s2[i] not in l: c += 2 else: c += 1 k1, k2, k3, k4 = 0, 0, 0, 0 if l1 != []: l1.sort() d, c3 = {(0): 0}, 1 for i in range(len(l1) - 1): if l1[i] == l1[i + 1]: c3 += 1 else: d[l1[i]] = c3 c3 = 1 d[l1[len(l1) - 1]] = c3 k1, k2, k3, k4 = 0, 0, 0, 0 for i in d: if chr(i) in l: if d[i] > k2: k1 = i k2 = d[i] elif d[i] > k4: k3 = i k4 = d[i] print(c + min((c1 - d[k1]) * 2 + c2, c1 + (c2 - d[k3]) * 2))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING ASSIGN VAR VAR VAR VAR NUMBER NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER IF VAR LIST EXPR FUNC_CALL VAR ASSIGN VAR VAR DICT NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR NUMBER
*"You have a reason to leave this place, but I don't."* In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows. Both of them are given 2 strings — S and P — of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?. First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation. Once the above step is done, you are allowed to perform the following two operations on P any number of times: Pick an index 1 ≤ i ≤ N such that P_{i} is a vowel, and change P_{i} to any consonant. Pick an index 1 ≤ i ≤ N such that P_{i} is a consonant, and change P_{i} to any vowel. Note: The vowels are the 5 characters \{a, e, i, o, u\} — the other 21 are consonants. Find the minimum number of operations required to convert P into S. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains an integer N, denoting the length of strings S and P. - The second and third lines contain strings S and P respectively. ------ Output Format ------ For each test case, output in a single line the minimum number of operations required to convert P into S. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ - The sum of all $N$ across all test cases does not exceed $5*10^{5}$. $|S| = |P|$ - Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$. - Every $?$ present in $S$ and $P$ should be assigned the same character. ----- Sample Input 1 ------ 3 5 ab?c? aeg?k 6 abcde? ehio?? 4 abcd abcd ----- Sample Output 1 ------ 4 6 0 ----- explanation 1 ------ Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows: 1. Convert $P_{2} = e$ to $b$ in 1 operation. 2. Convert $P_{3} = g$ to $u$ in 1 operation. 3. Convert $P_{4} = u$ to $c$ in 1 operation. 4. Convert $P_{5} = k$ to $u$ in 1 operation. Test Case 3: The strings are already equal, so no operations are required.
for _ in range(int(input())): n = int(input()) A = input() B = input() nc = 0 ans = 0 nv = 0 f = {} d = {} W = {"a": 1, "e": 1, "i": 1, "o": 1, "u": 1} for i in range(n): if A[i] == "?" and B[i] != "?": try: W[B[i]] nv += 1 try: f[B[i]] += 1 except: f[B[i]] = 1 except: nc += 1 try: d[B[i]] += 1 except: d[B[i]] = 1 elif B[i] == "?" and A[i] != "?": try: W[A[i]] nv += 1 try: f[A[i]] += 1 except: f[A[i]] = 1 except: nc += 1 try: d[A[i]] += 1 except: d[A[i]] = 1 elif A[i] != "?" and B[i] != "?": if A[i] == B[i]: pass else: try: W[A[i]] W[B[i]] ans += 2 except: try: W[A[i]] ans += 1 except: try: W[B[i]] ans += 1 except: ans += 2 if nc != 0: ke = max(d, key=d.get) if nv != 0: ky = max(f, key=f.get) if nc > 0 and nv > 0: ans += min(nv + (nc - d[ke]) * 2, nc + (nv - f[ky]) * 2) elif nc > 0: ans += min(nv + (nc - d[ke]) * 2, nc + nv * 2) elif nv > 0: ans += min(nv + nc * 2, nc + (nv - f[ky]) * 2) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING EXPR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR STRING VAR VAR STRING EXPR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR STRING VAR VAR STRING IF VAR VAR VAR VAR EXPR VAR VAR VAR EXPR VAR VAR VAR VAR NUMBER EXPR VAR VAR VAR VAR NUMBER EXPR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
*"You have a reason to leave this place, but I don't."* In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows. Both of them are given 2 strings — S and P — of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?. First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation. Once the above step is done, you are allowed to perform the following two operations on P any number of times: Pick an index 1 ≤ i ≤ N such that P_{i} is a vowel, and change P_{i} to any consonant. Pick an index 1 ≤ i ≤ N such that P_{i} is a consonant, and change P_{i} to any vowel. Note: The vowels are the 5 characters \{a, e, i, o, u\} — the other 21 are consonants. Find the minimum number of operations required to convert P into S. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains an integer N, denoting the length of strings S and P. - The second and third lines contain strings S and P respectively. ------ Output Format ------ For each test case, output in a single line the minimum number of operations required to convert P into S. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ - The sum of all $N$ across all test cases does not exceed $5*10^{5}$. $|S| = |P|$ - Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$. - Every $?$ present in $S$ and $P$ should be assigned the same character. ----- Sample Input 1 ------ 3 5 ab?c? aeg?k 6 abcde? ehio?? 4 abcd abcd ----- Sample Output 1 ------ 4 6 0 ----- explanation 1 ------ Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows: 1. Convert $P_{2} = e$ to $b$ in 1 operation. 2. Convert $P_{3} = g$ to $u$ in 1 operation. 3. Convert $P_{4} = u$ to $c$ in 1 operation. 4. Convert $P_{5} = k$ to $u$ in 1 operation. Test Case 3: The strings are already equal, so no operations are required.
t = int(input()) for _ in range(t): n = int(input()) s = input() p = input() count1 = 0 vowel = {"a", "e", "i", "o", "u"} arr = [] for j in range(n): if s[j] == "?" and p[j] == "?": continue elif s[j] != "?" and p[j] != "?": if s[j] != p[j]: if s[j] in vowel: if p[j] in vowel: count1 += 2 else: count1 += 1 elif p[j] in vowel: count1 += 1 else: count1 += 2 elif s[j] == "?" or p[j] == "?": arr.append(j) if len(arr) != 0: count2 = 2 * n for i in range(97, 123): char = chr(i) count = 0 for k in arr: flag = s[k] if s[k] == "?": flag = p[k] if flag != char: if flag in vowel: if char in vowel: count += 2 else: count += 1 elif char in vowel: count += 1 else: count += 2 count2 = min(count2, count) count1 += count2 print(count1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR STRING STRING STRING STRING STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING IF VAR VAR STRING VAR VAR STRING IF VAR VAR VAR VAR IF VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR IF VAR VAR IF VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
*"You have a reason to leave this place, but I don't."* In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows. Both of them are given 2 strings — S and P — of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?. First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation. Once the above step is done, you are allowed to perform the following two operations on P any number of times: Pick an index 1 ≤ i ≤ N such that P_{i} is a vowel, and change P_{i} to any consonant. Pick an index 1 ≤ i ≤ N such that P_{i} is a consonant, and change P_{i} to any vowel. Note: The vowels are the 5 characters \{a, e, i, o, u\} — the other 21 are consonants. Find the minimum number of operations required to convert P into S. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains an integer N, denoting the length of strings S and P. - The second and third lines contain strings S and P respectively. ------ Output Format ------ For each test case, output in a single line the minimum number of operations required to convert P into S. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ - The sum of all $N$ across all test cases does not exceed $5*10^{5}$. $|S| = |P|$ - Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$. - Every $?$ present in $S$ and $P$ should be assigned the same character. ----- Sample Input 1 ------ 3 5 ab?c? aeg?k 6 abcde? ehio?? 4 abcd abcd ----- Sample Output 1 ------ 4 6 0 ----- explanation 1 ------ Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows: 1. Convert $P_{2} = e$ to $b$ in 1 operation. 2. Convert $P_{3} = g$ to $u$ in 1 operation. 3. Convert $P_{4} = u$ to $c$ in 1 operation. 4. Convert $P_{5} = k$ to $u$ in 1 operation. Test Case 3: The strings are already equal, so no operations are required.
for i in range(int(input())): A = ["a", "e", "i", "o", "u"] n = int(input()) a = input() b = input() ans = 10**9 + 1 for i in range(97, 123): char = chr(i) x = a y = b p = x.replace("?", char) s = y.replace("?", char) res = 0 for i in range(n): if s[i] != p[i]: if s[i] in A: if p[i] in A: res += 2 else: res += 1 elif p[i] not in A: res += 2 else: res += 1 res = min(res, ans) ans = res print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
*"You have a reason to leave this place, but I don't."* In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows. Both of them are given 2 strings — S and P — of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?. First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation. Once the above step is done, you are allowed to perform the following two operations on P any number of times: Pick an index 1 ≤ i ≤ N such that P_{i} is a vowel, and change P_{i} to any consonant. Pick an index 1 ≤ i ≤ N such that P_{i} is a consonant, and change P_{i} to any vowel. Note: The vowels are the 5 characters \{a, e, i, o, u\} — the other 21 are consonants. Find the minimum number of operations required to convert P into S. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains an integer N, denoting the length of strings S and P. - The second and third lines contain strings S and P respectively. ------ Output Format ------ For each test case, output in a single line the minimum number of operations required to convert P into S. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ - The sum of all $N$ across all test cases does not exceed $5*10^{5}$. $|S| = |P|$ - Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$. - Every $?$ present in $S$ and $P$ should be assigned the same character. ----- Sample Input 1 ------ 3 5 ab?c? aeg?k 6 abcde? ehio?? 4 abcd abcd ----- Sample Output 1 ------ 4 6 0 ----- explanation 1 ------ Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows: 1. Convert $P_{2} = e$ to $b$ in 1 operation. 2. Convert $P_{3} = g$ to $u$ in 1 operation. 3. Convert $P_{4} = u$ to $c$ in 1 operation. 4. Convert $P_{5} = k$ to $u$ in 1 operation. Test Case 3: The strings are already equal, so no operations are required.
vowel = "aeiou" for _ in range(int(input())): length = int(input()) s = input() p = input() counts = [] for letter in range(97, 123): s1 = s.replace("?", chr(letter), length) p1 = p.replace("?", chr(letter), length) count = 0 for ind in range(length): if s1[ind] == p1[ind]: pass elif (s1[ind] in vowel) == (p1[ind] in vowel): count += 2 else: count += 1 counts.append(count) print(min(counts))
ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
*"You have a reason to leave this place, but I don't."* In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows. Both of them are given 2 strings — S and P — of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?. First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation. Once the above step is done, you are allowed to perform the following two operations on P any number of times: Pick an index 1 ≤ i ≤ N such that P_{i} is a vowel, and change P_{i} to any consonant. Pick an index 1 ≤ i ≤ N such that P_{i} is a consonant, and change P_{i} to any vowel. Note: The vowels are the 5 characters \{a, e, i, o, u\} — the other 21 are consonants. Find the minimum number of operations required to convert P into S. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains an integer N, denoting the length of strings S and P. - The second and third lines contain strings S and P respectively. ------ Output Format ------ For each test case, output in a single line the minimum number of operations required to convert P into S. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ - The sum of all $N$ across all test cases does not exceed $5*10^{5}$. $|S| = |P|$ - Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$. - Every $?$ present in $S$ and $P$ should be assigned the same character. ----- Sample Input 1 ------ 3 5 ab?c? aeg?k 6 abcde? ehio?? 4 abcd abcd ----- Sample Output 1 ------ 4 6 0 ----- explanation 1 ------ Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows: 1. Convert $P_{2} = e$ to $b$ in 1 operation. 2. Convert $P_{3} = g$ to $u$ in 1 operation. 3. Convert $P_{4} = u$ to $c$ in 1 operation. 4. Convert $P_{5} = k$ to $u$ in 1 operation. Test Case 3: The strings are already equal, so no operations are required.
vowels = ["a", "e", "i", "o", "u"] def changeOpR(a, b): if a == b: return 0 isVowel = [True if a in vowels else False, True if b in vowels else False] if isVowel[0] == isVowel[1]: return 2 return 1 def extra(A): possibleAns = 1000000 iterate = "abcdefghijklmnopqrstuvwxyz" for i in iterate: temp = 0 for j in A: temp += changeOpR(i, j) possibleAns = min(possibleAns, temp) return possibleAns for _ in range(int(input())): N = int(input()) S = input() P = input() count = 0 A = [] for i in range(N): if S[i] == "?" and P[i] == "?": continue if S[i] == "?": A.append(P[i]) elif P[i] == "?": A.append(S[i]) else: count += changeOpR(S[i], P[i]) count += extra(A) print(count)
ASSIGN VAR LIST STRING STRING STRING STRING STRING FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR LIST VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
*"You have a reason to leave this place, but I don't."* In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows. Both of them are given 2 strings — S and P — of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?. First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation. Once the above step is done, you are allowed to perform the following two operations on P any number of times: Pick an index 1 ≤ i ≤ N such that P_{i} is a vowel, and change P_{i} to any consonant. Pick an index 1 ≤ i ≤ N such that P_{i} is a consonant, and change P_{i} to any vowel. Note: The vowels are the 5 characters \{a, e, i, o, u\} — the other 21 are consonants. Find the minimum number of operations required to convert P into S. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains an integer N, denoting the length of strings S and P. - The second and third lines contain strings S and P respectively. ------ Output Format ------ For each test case, output in a single line the minimum number of operations required to convert P into S. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ - The sum of all $N$ across all test cases does not exceed $5*10^{5}$. $|S| = |P|$ - Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$. - Every $?$ present in $S$ and $P$ should be assigned the same character. ----- Sample Input 1 ------ 3 5 ab?c? aeg?k 6 abcde? ehio?? 4 abcd abcd ----- Sample Output 1 ------ 4 6 0 ----- explanation 1 ------ Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows: 1. Convert $P_{2} = e$ to $b$ in 1 operation. 2. Convert $P_{3} = g$ to $u$ in 1 operation. 3. Convert $P_{4} = u$ to $c$ in 1 operation. 4. Convert $P_{5} = k$ to $u$ in 1 operation. Test Case 3: The strings are already equal, so no operations are required.
vowels = ["a", "e", "i", "o", "u"] def changeOpR(a, b): if a == b: return 0 isVowel = [True if a in vowels else False, True if b in vowels else False] if isVowel[0] == isVowel[1]: return 2 return 1 def extra(A): possibleAns = 1000000 for i in range(26): temp = chr(97 + i) sample = A countA = 0 for j in range(len(sample)): x, y = sample[j] if x == "?": x = temp if y == "?": y = temp countA += changeOpR(x, y) possibleAns = min(possibleAns, countA) return possibleAns for _ in range(int(input())): N = int(input()) S = input() P = input() count = 0 A = [] for i in range(N): if S[i] == "?" and P[i] == "?": continue if S[i] == "?" or P[i] == "?": A.append([S[i], P[i]]) else: count += changeOpR(S[i], P[i]) count += extra(A) print(count)
ASSIGN VAR LIST STRING STRING STRING STRING STRING FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR LIST VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR STRING ASSIGN VAR VAR IF VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING IF VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
*"You have a reason to leave this place, but I don't."* In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows. Both of them are given 2 strings — S and P — of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?. First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation. Once the above step is done, you are allowed to perform the following two operations on P any number of times: Pick an index 1 ≤ i ≤ N such that P_{i} is a vowel, and change P_{i} to any consonant. Pick an index 1 ≤ i ≤ N such that P_{i} is a consonant, and change P_{i} to any vowel. Note: The vowels are the 5 characters \{a, e, i, o, u\} — the other 21 are consonants. Find the minimum number of operations required to convert P into S. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains an integer N, denoting the length of strings S and P. - The second and third lines contain strings S and P respectively. ------ Output Format ------ For each test case, output in a single line the minimum number of operations required to convert P into S. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ - The sum of all $N$ across all test cases does not exceed $5*10^{5}$. $|S| = |P|$ - Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$. - Every $?$ present in $S$ and $P$ should be assigned the same character. ----- Sample Input 1 ------ 3 5 ab?c? aeg?k 6 abcde? ehio?? 4 abcd abcd ----- Sample Output 1 ------ 4 6 0 ----- explanation 1 ------ Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows: 1. Convert $P_{2} = e$ to $b$ in 1 operation. 2. Convert $P_{3} = g$ to $u$ in 1 operation. 3. Convert $P_{4} = u$ to $c$ in 1 operation. 4. Convert $P_{5} = k$ to $u$ in 1 operation. Test Case 3: The strings are already equal, so no operations are required.
import sys def fun(a, b, n, car): c = 0 s = set(["a", "e", "i", "o", "u"]) f1 = 0 f2 = 0 for i in range(n): if a[i] == "?": a[i] = car f1 = 1 if b[i] == "?": b[i] = car f2 = 1 if a[i] != b[i]: if a[i] in s and b[i] in s or a[i] not in s and b[i] not in s: c += 2 else: c += 1 if f1 == 1: a[i] = "?" f1 = 0 if f2 == 1: b[i] = "?" f2 = 0 return c for _ in range(int(input())): n = int(sys.stdin.readline()) a = list(sys.stdin.readline()) b = list(sys.stdin.readline()) ans = 2 * n + 1 for j in range(26): ans = min(ans, fun(a, b, n, chr(j + 97))) sys.stdout.write(str(ans) + "\n")
IMPORT FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST STRING STRING STRING STRING STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
*"You have a reason to leave this place, but I don't."* In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows. Both of them are given 2 strings — S and P — of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?. First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation. Once the above step is done, you are allowed to perform the following two operations on P any number of times: Pick an index 1 ≤ i ≤ N such that P_{i} is a vowel, and change P_{i} to any consonant. Pick an index 1 ≤ i ≤ N such that P_{i} is a consonant, and change P_{i} to any vowel. Note: The vowels are the 5 characters \{a, e, i, o, u\} — the other 21 are consonants. Find the minimum number of operations required to convert P into S. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains an integer N, denoting the length of strings S and P. - The second and third lines contain strings S and P respectively. ------ Output Format ------ For each test case, output in a single line the minimum number of operations required to convert P into S. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ - The sum of all $N$ across all test cases does not exceed $5*10^{5}$. $|S| = |P|$ - Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$. - Every $?$ present in $S$ and $P$ should be assigned the same character. ----- Sample Input 1 ------ 3 5 ab?c? aeg?k 6 abcde? ehio?? 4 abcd abcd ----- Sample Output 1 ------ 4 6 0 ----- explanation 1 ------ Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows: 1. Convert $P_{2} = e$ to $b$ in 1 operation. 2. Convert $P_{3} = g$ to $u$ in 1 operation. 3. Convert $P_{4} = u$ to $c$ in 1 operation. 4. Convert $P_{5} = k$ to $u$ in 1 operation. Test Case 3: The strings are already equal, so no operations are required.
def check_vowel(x): if x == "a" or x == "e" or x == "i" or x == "o" or x == "u": return True return False def get_min_move(s, p, N): min_move = 99999999999999 for ch in "abcdefghijklmnopqrstuvwxyz": s_arr = s p_arr = p s_arr = s_arr.replace("?", ch) p_arr = p_arr.replace("?", ch) move = 0 for i in range(N): if s_arr[i] == p_arr[i]: pass elif s_arr[i] in "aeiou" and p_arr[i] not in "aeiou": move += 1 elif p_arr[i] in "aeiou" and s_arr[i] not in "aeiou": move += 1 else: move += 2 if move == 0: min_move = move break else: min_move = min(move, min_move) return min_move for i in range(int(input())): N = int(input()) s_arr = input() p_arr = input() min_move = get_min_move(s_arr, p_arr, N) print(min_move)
FUNC_DEF IF VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR STRING VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
*"You have a reason to leave this place, but I don't."* In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows. Both of them are given 2 strings — S and P — of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?. First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation. Once the above step is done, you are allowed to perform the following two operations on P any number of times: Pick an index 1 ≤ i ≤ N such that P_{i} is a vowel, and change P_{i} to any consonant. Pick an index 1 ≤ i ≤ N such that P_{i} is a consonant, and change P_{i} to any vowel. Note: The vowels are the 5 characters \{a, e, i, o, u\} — the other 21 are consonants. Find the minimum number of operations required to convert P into S. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains an integer N, denoting the length of strings S and P. - The second and third lines contain strings S and P respectively. ------ Output Format ------ For each test case, output in a single line the minimum number of operations required to convert P into S. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ - The sum of all $N$ across all test cases does not exceed $5*10^{5}$. $|S| = |P|$ - Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$. - Every $?$ present in $S$ and $P$ should be assigned the same character. ----- Sample Input 1 ------ 3 5 ab?c? aeg?k 6 abcde? ehio?? 4 abcd abcd ----- Sample Output 1 ------ 4 6 0 ----- explanation 1 ------ Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows: 1. Convert $P_{2} = e$ to $b$ in 1 operation. 2. Convert $P_{3} = g$ to $u$ in 1 operation. 3. Convert $P_{4} = u$ to $c$ in 1 operation. 4. Convert $P_{5} = k$ to $u$ in 1 operation. Test Case 3: The strings are already equal, so no operations are required.
ts = int(input()) def minimum(l, l1): score = [] al = [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", ] a = l b = l1 vowels = ["a", "e", "i", "o", "u"] for j in range(25): c = 0 l = a.replace("?", al[j]) l1 = b.replace("?", al[j]) for i in range(len(l)): if l[i] != l1[i]: c += 1 if l1[i] not in vowels: if l[i] not in vowels: c += 1 elif l1[i] in vowels: if l[i] in vowels: c += 1 score.append(c) return min(score) for i in range(ts): c = 0 l = [] l1 = [] n = int(input()) s1 = input() s2 = input() print(minimum(s1, s2))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
*"You have a reason to leave this place, but I don't."* In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows. Both of them are given 2 strings — S and P — of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?. First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation. Once the above step is done, you are allowed to perform the following two operations on P any number of times: Pick an index 1 ≤ i ≤ N such that P_{i} is a vowel, and change P_{i} to any consonant. Pick an index 1 ≤ i ≤ N such that P_{i} is a consonant, and change P_{i} to any vowel. Note: The vowels are the 5 characters \{a, e, i, o, u\} — the other 21 are consonants. Find the minimum number of operations required to convert P into S. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains an integer N, denoting the length of strings S and P. - The second and third lines contain strings S and P respectively. ------ Output Format ------ For each test case, output in a single line the minimum number of operations required to convert P into S. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ - The sum of all $N$ across all test cases does not exceed $5*10^{5}$. $|S| = |P|$ - Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$. - Every $?$ present in $S$ and $P$ should be assigned the same character. ----- Sample Input 1 ------ 3 5 ab?c? aeg?k 6 abcde? ehio?? 4 abcd abcd ----- Sample Output 1 ------ 4 6 0 ----- explanation 1 ------ Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows: 1. Convert $P_{2} = e$ to $b$ in 1 operation. 2. Convert $P_{3} = g$ to $u$ in 1 operation. 3. Convert $P_{4} = u$ to $c$ in 1 operation. 4. Convert $P_{5} = k$ to $u$ in 1 operation. Test Case 3: The strings are already equal, so no operations are required.
u = int(input()) vow = {"a", "e", "i", "o", "u"} for _ in range(u): n = int(input()) s = input() p = input() answ = n * 2 for char in range(ord("a"), ord("z") + 1): char = chr(char) curr = 0 for ind in range(n): charS = s[ind] charP = p[ind] if charS == "?": charS = char if charP == "?": charP = char if charS == charP: continue if charS in vow: if charP in vow: curr += 2 else: curr += 1 elif charP not in vow: curr += 2 else: curr += 1 if curr < answ: answ = curr print(answ)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING STRING STRING STRING STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR STRING ASSIGN VAR VAR IF VAR STRING ASSIGN VAR VAR IF VAR VAR IF VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
*"You have a reason to leave this place, but I don't."* In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows. Both of them are given 2 strings — S and P — of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?. First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation. Once the above step is done, you are allowed to perform the following two operations on P any number of times: Pick an index 1 ≤ i ≤ N such that P_{i} is a vowel, and change P_{i} to any consonant. Pick an index 1 ≤ i ≤ N such that P_{i} is a consonant, and change P_{i} to any vowel. Note: The vowels are the 5 characters \{a, e, i, o, u\} — the other 21 are consonants. Find the minimum number of operations required to convert P into S. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains an integer N, denoting the length of strings S and P. - The second and third lines contain strings S and P respectively. ------ Output Format ------ For each test case, output in a single line the minimum number of operations required to convert P into S. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ - The sum of all $N$ across all test cases does not exceed $5*10^{5}$. $|S| = |P|$ - Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$. - Every $?$ present in $S$ and $P$ should be assigned the same character. ----- Sample Input 1 ------ 3 5 ab?c? aeg?k 6 abcde? ehio?? 4 abcd abcd ----- Sample Output 1 ------ 4 6 0 ----- explanation 1 ------ Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows: 1. Convert $P_{2} = e$ to $b$ in 1 operation. 2. Convert $P_{3} = g$ to $u$ in 1 operation. 3. Convert $P_{4} = u$ to $c$ in 1 operation. 4. Convert $P_{5} = k$ to $u$ in 1 operation. Test Case 3: The strings are already equal, so no operations are required.
t = int(input()) while t > 0: t -= 1 n = int(input()) s = input() p = input() vowel = ["a", "e", "i", "o", "u"] temp = {} for i in range(26): temp[chr(i + 97)] = 0 vc = 0 cc = 0 ans = 0 for i in range(0, n): if s[i] != p[i]: if s[i] == "?": temp[p[i]] += 1 if p[i] in vowel: vc += 1 else: cc += 1 elif p[i] == "?": temp[s[i]] += 1 if s[i] in vowel: vc += 1 else: cc += 1 elif s[i] in vowel: if p[i] in vowel: ans += 2 else: ans += 1 elif p[i] in vowel: ans += 1 else: ans += 2 ans_temp = 1000000000 for i in range(26): if chr(i + 97) in vowel: ans_temp = min(ans_temp, 2 * (vc - temp[chr(i + 97)]) + cc) else: ans_temp = min(ans_temp, 2 * (cc - temp[chr(i + 97)]) + vc) print(ans + ans_temp)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR IF VAR VAR STRING VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
*"You have a reason to leave this place, but I don't."* In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows. Both of them are given 2 strings — S and P — of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?. First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation. Once the above step is done, you are allowed to perform the following two operations on P any number of times: Pick an index 1 ≤ i ≤ N such that P_{i} is a vowel, and change P_{i} to any consonant. Pick an index 1 ≤ i ≤ N such that P_{i} is a consonant, and change P_{i} to any vowel. Note: The vowels are the 5 characters \{a, e, i, o, u\} — the other 21 are consonants. Find the minimum number of operations required to convert P into S. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains an integer N, denoting the length of strings S and P. - The second and third lines contain strings S and P respectively. ------ Output Format ------ For each test case, output in a single line the minimum number of operations required to convert P into S. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ - The sum of all $N$ across all test cases does not exceed $5*10^{5}$. $|S| = |P|$ - Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$. - Every $?$ present in $S$ and $P$ should be assigned the same character. ----- Sample Input 1 ------ 3 5 ab?c? aeg?k 6 abcde? ehio?? 4 abcd abcd ----- Sample Output 1 ------ 4 6 0 ----- explanation 1 ------ Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows: 1. Convert $P_{2} = e$ to $b$ in 1 operation. 2. Convert $P_{3} = g$ to $u$ in 1 operation. 3. Convert $P_{4} = u$ to $c$ in 1 operation. 4. Convert $P_{5} = k$ to $u$ in 1 operation. Test Case 3: The strings are already equal, so no operations are required.
from sys import stdin, stdout input = stdin.readline for _ in range(int(input())): n = int(input()) p = input().strip() s = input().strip() ans = n * 2 for i in range(26): c = chr(i + ord("a")) a = s[:] b = p[:] a = a.replace("?", c) b = b.replace("?", c) t = 0 for j in range(n): if a[j] != b[j]: if a[j] in "aeiou" and b[j] in "aeiou": t += 2 elif a[j] not in "aeiou" and b[j] not in "aeiou": t += 2 else: t += 1 ans = min(ans, t) print(ans)
ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR STRING VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
*"You have a reason to leave this place, but I don't."* In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows. Both of them are given 2 strings — S and P — of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?. First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation. Once the above step is done, you are allowed to perform the following two operations on P any number of times: Pick an index 1 ≤ i ≤ N such that P_{i} is a vowel, and change P_{i} to any consonant. Pick an index 1 ≤ i ≤ N such that P_{i} is a consonant, and change P_{i} to any vowel. Note: The vowels are the 5 characters \{a, e, i, o, u\} — the other 21 are consonants. Find the minimum number of operations required to convert P into S. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains an integer N, denoting the length of strings S and P. - The second and third lines contain strings S and P respectively. ------ Output Format ------ For each test case, output in a single line the minimum number of operations required to convert P into S. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ - The sum of all $N$ across all test cases does not exceed $5*10^{5}$. $|S| = |P|$ - Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$. - Every $?$ present in $S$ and $P$ should be assigned the same character. ----- Sample Input 1 ------ 3 5 ab?c? aeg?k 6 abcde? ehio?? 4 abcd abcd ----- Sample Output 1 ------ 4 6 0 ----- explanation 1 ------ Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows: 1. Convert $P_{2} = e$ to $b$ in 1 operation. 2. Convert $P_{3} = g$ to $u$ in 1 operation. 3. Convert $P_{4} = u$ to $c$ in 1 operation. 4. Convert $P_{5} = k$ to $u$ in 1 operation. Test Case 3: The strings are already equal, so no operations are required.
for _ in range(int(input())): ans = 0 n = int(input()) s = input() p = input() vowels = ["a", "e", "i", "o", "u"] d = dict() for i in range(26): d[chr(ord("a") + i)] = 0 if s == p: print(0) else: ans = 2 * n m = n t1 = "" t2 = "" ans1 = 0 for i in range(n): if s[i] == "?" and p[i] == "?": continue if s[i] == "?" or p[i] == "?": t1 += s[i] t2 += p[i] continue if s[i] == p[i]: continue if s[i] in vowels and p[i] in vowels: ans1 += 2 elif s[i] not in vowels and p[i] not in vowels: ans1 += 2 else: ans1 += 1 m = len(t1) for i in range(26): s1 = t1 temp = chr(ord("a") + i) s1 = s1.replace("?", temp) p1 = t2 p1 = p1.replace("?", temp) val = 0 for j in range(m): v1 = s1[j] in vowels v2 = p1[j] in vowels if s1[j] != p1[j]: if v1 is True and v2 is True: val += 2 elif v1 is False and v2 is False: val += 2 else: val += 1 if val < ans: ans = val ans += ans1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING IF VAR VAR STRING VAR VAR STRING VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR STRING VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
*"You have a reason to leave this place, but I don't."* In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows. Both of them are given 2 strings — S and P — of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?. First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation. Once the above step is done, you are allowed to perform the following two operations on P any number of times: Pick an index 1 ≤ i ≤ N such that P_{i} is a vowel, and change P_{i} to any consonant. Pick an index 1 ≤ i ≤ N such that P_{i} is a consonant, and change P_{i} to any vowel. Note: The vowels are the 5 characters \{a, e, i, o, u\} — the other 21 are consonants. Find the minimum number of operations required to convert P into S. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains an integer N, denoting the length of strings S and P. - The second and third lines contain strings S and P respectively. ------ Output Format ------ For each test case, output in a single line the minimum number of operations required to convert P into S. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ - The sum of all $N$ across all test cases does not exceed $5*10^{5}$. $|S| = |P|$ - Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$. - Every $?$ present in $S$ and $P$ should be assigned the same character. ----- Sample Input 1 ------ 3 5 ab?c? aeg?k 6 abcde? ehio?? 4 abcd abcd ----- Sample Output 1 ------ 4 6 0 ----- explanation 1 ------ Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows: 1. Convert $P_{2} = e$ to $b$ in 1 operation. 2. Convert $P_{3} = g$ to $u$ in 1 operation. 3. Convert $P_{4} = u$ to $c$ in 1 operation. 4. Convert $P_{5} = k$ to $u$ in 1 operation. Test Case 3: The strings are already equal, so no operations are required.
T = int(input()) for tc in range(T): N = int(input()) s = input() p = input() vow = "aeiou" alpha = "abcdefghijklmnopqrstuvwxyz" l = [] for e in alpha: sum = 0 p2 = p.replace("?", e) s2 = s.replace("?", e) for i in range(N): if p2[i] != s2[i]: if p2[i] in vow: if s2[i] in vow: sum = sum + 2 else: sum = sum + 1 elif s2[i] in vow: sum = sum + 1 else: sum = sum + 2 l.append(sum) m = min(l) print(m)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
*"You have a reason to leave this place, but I don't."* In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows. Both of them are given 2 strings — S and P — of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?. First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation. Once the above step is done, you are allowed to perform the following two operations on P any number of times: Pick an index 1 ≤ i ≤ N such that P_{i} is a vowel, and change P_{i} to any consonant. Pick an index 1 ≤ i ≤ N such that P_{i} is a consonant, and change P_{i} to any vowel. Note: The vowels are the 5 characters \{a, e, i, o, u\} — the other 21 are consonants. Find the minimum number of operations required to convert P into S. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains an integer N, denoting the length of strings S and P. - The second and third lines contain strings S and P respectively. ------ Output Format ------ For each test case, output in a single line the minimum number of operations required to convert P into S. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ - The sum of all $N$ across all test cases does not exceed $5*10^{5}$. $|S| = |P|$ - Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$. - Every $?$ present in $S$ and $P$ should be assigned the same character. ----- Sample Input 1 ------ 3 5 ab?c? aeg?k 6 abcde? ehio?? 4 abcd abcd ----- Sample Output 1 ------ 4 6 0 ----- explanation 1 ------ Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows: 1. Convert $P_{2} = e$ to $b$ in 1 operation. 2. Convert $P_{3} = g$ to $u$ in 1 operation. 3. Convert $P_{4} = u$ to $c$ in 1 operation. 4. Convert $P_{5} = k$ to $u$ in 1 operation. Test Case 3: The strings are already equal, so no operations are required.
for tc in range(int(input())): n = int(input()) s = input() p = input() s1 = 0 v = {"a", "e", "i", "o", "u"} dv = {} dc = {} for i in range(n): if s[i] != p[i]: if s[i] != "?" and p[i] != "?": if s[i] in v and p[i] not in v: s1 += 1 elif s[i] not in v and p[i] in v: s1 += 1 else: s1 += 2 else: if s[i] == "?": c = p[i] else: c = s[i] if c in v: if c in dv: dv[c] += 1 else: dv[c] = 1 elif c in dc: dc[c] += 1 else: dc[c] = 1 dc, dv = list(dc.values()), list(dv.values()) dc_sum, dv_sum = sum(dc), sum(dv) dc_max, dv_max = max(dc, default=0), max(dv, default=0) print(s1 + min(2 * (dc_sum - dc_max) + dv_sum, 2 * (dv_sum - dv_max) + dc_sum))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR STRING STRING STRING STRING STRING ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR STRING VAR VAR STRING IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR VAR
*"You have a reason to leave this place, but I don't."* In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows. Both of them are given 2 strings — S and P — of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?. First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation. Once the above step is done, you are allowed to perform the following two operations on P any number of times: Pick an index 1 ≤ i ≤ N such that P_{i} is a vowel, and change P_{i} to any consonant. Pick an index 1 ≤ i ≤ N such that P_{i} is a consonant, and change P_{i} to any vowel. Note: The vowels are the 5 characters \{a, e, i, o, u\} — the other 21 are consonants. Find the minimum number of operations required to convert P into S. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains an integer N, denoting the length of strings S and P. - The second and third lines contain strings S and P respectively. ------ Output Format ------ For each test case, output in a single line the minimum number of operations required to convert P into S. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ - The sum of all $N$ across all test cases does not exceed $5*10^{5}$. $|S| = |P|$ - Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$. - Every $?$ present in $S$ and $P$ should be assigned the same character. ----- Sample Input 1 ------ 3 5 ab?c? aeg?k 6 abcde? ehio?? 4 abcd abcd ----- Sample Output 1 ------ 4 6 0 ----- explanation 1 ------ Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows: 1. Convert $P_{2} = e$ to $b$ in 1 operation. 2. Convert $P_{3} = g$ to $u$ in 1 operation. 3. Convert $P_{4} = u$ to $c$ in 1 operation. 4. Convert $P_{5} = k$ to $u$ in 1 operation. Test Case 3: The strings are already equal, so no operations are required.
t = int(input()) for _ in range(t): n = int(input()) s = input() p = input() vowel = "a", "e", "i", "o", "u" d = {} ct = 0 qv = 0 qc = 0 streakc = 0 streakv = 0 for i in range(n): x = p[i] z = s[i] sf = 10**5 xv = x in vowel zv = z in vowel if x == z: continue if x == "?": if z in d: d[z] += 1 else: d[z] = 1 if zv: qv += 1 streakv = max(streakv, d[z]) else: qc += 1 streakc = max(streakc, d[z]) continue if z == "?": if x in d: d[x] += 1 else: d[x] = 1 if xv: qv += 1 streakv = max(streakv, d[x]) else: qc += 1 streakc = max(streakc, d[x]) continue if xv ^ zv: ct += 1 else: ct += 2 tv = qc + (qv - streakv) * 2 tc = qv + (qc - streakc) * 2 ans = min(tv, tc) print(ans + ct)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING STRING STRING STRING STRING ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR IF VAR STRING IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR STRING IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
*"You have a reason to leave this place, but I don't."* In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows. Both of them are given 2 strings — S and P — of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?. First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation. Once the above step is done, you are allowed to perform the following two operations on P any number of times: Pick an index 1 ≤ i ≤ N such that P_{i} is a vowel, and change P_{i} to any consonant. Pick an index 1 ≤ i ≤ N such that P_{i} is a consonant, and change P_{i} to any vowel. Note: The vowels are the 5 characters \{a, e, i, o, u\} — the other 21 are consonants. Find the minimum number of operations required to convert P into S. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains an integer N, denoting the length of strings S and P. - The second and third lines contain strings S and P respectively. ------ Output Format ------ For each test case, output in a single line the minimum number of operations required to convert P into S. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ - The sum of all $N$ across all test cases does not exceed $5*10^{5}$. $|S| = |P|$ - Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$. - Every $?$ present in $S$ and $P$ should be assigned the same character. ----- Sample Input 1 ------ 3 5 ab?c? aeg?k 6 abcde? ehio?? 4 abcd abcd ----- Sample Output 1 ------ 4 6 0 ----- explanation 1 ------ Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows: 1. Convert $P_{2} = e$ to $b$ in 1 operation. 2. Convert $P_{3} = g$ to $u$ in 1 operation. 3. Convert $P_{4} = u$ to $c$ in 1 operation. 4. Convert $P_{5} = k$ to $u$ in 1 operation. Test Case 3: The strings are already equal, so no operations are required.
def most_frequent(List): counter = 0 num = List[0] for i in List: curr_frequency = List.count(i) if curr_frequency > counter: counter = curr_frequency num = i return num n = int(input()) for _ in range(n): l = int(input()) s1 = input() s2 = input() alls = "abcdefghijklmnopqrstuvwxyz" vovels = "aeiou" c = [] for j in alls: x1 = "" x2 = "" count = 0 for i in range(len(s1)): if s1[i] == "?": si = j else: si = s1[i] if s2[i] == "?": pi = j else: pi = s2[i] if si == pi: continue elif si in vovels and pi in vovels: count += 2 elif si not in vovels and pi not in vovels: count += 2 else: count += 1 c.append(count) print(min(c))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR