description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Given a binary tree, you need to find the number of all root to leaf paths along with their path lengths.
Example 1:
Input:
3
/ \
2 4
Output:
2 2 $
Explanation :
There are 2 roots to leaf paths
of length 2(3 -> 2 and 3 -> 4)
Example 2:
Input:
10
/ \
20 30
/ \
40 60
Output:
2 1 $3 2 $
Explanation:
There is 1 root leaf paths of
length 2 and 2 roots to leaf paths
of length 3.
Your Task:
Your task is to complete the function pathCounts that prints the path length and the number of root to leaf paths of this length separated by space. Every path length and number of root to leaf path should be separated by "$".
Constraints:
1 <= T <= 30
1 <= Number of nodes <= 100
1 <= Data of a node <= 1000 | def pathCounts(root):
d = {}
x = []
def helper(root):
q = []
level = 1
if not root:
return None
q.append(root)
while q:
count = 0
for i in range(len(q)):
a = q.pop(0)
if a.left:
q.append(a.left)
if a.right:
q.append(a.right)
if a.left == None and a.right == None:
count += 1
if count != 0:
d[level] = count
level += 1
helper(root)
ans = ""
for i in d:
ans += str(i) + " "
ans += str(d[i]) + " " + "$"
print(ans) | FUNC_DEF ASSIGN VAR DICT ASSIGN VAR LIST FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER IF VAR RETURN NONE EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR IF VAR NONE VAR NONE VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR STRING VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR STRING STRING EXPR FUNC_CALL VAR VAR |
Given a binary tree, you need to find the number of all root to leaf paths along with their path lengths.
Example 1:
Input:
3
/ \
2 4
Output:
2 2 $
Explanation :
There are 2 roots to leaf paths
of length 2(3 -> 2 and 3 -> 4)
Example 2:
Input:
10
/ \
20 30
/ \
40 60
Output:
2 1 $3 2 $
Explanation:
There is 1 root leaf paths of
length 2 and 2 roots to leaf paths
of length 3.
Your Task:
Your task is to complete the function pathCounts that prints the path length and the number of root to leaf paths of this length separated by space. Every path length and number of root to leaf path should be separated by "$".
Constraints:
1 <= T <= 30
1 <= Number of nodes <= 100
1 <= Data of a node <= 1000 | def pathCounts(root):
paths = {}
count = 0
def traverse(root):
nonlocal count
if root == None:
return
count += 1
if root.left == root.right == None:
if paths.get(count) == None:
paths[count] = 1
else:
paths[count] += 1
traverse(root.left)
traverse(root.right)
count -= 1
traverse(root)
for i in sorted(paths.keys()):
print(i, paths[i], end=" $")
print()
return | FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN VAR NUMBER IF VAR VAR NONE IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR RETURN |
Given a binary tree, you need to find the number of all root to leaf paths along with their path lengths.
Example 1:
Input:
3
/ \
2 4
Output:
2 2 $
Explanation :
There are 2 roots to leaf paths
of length 2(3 -> 2 and 3 -> 4)
Example 2:
Input:
10
/ \
20 30
/ \
40 60
Output:
2 1 $3 2 $
Explanation:
There is 1 root leaf paths of
length 2 and 2 roots to leaf paths
of length 3.
Your Task:
Your task is to complete the function pathCounts that prints the path length and the number of root to leaf paths of this length separated by space. Every path length and number of root to leaf path should be separated by "$".
Constraints:
1 <= T <= 30
1 <= Number of nodes <= 100
1 <= Data of a node <= 1000 | def path(dic, root, level):
if root:
if root.left is None and root.right is None:
if level in dic:
dic[level] += 1
else:
dic[level] = 1
path(dic, root.left, level + 1)
path(dic, root.right, level + 1)
def pathCounts(root):
dic = {}
path(dic, root, 1)
x = ""
for i in sorted(dic):
st = str(i) + " " + str(dic[i]) + " $"
x += st
print(x) | FUNC_DEF IF VAR IF VAR NONE VAR NONE IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR DICT EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR VAR STRING VAR VAR EXPR FUNC_CALL VAR VAR |
Given a binary tree, you need to find the number of all root to leaf paths along with their path lengths.
Example 1:
Input:
3
/ \
2 4
Output:
2 2 $
Explanation :
There are 2 roots to leaf paths
of length 2(3 -> 2 and 3 -> 4)
Example 2:
Input:
10
/ \
20 30
/ \
40 60
Output:
2 1 $3 2 $
Explanation:
There is 1 root leaf paths of
length 2 and 2 roots to leaf paths
of length 3.
Your Task:
Your task is to complete the function pathCounts that prints the path length and the number of root to leaf paths of this length separated by space. Every path length and number of root to leaf path should be separated by "$".
Constraints:
1 <= T <= 30
1 <= Number of nodes <= 100
1 <= Data of a node <= 1000 | def pathCounts(root):
def dfs(root, cnt):
if root.left == None and root.right == None:
cnt += 1
hm[cnt] = 1 + hm.get(cnt, 0)
return
cnt += 1
if root.left:
dfs(root.left, cnt)
if root.right:
dfs(root.right, cnt)
res = []
hm = {}
dfs(root, 0)
for k, v in hm.items():
res.append((k, v))
res.sort()
for item in res:
l, cnt = item
print(l, cnt, end=" $")
print() | FUNC_DEF FUNC_DEF IF VAR NONE VAR NONE VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR DICT EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR |
Given a binary tree, you need to find the number of all root to leaf paths along with their path lengths.
Example 1:
Input:
3
/ \
2 4
Output:
2 2 $
Explanation :
There are 2 roots to leaf paths
of length 2(3 -> 2 and 3 -> 4)
Example 2:
Input:
10
/ \
20 30
/ \
40 60
Output:
2 1 $3 2 $
Explanation:
There is 1 root leaf paths of
length 2 and 2 roots to leaf paths
of length 3.
Your Task:
Your task is to complete the function pathCounts that prints the path length and the number of root to leaf paths of this length separated by space. Every path length and number of root to leaf path should be separated by "$".
Constraints:
1 <= T <= 30
1 <= Number of nodes <= 100
1 <= Data of a node <= 1000 | def pathCounts(root):
d = {}
def sol(r, cur):
nonlocal d
if r == None:
return
if r.left == None and r.right == None:
cur += 1
if cur in d:
d[cur] += 1
else:
d[cur] = 1
return
sol(r.left, cur + 1)
sol(r.right, cur + 1)
sol(root, 0)
ans = []
for i in sorted(d.keys()):
print(i, d[i], "$", end="")
print() | FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR NONE RETURN IF VAR NONE VAR NONE VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING STRING EXPR FUNC_CALL VAR |
Given a binary tree, you need to find the number of all root to leaf paths along with their path lengths.
Example 1:
Input:
3
/ \
2 4
Output:
2 2 $
Explanation :
There are 2 roots to leaf paths
of length 2(3 -> 2 and 3 -> 4)
Example 2:
Input:
10
/ \
20 30
/ \
40 60
Output:
2 1 $3 2 $
Explanation:
There is 1 root leaf paths of
length 2 and 2 roots to leaf paths
of length 3.
Your Task:
Your task is to complete the function pathCounts that prints the path length and the number of root to leaf paths of this length separated by space. Every path length and number of root to leaf path should be separated by "$".
Constraints:
1 <= T <= 30
1 <= Number of nodes <= 100
1 <= Data of a node <= 1000 | def height(root):
if root is None:
return 0
return max(height(root.left), height(root.right)) + 1
def dfs(root, level, path):
if root.left is None and root.right is None:
path[level] += 1
if root.left:
dfs(root.left, level + 1, path)
if root.right:
dfs(root.right, level + 1, path)
def pathCounts(root):
h = height(root)
path = {}
for i in range(0, h + 1):
path[i] = 0
dfs(root, 1, path)
for i in range(1, h + 1):
if path[i]:
print(i, path[i], end=" $")
print() | FUNC_DEF IF VAR NONE RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR NONE VAR NONE VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR |
Given a binary tree, you need to find the number of all root to leaf paths along with their path lengths.
Example 1:
Input:
3
/ \
2 4
Output:
2 2 $
Explanation :
There are 2 roots to leaf paths
of length 2(3 -> 2 and 3 -> 4)
Example 2:
Input:
10
/ \
20 30
/ \
40 60
Output:
2 1 $3 2 $
Explanation:
There is 1 root leaf paths of
length 2 and 2 roots to leaf paths
of length 3.
Your Task:
Your task is to complete the function pathCounts that prints the path length and the number of root to leaf paths of this length separated by space. Every path length and number of root to leaf path should be separated by "$".
Constraints:
1 <= T <= 30
1 <= Number of nodes <= 100
1 <= Data of a node <= 1000 | def pathCounts(root):
if root is None:
return "0 0 $"
queue = [root]
path_lengths = {}
length = 1
while queue:
n = len(queue)
for i in range(len(queue)):
node = queue.pop(0)
if node.left is None and node.right is None:
if length not in path_lengths:
path_lengths[length] = 1
else:
path_lengths[length] += 1
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
length += 1
P = list(path_lengths.items())
P.sort(key=lambda x: x[0])
result = ""
for a, b in P:
result += str(a) + " " + str(b) + " $"
print(result) | FUNC_DEF IF VAR NONE RETURN STRING ASSIGN VAR LIST VAR ASSIGN VAR DICT ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NONE VAR NONE IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING FOR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR |
Given a binary tree, you need to find the number of all root to leaf paths along with their path lengths.
Example 1:
Input:
3
/ \
2 4
Output:
2 2 $
Explanation :
There are 2 roots to leaf paths
of length 2(3 -> 2 and 3 -> 4)
Example 2:
Input:
10
/ \
20 30
/ \
40 60
Output:
2 1 $3 2 $
Explanation:
There is 1 root leaf paths of
length 2 and 2 roots to leaf paths
of length 3.
Your Task:
Your task is to complete the function pathCounts that prints the path length and the number of root to leaf paths of this length separated by space. Every path length and number of root to leaf path should be separated by "$".
Constraints:
1 <= T <= 30
1 <= Number of nodes <= 100
1 <= Data of a node <= 1000 | def pathCounts(root):
l = []
d = {}
def helper(root):
if root is None:
return
l.append(root.data)
if not root.left and not root.right:
if len(l) not in d:
d[len(l)] = 0
d[len(l)] += 1
helper(root.left)
helper(root.right)
l.pop()
helper(root)
for i in d:
print(i, d[i], "$", end="")
print() | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR DICT FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING STRING EXPR FUNC_CALL VAR |
Given a binary tree, you need to find the number of all root to leaf paths along with their path lengths.
Example 1:
Input:
3
/ \
2 4
Output:
2 2 $
Explanation :
There are 2 roots to leaf paths
of length 2(3 -> 2 and 3 -> 4)
Example 2:
Input:
10
/ \
20 30
/ \
40 60
Output:
2 1 $3 2 $
Explanation:
There is 1 root leaf paths of
length 2 and 2 roots to leaf paths
of length 3.
Your Task:
Your task is to complete the function pathCounts that prints the path length and the number of root to leaf paths of this length separated by space. Every path length and number of root to leaf path should be separated by "$".
Constraints:
1 <= T <= 30
1 <= Number of nodes <= 100
1 <= Data of a node <= 1000 | def solve(root, ans, level):
if root == None:
return
if root.left == None and root.right == None:
ans.append(level)
solve(root.left, ans, level + 1)
solve(root.right, ans, level + 1)
def pathCounts(root):
ans = []
solve(root, ans, 1)
ans.sort()
count = 1
for i in range(1, len(ans)):
if ans[i] > ans[i - 1]:
print(ans[i - 1], count, end=" $")
count = 0
count += 1
print(ans[-1], count, end=" $")
print() | FUNC_DEF IF VAR NONE RETURN IF VAR NONE VAR NONE EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR STRING ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR STRING EXPR FUNC_CALL VAR |
Given a binary tree, you need to find the number of all root to leaf paths along with their path lengths.
Example 1:
Input:
3
/ \
2 4
Output:
2 2 $
Explanation :
There are 2 roots to leaf paths
of length 2(3 -> 2 and 3 -> 4)
Example 2:
Input:
10
/ \
20 30
/ \
40 60
Output:
2 1 $3 2 $
Explanation:
There is 1 root leaf paths of
length 2 and 2 roots to leaf paths
of length 3.
Your Task:
Your task is to complete the function pathCounts that prints the path length and the number of root to leaf paths of this length separated by space. Every path length and number of root to leaf path should be separated by "$".
Constraints:
1 <= T <= 30
1 <= Number of nodes <= 100
1 <= Data of a node <= 1000 | def dfs(root, curr, hashmap):
if not root:
return
curr += 1
if root.left == None and root.right == None:
if curr not in hashmap:
hashmap[curr] = 0
hashmap[curr] += 1
return
dfs(root.left, curr, hashmap)
dfs(root.right, curr, hashmap)
def pathCounts(root):
hashmap = {}
dfs(root, 0, hashmap)
ans = []
for path in hashmap:
ans.append(str(path) + " " + str(hashmap[path]))
ans = " $".join(ans)
ans += " $"
print(ans) | FUNC_DEF IF VAR RETURN VAR NUMBER IF VAR NONE VAR NONE IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR DICT EXPR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR VAR STRING EXPR FUNC_CALL VAR VAR |
Given a binary tree, you need to find the number of all root to leaf paths along with their path lengths.
Example 1:
Input:
3
/ \
2 4
Output:
2 2 $
Explanation :
There are 2 roots to leaf paths
of length 2(3 -> 2 and 3 -> 4)
Example 2:
Input:
10
/ \
20 30
/ \
40 60
Output:
2 1 $3 2 $
Explanation:
There is 1 root leaf paths of
length 2 and 2 roots to leaf paths
of length 3.
Your Task:
Your task is to complete the function pathCounts that prints the path length and the number of root to leaf paths of this length separated by space. Every path length and number of root to leaf path should be separated by "$".
Constraints:
1 <= T <= 30
1 <= Number of nodes <= 100
1 <= Data of a node <= 1000 | def path(root, count, l):
if root == None:
return
if root.left == None and root.right == None:
l.append(count)
return
path(root.left, count + 1, l)
path(root.right, count + 1, l)
def pathCounts(root):
l = []
count = 1
path(root, count, l)
for i in set(l):
print("{} {} ".format(i, l.count(i)), end="$")
print() | FUNC_DEF IF VAR NONE RETURN IF VAR NONE VAR NONE EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
Given a binary tree, you need to find the number of all root to leaf paths along with their path lengths.
Example 1:
Input:
3
/ \
2 4
Output:
2 2 $
Explanation :
There are 2 roots to leaf paths
of length 2(3 -> 2 and 3 -> 4)
Example 2:
Input:
10
/ \
20 30
/ \
40 60
Output:
2 1 $3 2 $
Explanation:
There is 1 root leaf paths of
length 2 and 2 roots to leaf paths
of length 3.
Your Task:
Your task is to complete the function pathCounts that prints the path length and the number of root to leaf paths of this length separated by space. Every path length and number of root to leaf path should be separated by "$".
Constraints:
1 <= T <= 30
1 <= Number of nodes <= 100
1 <= Data of a node <= 1000 | def fn(root, dic, lvl):
if root:
if not root.left and not root.right:
try:
dic[lvl] += 1
except:
dic[lvl] = 1
fn(root.left, dic, lvl + 1)
fn(root.right, dic, lvl + 1)
def pathCounts(root):
dic = {}
s = ""
fn(root, dic, 1)
for i in dic:
s += str(i) + " " + str(dic[i]) + " " + "$"
print(s) | FUNC_DEF IF VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR DICT ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR VAR STRING STRING EXPR FUNC_CALL VAR VAR |
Given a binary tree, you need to find the number of all root to leaf paths along with their path lengths.
Example 1:
Input:
3
/ \
2 4
Output:
2 2 $
Explanation :
There are 2 roots to leaf paths
of length 2(3 -> 2 and 3 -> 4)
Example 2:
Input:
10
/ \
20 30
/ \
40 60
Output:
2 1 $3 2 $
Explanation:
There is 1 root leaf paths of
length 2 and 2 roots to leaf paths
of length 3.
Your Task:
Your task is to complete the function pathCounts that prints the path length and the number of root to leaf paths of this length separated by space. Every path length and number of root to leaf path should be separated by "$".
Constraints:
1 <= T <= 30
1 <= Number of nodes <= 100
1 <= Data of a node <= 1000 | table = dict()
def solve(node, c=0):
c += 1
if node == None:
return
if node.left == None and node.right == None:
if c not in table:
table[c] = 0
table[c] += 1
return
solve(node.left, c)
solve(node.right, c)
return
def pathCounts(root):
global table
table = dict()
solve(root)
s = ""
for key in table:
s += str(key) + " " + str(table[key]) + " " + "$"
print(s) | ASSIGN VAR FUNC_CALL VAR FUNC_DEF NUMBER VAR NUMBER IF VAR NONE RETURN IF VAR NONE VAR NONE IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR VAR STRING STRING EXPR FUNC_CALL VAR VAR |
Given a binary tree, you need to find the number of all root to leaf paths along with their path lengths.
Example 1:
Input:
3
/ \
2 4
Output:
2 2 $
Explanation :
There are 2 roots to leaf paths
of length 2(3 -> 2 and 3 -> 4)
Example 2:
Input:
10
/ \
20 30
/ \
40 60
Output:
2 1 $3 2 $
Explanation:
There is 1 root leaf paths of
length 2 and 2 roots to leaf paths
of length 3.
Your Task:
Your task is to complete the function pathCounts that prints the path length and the number of root to leaf paths of this length separated by space. Every path length and number of root to leaf path should be separated by "$".
Constraints:
1 <= T <= 30
1 <= Number of nodes <= 100
1 <= Data of a node <= 1000 | def pathCounts(root):
dic = dict()
def rec(nod, dis):
if nod.left is None and nod.right is None:
if dis not in dic:
dic[dis] = 0
dic[dis] += 1
return
if nod.left:
rec(nod.left, dis + 1)
if nod.right:
rec(nod.right, dis + 1)
rec(root, 1)
for i in sorted(dic.keys()):
print(i, dic[i], "$", end="")
print() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR NONE VAR NONE IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER RETURN IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING STRING EXPR FUNC_CALL VAR |
Given a binary tree, you need to find the number of all root to leaf paths along with their path lengths.
Example 1:
Input:
3
/ \
2 4
Output:
2 2 $
Explanation :
There are 2 roots to leaf paths
of length 2(3 -> 2 and 3 -> 4)
Example 2:
Input:
10
/ \
20 30
/ \
40 60
Output:
2 1 $3 2 $
Explanation:
There is 1 root leaf paths of
length 2 and 2 roots to leaf paths
of length 3.
Your Task:
Your task is to complete the function pathCounts that prints the path length and the number of root to leaf paths of this length separated by space. Every path length and number of root to leaf path should be separated by "$".
Constraints:
1 <= T <= 30
1 <= Number of nodes <= 100
1 <= Data of a node <= 1000 | def solve(root, lis, count):
if root == None:
return
count += 1
if root.left == None and root.right == None:
lis.append(count)
solve(root.left, lis, count)
solve(root.right, lis, count)
count -= 1
def pathCounts(root):
lis = []
ans = []
solve(root, lis, 0)
ans = lis.copy()
ans = set(ans)
for i in ans:
k = lis.count(i)
print(i, k, "$", end="") | FUNC_DEF IF VAR NONE RETURN VAR NUMBER IF VAR NONE VAR NONE EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING STRING |
Given a binary tree, you need to find the number of all root to leaf paths along with their path lengths.
Example 1:
Input:
3
/ \
2 4
Output:
2 2 $
Explanation :
There are 2 roots to leaf paths
of length 2(3 -> 2 and 3 -> 4)
Example 2:
Input:
10
/ \
20 30
/ \
40 60
Output:
2 1 $3 2 $
Explanation:
There is 1 root leaf paths of
length 2 and 2 roots to leaf paths
of length 3.
Your Task:
Your task is to complete the function pathCounts that prints the path length and the number of root to leaf paths of this length separated by space. Every path length and number of root to leaf path should be separated by "$".
Constraints:
1 <= T <= 30
1 <= Number of nodes <= 100
1 <= Data of a node <= 1000 | def pathCounts(root):
def traversal(root):
nonlocal count
if root == None:
return None
count += 1
if root.left == None and root.right == None:
if count not in dict1:
dict1[count] = 1
else:
dict1[count] += 1
if traversal(root.left) or traversal(root.right):
return True
count -= 1
return False
count = 0
dict1 = dict()
traversal(root)
string = ""
arrx = sorted(list(dict1.items()), key=lambda x: [0])
for i in arrx:
string += str(i[0]) + " " + str(i[1]) + " $"
print(string) | FUNC_DEF FUNC_DEF IF VAR NONE RETURN NONE VAR NUMBER IF VAR NONE VAR NONE IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR LIST NUMBER FOR VAR VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR |
Given a binary tree, you need to find the number of all root to leaf paths along with their path lengths.
Example 1:
Input:
3
/ \
2 4
Output:
2 2 $
Explanation :
There are 2 roots to leaf paths
of length 2(3 -> 2 and 3 -> 4)
Example 2:
Input:
10
/ \
20 30
/ \
40 60
Output:
2 1 $3 2 $
Explanation:
There is 1 root leaf paths of
length 2 and 2 roots to leaf paths
of length 3.
Your Task:
Your task is to complete the function pathCounts that prints the path length and the number of root to leaf paths of this length separated by space. Every path length and number of root to leaf path should be separated by "$".
Constraints:
1 <= T <= 30
1 <= Number of nodes <= 100
1 <= Data of a node <= 1000 | def recur(root, level, d):
if not root:
return
if not root.left and not root.right:
if level in d:
d[level] += 1
else:
d[level] = 1
recur(root.left, level + 1, d)
recur(root.right, level + 1, d)
def pathCounts(root):
d = {}
recur(root, 1, d)
for i in sorted(d):
print(str(i) + " " + str(d[i]) + " $", end="")
print() | FUNC_DEF IF VAR RETURN IF VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR DICT EXPR FUNC_CALL VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR VAR STRING STRING EXPR FUNC_CALL VAR |
Given a binary tree, you need to find the number of all root to leaf paths along with their path lengths.
Example 1:
Input:
3
/ \
2 4
Output:
2 2 $
Explanation :
There are 2 roots to leaf paths
of length 2(3 -> 2 and 3 -> 4)
Example 2:
Input:
10
/ \
20 30
/ \
40 60
Output:
2 1 $3 2 $
Explanation:
There is 1 root leaf paths of
length 2 and 2 roots to leaf paths
of length 3.
Your Task:
Your task is to complete the function pathCounts that prints the path length and the number of root to leaf paths of this length separated by space. Every path length and number of root to leaf path should be separated by "$".
Constraints:
1 <= T <= 30
1 <= Number of nodes <= 100
1 <= Data of a node <= 1000 | def pathCounts(root):
l = get(root)
d = {}
for item in l:
if item in d:
d[item] += 1
else:
d[item] = 1
for item in d:
print(item, d[item], "$", end="")
print()
def get(root):
if root:
if root.left == None and root.right == None:
return [1]
else:
a = get(root.left)
a.extend(get(root.right))
for i in range(len(a)):
a[i] += 1
return a
else:
return [] | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING STRING EXPR FUNC_CALL VAR FUNC_DEF IF VAR IF VAR NONE VAR NONE RETURN LIST NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR RETURN LIST |
Given a binary tree, you need to find the number of all root to leaf paths along with their path lengths.
Example 1:
Input:
3
/ \
2 4
Output:
2 2 $
Explanation :
There are 2 roots to leaf paths
of length 2(3 -> 2 and 3 -> 4)
Example 2:
Input:
10
/ \
20 30
/ \
40 60
Output:
2 1 $3 2 $
Explanation:
There is 1 root leaf paths of
length 2 and 2 roots to leaf paths
of length 3.
Your Task:
Your task is to complete the function pathCounts that prints the path length and the number of root to leaf paths of this length separated by space. Every path length and number of root to leaf path should be separated by "$".
Constraints:
1 <= T <= 30
1 <= Number of nodes <= 100
1 <= Data of a node <= 1000 | def pathCounts(root):
temp = 0
global _list1
_list1 = []
if root.left:
pathCountsUtil(root.left, temp + 1)
if root.right:
pathCountsUtil(root.right, temp + 1)
s = set()
for i in _list1:
s.add(i)
for i in s:
print(str(i) + " " + str(_list1.count(i)) + " $", end="")
print()
s1 = set()
def pathCountsUtil(root, temp):
global arr
if root.left:
pathCountsUtil(root.left, temp + 1)
if root.right:
pathCountsUtil(root.right, temp + 1)
if not root.left and not root.right:
_list1.append(temp + 1) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR FUNC_CALL VAR VAR STRING STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
Given an array of lowercase strings A[] of size N, determine if the strings can be chained together to form a circle.
A string X can be chained together with another string Y if the last character of X is same as first character of Y. If every string of the array can be chained with exactly two strings of the array(one with the first character and second with the last character of the string), it will form a circle.
For example, for the array arr[] = {"for", "geek", "rig", "kaf"} the answer will be Yes as the given strings can be chained as "for", "rig", "geek" and "kaf"
Example 1:
Input:
N = 3
A[] = { "abc", "bcd", "cdf" }
Output:
0
Explaination:
These strings can't form a circle
because no string has 'd'at the starting index.
Example 2:
Input:
N = 4
A[] = { "ab" , "bc", "cd", "da" }
Output:
1
Explaination:
These strings can form a circle
of strings.
Your Task:
You don't need to read input or print output. Your task is to complete the function isCircle() which takes the length of the array N and the array A as input parameters and returns 1 if we can form a circle or 0 if we cannot.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{4}
1 ≤ Length of strings ≤ 20 | def dfs(g, i):
global visited
visited[i] = True
for j in g[i]:
if visited[j] == False:
dfs(g, j)
def Circle(s):
global visited
n = len(s)
mark = [False] * 26
adj = {i: [] for i in range(26)}
indegree, outdegree = [0] * 26, [0] * 26
for i in s:
u, v = ord(i[0]) - ord("a"), ord(i[-1]) - ord("a")
mark[u], mark[v] = True, True
indegree[v] += 1
outdegree[u] += 1
adj[u].append(v)
for i in range(26):
if indegree[i] != outdegree[i]:
return 0
visited = [False] * 26
dfs(adj, ord(s[0][0]) - ord("a"))
for i in range(26):
if visited[i] == 0 and mark[i] == True:
return 0
return 1
class Solution:
def isCircle(self, N, A):
return Circle(A) | FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER NUMBER BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER RETURN NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR |
Given an array of lowercase strings A[] of size N, determine if the strings can be chained together to form a circle.
A string X can be chained together with another string Y if the last character of X is same as first character of Y. If every string of the array can be chained with exactly two strings of the array(one with the first character and second with the last character of the string), it will form a circle.
For example, for the array arr[] = {"for", "geek", "rig", "kaf"} the answer will be Yes as the given strings can be chained as "for", "rig", "geek" and "kaf"
Example 1:
Input:
N = 3
A[] = { "abc", "bcd", "cdf" }
Output:
0
Explaination:
These strings can't form a circle
because no string has 'd'at the starting index.
Example 2:
Input:
N = 4
A[] = { "ab" , "bc", "cd", "da" }
Output:
1
Explaination:
These strings can form a circle
of strings.
Your Task:
You don't need to read input or print output. Your task is to complete the function isCircle() which takes the length of the array N and the array A as input parameters and returns 1 if we can form a circle or 0 if we cannot.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{4}
1 ≤ Length of strings ≤ 20 | class Solution:
def DFS(self, adj, start, visited):
visited[start] = 1
for ch in adj[start]:
if visited[ch] == 0:
self.DFS(adj, ch, visited)
def CheckLoop(self, adj, characters, start):
visited = [0] * 26
self.DFS(adj, start, visited)
for i in range(26):
if characters[i] == 1 and visited[i] == 0:
return False
return True
def check(self, N, A):
characters = [0] * 26
adj = [[] for i in range(26)]
indegree = [0] * 26
outdegree = [0] * 26
for string in A:
first = ord(string[0]) - 97
last = ord(string[len(string) - 1]) - 97
adj[first].append(last)
outdegree[first] += 1
indegree[last] += 1
characters[first] = 1
characters[last] = 1
for i in range(26):
if indegree[i] != outdegree[i]:
return False
return self.CheckLoop(adj, characters, ord(A[0][0]) - 97)
def isCircle(self, N, A):
return int(self.check(N, A)) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Given an array of lowercase strings A[] of size N, determine if the strings can be chained together to form a circle.
A string X can be chained together with another string Y if the last character of X is same as first character of Y. If every string of the array can be chained with exactly two strings of the array(one with the first character and second with the last character of the string), it will form a circle.
For example, for the array arr[] = {"for", "geek", "rig", "kaf"} the answer will be Yes as the given strings can be chained as "for", "rig", "geek" and "kaf"
Example 1:
Input:
N = 3
A[] = { "abc", "bcd", "cdf" }
Output:
0
Explaination:
These strings can't form a circle
because no string has 'd'at the starting index.
Example 2:
Input:
N = 4
A[] = { "ab" , "bc", "cd", "da" }
Output:
1
Explaination:
These strings can form a circle
of strings.
Your Task:
You don't need to read input or print output. Your task is to complete the function isCircle() which takes the length of the array N and the array A as input parameters and returns 1 if we can form a circle or 0 if we cannot.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{4}
1 ≤ Length of strings ≤ 20 | class Solution:
def isCircle(self, N, A):
adj, wcnt, vis, c = {}, {}, set(), 0
for w in A:
if w[0] not in adj:
adj[w[0]] = [w[-1]]
else:
adj[w[0]].append(w[-1])
if w[0] not in wcnt:
wcnt[w[0]] = 1
else:
wcnt[w[0]] += 1
if w[-1] not in wcnt:
wcnt[w[-1]] = 1
else:
wcnt[w[-1]] += 1
def dfs(x):
vis.add(x)
if x in adj:
for ele in adj[x]:
if ele not in vis:
dfs(ele)
for k in adj:
if k not in vis:
dfs(k)
c += 1
if c > 1:
return 0
for u in wcnt:
if wcnt[u] % 2 != 0:
return 0
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR DICT DICT FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER LIST VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR IF VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER |
Given an array of lowercase strings A[] of size N, determine if the strings can be chained together to form a circle.
A string X can be chained together with another string Y if the last character of X is same as first character of Y. If every string of the array can be chained with exactly two strings of the array(one with the first character and second with the last character of the string), it will form a circle.
For example, for the array arr[] = {"for", "geek", "rig", "kaf"} the answer will be Yes as the given strings can be chained as "for", "rig", "geek" and "kaf"
Example 1:
Input:
N = 3
A[] = { "abc", "bcd", "cdf" }
Output:
0
Explaination:
These strings can't form a circle
because no string has 'd'at the starting index.
Example 2:
Input:
N = 4
A[] = { "ab" , "bc", "cd", "da" }
Output:
1
Explaination:
These strings can form a circle
of strings.
Your Task:
You don't need to read input or print output. Your task is to complete the function isCircle() which takes the length of the array N and the array A as input parameters and returns 1 if we can form a circle or 0 if we cannot.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{4}
1 ≤ Length of strings ≤ 20 | CHARS = 26
class Graph(object):
def __init__(self, V):
self.V = V
self.adj = [[] for x in range(V)]
self.inp = [0] * V
def addEdge(self, v, w):
self.adj[v].append(w)
self.inp[w] += 1
def isEulerianCycle(self):
if self.isSconnected() == False:
return False
for i in range(self.V):
if len(self.adj[i]) != self.inp[i]:
return False
return True
def isSconnected(self):
visited = [False] * self.V
n = 0
for n in range(self.V):
if len(self.adj[n]) > 0:
break
self.DFS(n, visited)
for i in range(self.V):
if len(self.adj[i]) > 0 and visited[i] == False:
return False
gr = self.getTranspose()
visited = [False] * self.V
gr.DFS(n, visited)
for i in range(self.V):
if len(self.adj[i]) > 0 and visited[i] == False:
return False
return True
def DFS(self, v, visited):
visited[v] = True
for i in self.adj[v]:
if visited[i] == False:
self.DFS(i, visited)
def getTranspose(self):
g = Graph(self.V)
for v in range(self.V):
for i in range(len(self.adj[v])):
g.adj[self.adj[v][i]].append(v)
g.inp[v] += 1
return g
class Solution:
def isCircle(self, N, A):
g = Graph(CHARS)
for i in range(N):
s = A[i]
g.addEdge(ord(s[0]) - ord("a"), ord(s[len(s) - 1]) - ord("a"))
return 1 if g.isEulerianCycle() else 0 | ASSIGN VAR NUMBER CLASS_DEF VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FUNC_DEF IF FUNC_CALL VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING RETURN FUNC_CALL VAR NUMBER NUMBER |
Given an array of lowercase strings A[] of size N, determine if the strings can be chained together to form a circle.
A string X can be chained together with another string Y if the last character of X is same as first character of Y. If every string of the array can be chained with exactly two strings of the array(one with the first character and second with the last character of the string), it will form a circle.
For example, for the array arr[] = {"for", "geek", "rig", "kaf"} the answer will be Yes as the given strings can be chained as "for", "rig", "geek" and "kaf"
Example 1:
Input:
N = 3
A[] = { "abc", "bcd", "cdf" }
Output:
0
Explaination:
These strings can't form a circle
because no string has 'd'at the starting index.
Example 2:
Input:
N = 4
A[] = { "ab" , "bc", "cd", "da" }
Output:
1
Explaination:
These strings can form a circle
of strings.
Your Task:
You don't need to read input or print output. Your task is to complete the function isCircle() which takes the length of the array N and the array A as input parameters and returns 1 if we can form a circle or 0 if we cannot.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{4}
1 ≤ Length of strings ≤ 20 | class Solution:
def isCircle(self, N, A):
def dfs(visited, v, adj):
visited[v] = True
for i in adj[v]:
if not visited[i]:
dfs(visited, i, adj)
def isconnected(adj, mark, v):
visited = [False] * 26
dfs(visited, v, adj)
for i in range(26):
if mark[i] and not visited[i]:
return 0
return 1
adj = {}
mark = [False] * 26
In = [0] * 26
Out = [0] * 26
for i in range(N):
f = ord(A[i][0]) - ord("a")
l = ord(A[i][-1]) - ord("a")
mark[f], mark[l] = True, True
if f not in adj:
adj[f] = []
adj[f].append(l)
In[l] += 1
Out[f] += 1
for i in range(26):
if In[i] != Out[i]:
return 0
return isconnected(adj, mark, ord(A[0][0]) - ord("a")) | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR STRING |
Given an array of lowercase strings A[] of size N, determine if the strings can be chained together to form a circle.
A string X can be chained together with another string Y if the last character of X is same as first character of Y. If every string of the array can be chained with exactly two strings of the array(one with the first character and second with the last character of the string), it will form a circle.
For example, for the array arr[] = {"for", "geek", "rig", "kaf"} the answer will be Yes as the given strings can be chained as "for", "rig", "geek" and "kaf"
Example 1:
Input:
N = 3
A[] = { "abc", "bcd", "cdf" }
Output:
0
Explaination:
These strings can't form a circle
because no string has 'd'at the starting index.
Example 2:
Input:
N = 4
A[] = { "ab" , "bc", "cd", "da" }
Output:
1
Explaination:
These strings can form a circle
of strings.
Your Task:
You don't need to read input or print output. Your task is to complete the function isCircle() which takes the length of the array N and the array A as input parameters and returns 1 if we can form a circle or 0 if we cannot.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{4}
1 ≤ Length of strings ≤ 20 | class Solution:
def __init__(self):
self.V = 26
self.adj = [[] for x in range(26)]
self.inp = [0] * 26
def addEdge(self, v, w):
self.adj[v].append(w)
self.inp[w] += 1
def isSC(self):
visited = [False] * self.V
n = 0
for n in range(self.V):
if len(self.adj[n]) > 0:
break
self.DFSUtil(n, visited)
for i in range(self.V):
if len(self.adj[i]) > 0 and visited[i] == False:
return False
gr = self.getTranspose()
for i in range(self.V):
visited[i] = False
gr.DFSUtil(n, visited)
for i in range(self.V):
if len(self.adj[i]) > 0 and visited[i] == False:
return False
return True
def isEulerianCycle(self):
if self.isSC() == False:
return False
for i in range(self.V):
if len(self.adj[i]) != self.inp[i]:
return False
return True
def DFSUtil(self, v, visited):
visited[v] = True
for i in range(len(self.adj[v])):
if not visited[self.adj[v][i]]:
self.DFSUtil(self.adj[v][i], visited)
def getTranspose(self):
g = Solution()
for v in range(self.V):
for i in range(len(self.adj[v])):
g.adj[self.adj[v][i]].append(v)
g.inp[v] += 1
return g
def isCircle(self, N, A):
g = Solution()
for i in range(N):
s = A[i]
g.addEdge(ord(s[0]) - ord("a"), ord(s[len(s) - 1]) - ord("a"))
if g.isEulerianCycle():
return 1
else:
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF FUNC_CALL VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING IF FUNC_CALL VAR RETURN NUMBER RETURN NUMBER |
Given an array of lowercase strings A[] of size N, determine if the strings can be chained together to form a circle.
A string X can be chained together with another string Y if the last character of X is same as first character of Y. If every string of the array can be chained with exactly two strings of the array(one with the first character and second with the last character of the string), it will form a circle.
For example, for the array arr[] = {"for", "geek", "rig", "kaf"} the answer will be Yes as the given strings can be chained as "for", "rig", "geek" and "kaf"
Example 1:
Input:
N = 3
A[] = { "abc", "bcd", "cdf" }
Output:
0
Explaination:
These strings can't form a circle
because no string has 'd'at the starting index.
Example 2:
Input:
N = 4
A[] = { "ab" , "bc", "cd", "da" }
Output:
1
Explaination:
These strings can form a circle
of strings.
Your Task:
You don't need to read input or print output. Your task is to complete the function isCircle() which takes the length of the array N and the array A as input parameters and returns 1 if we can form a circle or 0 if we cannot.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{4}
1 ≤ Length of strings ≤ 20 | import sys
class Graph:
def __init__(self, v):
self.v = v
self.adj = [[] for i in range(v)]
self.inp = [0] * v
def addEdge(self, v, w):
self.adj[v].append(w)
self.inp[w] += 1
def DFSUtil(self, v, visited):
visited[v] = True
for i in self.adj[v]:
if not visited[i]:
self.DFSUtil(i, visited)
def getTranspose(self):
g = Graph(self.v)
for v in range(self.v):
for i in range(len(self.adj[v])):
g.adj[self.adj[v][i]].append(v)
g.inp[v] += 1
return g
def isSC(self):
visited = [False] * self.v
n = 0
for n in range(self.v):
if len(self.adj[n]) > 0:
break
self.DFSUtil(n, visited)
for i in range(self.v):
if len(self.adj[i]) > 0 and visited[i] == False:
return False
gr = self.getTranspose()
for i in range(self.v):
visited[i] = False
gr.DFSUtil(n, visited)
for i in range(self.v):
if len(self.adj[i]) > 0 and visited[i] == False:
return False
return True
def isEulerianCycle(self):
if self.isSC() == False:
return False
for i in range(self.v):
if len(self.adj[i]) != self.inp[i]:
return False
return True
class Solution:
def isCircle(self, n, arr):
g = Graph(26)
for i in range(n):
s = arr[i]
g.addEdge(ord(s[0]) - ord("a"), ord(s[-1]) - ord("a"))
if g.isEulerianCycle():
return 1
return 0
sys.setrecursionlimit(10**6)
if __name__ == "__main__":
t = int(input())
for _ in range(t):
N = int(input())
A = input().split()
ob = Solution()
print(ob.isCircle(N, A)) | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF FUNC_CALL VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING IF FUNC_CALL VAR RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Given an array of lowercase strings A[] of size N, determine if the strings can be chained together to form a circle.
A string X can be chained together with another string Y if the last character of X is same as first character of Y. If every string of the array can be chained with exactly two strings of the array(one with the first character and second with the last character of the string), it will form a circle.
For example, for the array arr[] = {"for", "geek", "rig", "kaf"} the answer will be Yes as the given strings can be chained as "for", "rig", "geek" and "kaf"
Example 1:
Input:
N = 3
A[] = { "abc", "bcd", "cdf" }
Output:
0
Explaination:
These strings can't form a circle
because no string has 'd'at the starting index.
Example 2:
Input:
N = 4
A[] = { "ab" , "bc", "cd", "da" }
Output:
1
Explaination:
These strings can form a circle
of strings.
Your Task:
You don't need to read input or print output. Your task is to complete the function isCircle() which takes the length of the array N and the array A as input parameters and returns 1 if we can form a circle or 0 if we cannot.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{4}
1 ≤ Length of strings ≤ 20 | class Solution:
def isCircle(self, N, A):
endswith = {}
startswith = {}
first_pal = set()
for string in A:
start = string[0]
end = string[-1]
if start == end:
first_pal.add(start)
continue
startswith[start] = startswith.get(start, 0) + 1
endswith[end] = endswith.get(end, 0) + 1
for char, freq in endswith.items():
if freq != startswith.get(char, 0):
return 0
if len(startswith) == 0:
if len(first_pal) <= 1:
return 1
return 0
for item in first_pal:
if item not in startswith:
return 0
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FOR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER |
Given an array of lowercase strings A[] of size N, determine if the strings can be chained together to form a circle.
A string X can be chained together with another string Y if the last character of X is same as first character of Y. If every string of the array can be chained with exactly two strings of the array(one with the first character and second with the last character of the string), it will form a circle.
For example, for the array arr[] = {"for", "geek", "rig", "kaf"} the answer will be Yes as the given strings can be chained as "for", "rig", "geek" and "kaf"
Example 1:
Input:
N = 3
A[] = { "abc", "bcd", "cdf" }
Output:
0
Explaination:
These strings can't form a circle
because no string has 'd'at the starting index.
Example 2:
Input:
N = 4
A[] = { "ab" , "bc", "cd", "da" }
Output:
1
Explaination:
These strings can form a circle
of strings.
Your Task:
You don't need to read input or print output. Your task is to complete the function isCircle() which takes the length of the array N and the array A as input parameters and returns 1 if we can form a circle or 0 if we cannot.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{4}
1 ≤ Length of strings ≤ 20 | import sys
class Solution:
def dfs(self, node, adj, vis):
vis[node] = True
for u in adj[node]:
if vis[u] == False:
self.dfs(u, adj, vis)
def isCircle(self, N, A):
adj = [[] for i in range(26)]
adjr = [[] for i in range(26)]
for i in range(N):
x = len(A[i]) - 1
adj[ord(A[i][0]) - ord("a")].append(ord(A[i][x]) - ord("a"))
adjr[ord(A[i][x]) - ord("a")].append(ord(A[i][0]) - ord("a"))
indeg = [(0) for i in range(26)]
for i in range(26):
if len(adj[i]):
for u in adj[i]:
indeg[u] = indeg[u] + 1
for i in range(26):
if len(adj[i]) > 0 and indeg[i] != len(adj[i]):
return 0
visited = [(False) for i in range(26)]
n = 0
for i in range(26):
if indeg[i]:
n = i
break
self.dfs(n, adj, visited)
for i in range(26):
if visited[i] == False and len(adjr[i]):
return 0
return 1
sys.setrecursionlimit(10**6)
if __name__ == "__main__":
t = int(input())
for _ in range(t):
N = int(input())
A = input().split()
ob = Solution()
print(ob.isCircle(N, A)) | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Given an array of lowercase strings A[] of size N, determine if the strings can be chained together to form a circle.
A string X can be chained together with another string Y if the last character of X is same as first character of Y. If every string of the array can be chained with exactly two strings of the array(one with the first character and second with the last character of the string), it will form a circle.
For example, for the array arr[] = {"for", "geek", "rig", "kaf"} the answer will be Yes as the given strings can be chained as "for", "rig", "geek" and "kaf"
Example 1:
Input:
N = 3
A[] = { "abc", "bcd", "cdf" }
Output:
0
Explaination:
These strings can't form a circle
because no string has 'd'at the starting index.
Example 2:
Input:
N = 4
A[] = { "ab" , "bc", "cd", "da" }
Output:
1
Explaination:
These strings can form a circle
of strings.
Your Task:
You don't need to read input or print output. Your task is to complete the function isCircle() which takes the length of the array N and the array A as input parameters and returns 1 if we can form a circle or 0 if we cannot.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{4}
1 ≤ Length of strings ≤ 20 | import sys
class Solution:
def dfs(self, adj, n, vis):
vis[n] = 1
for node in adj[n]:
if vis[node] == 0:
self.dfs(adj, node, vis)
def sc(self, v, adj):
vis = [False] * v
for n in range(v):
if len(adj[n]) > 0:
break
self.dfs(adj, n, vis)
for i in range(v):
if len(adj[i]) > 0 and vis[i] == False:
return False
newAdj = [[] for i in range(v)]
for i in range(v):
for node in adj[i]:
newAdj[node].append(i)
for i in range(v):
vis[i] = False
self.dfs(newAdj, n, vis)
for i in range(v):
if len(newAdj[i]) > 0 and vis[i] == False:
return False
return True
def isCircle(self, n, A):
v = 26
adj = [[] for i in range(26)]
inp = [0] * 26
for s in A:
ind1, ind2 = ord(s[0]) - ord("a"), ord(s[len(s) - 1]) - ord("a")
adj[ind1].append(ind2)
inp[ind2] += 1
if self.sc(v, adj) == False:
return 0
for i in range(v):
if len(adj[i]) != inp[i]:
return 0
return 1
sys.setrecursionlimit(10**6)
if __name__ == "__main__":
t = int(input())
for _ in range(t):
N = int(input())
A = input().split()
ob = Solution()
print(ob.isCircle(N, A)) | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER RETURN NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Given an array of lowercase strings A[] of size N, determine if the strings can be chained together to form a circle.
A string X can be chained together with another string Y if the last character of X is same as first character of Y. If every string of the array can be chained with exactly two strings of the array(one with the first character and second with the last character of the string), it will form a circle.
For example, for the array arr[] = {"for", "geek", "rig", "kaf"} the answer will be Yes as the given strings can be chained as "for", "rig", "geek" and "kaf"
Example 1:
Input:
N = 3
A[] = { "abc", "bcd", "cdf" }
Output:
0
Explaination:
These strings can't form a circle
because no string has 'd'at the starting index.
Example 2:
Input:
N = 4
A[] = { "ab" , "bc", "cd", "da" }
Output:
1
Explaination:
These strings can form a circle
of strings.
Your Task:
You don't need to read input or print output. Your task is to complete the function isCircle() which takes the length of the array N and the array A as input parameters and returns 1 if we can form a circle or 0 if we cannot.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{4}
1 ≤ Length of strings ≤ 20 | class Solution:
def isCircle(self, N, A):
adj = dict()
in_out_count = dict()
for node in A:
fl = node[0]
ll = node[-1]
if adj.get(fl):
adj[fl].append(ll)
else:
adj[fl] = [ll]
if not adj.get(ll):
adj[ll] = []
in_out_count[fl] = in_out_count.get(fl, 0) + 1
in_out_count[ll] = in_out_count.get(ll, 0) - 1
visited = {}
for key in adj.keys():
visited[key] = False
self.performDFS(key, adj, visited)
for key in visited.keys():
if visited[key] == False:
return 0
for key in in_out_count.keys():
if in_out_count[key] != 0:
return 0
return 1
def performDFS(self, sv, adj, visited):
visited[sv] = True
for a in adj[sv]:
if visited[a] is False:
self.performDFS(a, adj, visited)
return | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR IF FUNC_CALL VAR VAR ASSIGN VAR VAR LIST ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR IF VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN |
Given an array of lowercase strings A[] of size N, determine if the strings can be chained together to form a circle.
A string X can be chained together with another string Y if the last character of X is same as first character of Y. If every string of the array can be chained with exactly two strings of the array(one with the first character and second with the last character of the string), it will form a circle.
For example, for the array arr[] = {"for", "geek", "rig", "kaf"} the answer will be Yes as the given strings can be chained as "for", "rig", "geek" and "kaf"
Example 1:
Input:
N = 3
A[] = { "abc", "bcd", "cdf" }
Output:
0
Explaination:
These strings can't form a circle
because no string has 'd'at the starting index.
Example 2:
Input:
N = 4
A[] = { "ab" , "bc", "cd", "da" }
Output:
1
Explaination:
These strings can form a circle
of strings.
Your Task:
You don't need to read input or print output. Your task is to complete the function isCircle() which takes the length of the array N and the array A as input parameters and returns 1 if we can form a circle or 0 if we cannot.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{4}
1 ≤ Length of strings ≤ 20 | def dfs(node, map1, visited):
temp = map1[node] if node in map1 else []
if len(temp) == 0:
return
visited[node] = 1
for i in temp:
if visited[i] == 0:
dfs(i, map1, visited)
def detectCycle(src, map1, mark):
visited = [0] * 26
dfs(src, map1, visited)
for i in range(26):
if visited[i] == 0 and mark[i] == 1:
return 0
return 1
class Solution:
def isCircle(self, N, A):
if N == 1 and A[0][0] != A[0][-1]:
return 0
map1 = {}
mark = [0] * 26
ed_in, ed_out = [0] * 26, [0] * 26
for x in A:
u, v = ord(x[0]) - ord("a"), ord(x[len(x) - 1]) - ord("a")
if u not in map1:
map1[u] = [v]
else:
map1[u].append(v)
mark[u] = mark[v] = 1
ed_in[v] += 1
ed_out[u] += 1
for i in range(26):
if ed_in[i] != ed_out[i]:
return 0
return detectCycle(ord(A[0][0]) - ord("a"), map1, mark) | FUNC_DEF ASSIGN VAR VAR VAR VAR VAR LIST IF FUNC_CALL VAR VAR NUMBER RETURN ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER RETURN NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER NUMBER BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR STRING VAR VAR |
Given an array of lowercase strings A[] of size N, determine if the strings can be chained together to form a circle.
A string X can be chained together with another string Y if the last character of X is same as first character of Y. If every string of the array can be chained with exactly two strings of the array(one with the first character and second with the last character of the string), it will form a circle.
For example, for the array arr[] = {"for", "geek", "rig", "kaf"} the answer will be Yes as the given strings can be chained as "for", "rig", "geek" and "kaf"
Example 1:
Input:
N = 3
A[] = { "abc", "bcd", "cdf" }
Output:
0
Explaination:
These strings can't form a circle
because no string has 'd'at the starting index.
Example 2:
Input:
N = 4
A[] = { "ab" , "bc", "cd", "da" }
Output:
1
Explaination:
These strings can form a circle
of strings.
Your Task:
You don't need to read input or print output. Your task is to complete the function isCircle() which takes the length of the array N and the array A as input parameters and returns 1 if we can form a circle or 0 if we cannot.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{4}
1 ≤ Length of strings ≤ 20 | class Solution:
def isCircle(self, N, A):
adj = [[] for i in range(26)]
indeg = [(0) for i in range(26)]
for i in A:
adj[ord(i[0]) - 97].append(ord(i[-1]) - 97)
indeg[ord(i[-1]) - 97] += 1
for i in range(26):
if len(adj[i]) != indeg[i]:
return 0
visited = [(False) for i in range(26)]
node = ord(A[0][0]) - 97
self.dfs(node, adj, visited)
for i in range(26):
if adj[i] and not visited[i]:
return 0
return 1
def dfs(self, node, adj, visited):
visited[node] = True
for i in adj[node]:
if not visited[i]:
self.dfs(i, adj, visited) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR |
Given an array of lowercase strings A[] of size N, determine if the strings can be chained together to form a circle.
A string X can be chained together with another string Y if the last character of X is same as first character of Y. If every string of the array can be chained with exactly two strings of the array(one with the first character and second with the last character of the string), it will form a circle.
For example, for the array arr[] = {"for", "geek", "rig", "kaf"} the answer will be Yes as the given strings can be chained as "for", "rig", "geek" and "kaf"
Example 1:
Input:
N = 3
A[] = { "abc", "bcd", "cdf" }
Output:
0
Explaination:
These strings can't form a circle
because no string has 'd'at the starting index.
Example 2:
Input:
N = 4
A[] = { "ab" , "bc", "cd", "da" }
Output:
1
Explaination:
These strings can form a circle
of strings.
Your Task:
You don't need to read input or print output. Your task is to complete the function isCircle() which takes the length of the array N and the array A as input parameters and returns 1 if we can form a circle or 0 if we cannot.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{4}
1 ≤ Length of strings ≤ 20 | import sys
class Solution:
def getT(adj):
tpg = [[] for i in range(26)]
for i in range(26):
for j in adj[i]:
tpg[j].append(i)
return tpg
def bfs(adj, visited, i):
visited[i] = True
for j in adj[i]:
if visited[j] == False:
Solution.bfs(adj, visited, j)
def isCircle(self, N, A):
adj = [[] for i in range(26)]
indeg = [(0) for i in range(26)]
for word in A:
i = ord(word[0]) % 26
j = ord(word[-1]) % 26
adj[i].append(j)
indeg[j] += 1
tpg = Solution.getT(adj)
visited = [(False) for i in range(26)]
n = 0
for i in range(26):
if len(tpg[i]) > 0:
n = i
break
Solution.bfs(tpg, visited, n)
for i in range(26):
if len(tpg[i]) > 0 and visited[i] == False:
return 0
visited = [(False) for i in range(26)]
Solution.bfs(adj, visited, n)
for i in range(26):
if len(adj[i]) > 0 and visited[i] == False:
return 0
for i in range(26):
if len(adj[i]) != indeg[i]:
return 0
return 1
sys.setrecursionlimit(10**6)
if __name__ == "__main__":
t = int(input())
for _ in range(t):
N = int(input())
A = input().split()
ob = Solution()
print(ob.isCircle(N, A)) | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Given an array of lowercase strings A[] of size N, determine if the strings can be chained together to form a circle.
A string X can be chained together with another string Y if the last character of X is same as first character of Y. If every string of the array can be chained with exactly two strings of the array(one with the first character and second with the last character of the string), it will form a circle.
For example, for the array arr[] = {"for", "geek", "rig", "kaf"} the answer will be Yes as the given strings can be chained as "for", "rig", "geek" and "kaf"
Example 1:
Input:
N = 3
A[] = { "abc", "bcd", "cdf" }
Output:
0
Explaination:
These strings can't form a circle
because no string has 'd'at the starting index.
Example 2:
Input:
N = 4
A[] = { "ab" , "bc", "cd", "da" }
Output:
1
Explaination:
These strings can form a circle
of strings.
Your Task:
You don't need to read input or print output. Your task is to complete the function isCircle() which takes the length of the array N and the array A as input parameters and returns 1 if we can form a circle or 0 if we cannot.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{4}
1 ≤ Length of strings ≤ 20 | def dfs(adj, u, vis):
vis[u] = 1
for i in adj[u]:
if vis[i] == 0:
dfs(adj, i, vis)
class Solution:
def isCircle(self, N, A):
adj = [[] for i in range(26)]
adj_reverse = [[] for i in range(26)]
for s in A:
u = ord(s[0]) - ord("a")
v = ord(s[-1]) - ord("a")
adj[u].append(v)
adj_reverse[v].append(u)
indeg = [0] * 26
for i in range(26):
for j in adj[i]:
indeg[j] += 1
for i in range(26):
if len(adj[i]) and len(adj[i]) != indeg[i]:
return 0
n = 0
for i in range(26):
if indeg[i]:
n = i
break
vis = [0] * 26
dfs(adj, n, vis)
for i in range(26):
if vis[i] == 0 and len(adj_reverse[i]):
return 0
return 1 | FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER |
Given an array of lowercase strings A[] of size N, determine if the strings can be chained together to form a circle.
A string X can be chained together with another string Y if the last character of X is same as first character of Y. If every string of the array can be chained with exactly two strings of the array(one with the first character and second with the last character of the string), it will form a circle.
For example, for the array arr[] = {"for", "geek", "rig", "kaf"} the answer will be Yes as the given strings can be chained as "for", "rig", "geek" and "kaf"
Example 1:
Input:
N = 3
A[] = { "abc", "bcd", "cdf" }
Output:
0
Explaination:
These strings can't form a circle
because no string has 'd'at the starting index.
Example 2:
Input:
N = 4
A[] = { "ab" , "bc", "cd", "da" }
Output:
1
Explaination:
These strings can form a circle
of strings.
Your Task:
You don't need to read input or print output. Your task is to complete the function isCircle() which takes the length of the array N and the array A as input parameters and returns 1 if we can form a circle or 0 if we cannot.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{4}
1 ≤ Length of strings ≤ 20 | class Solution:
def isCircle(self, N, A):
start = {}
end = {}
same_letter = {}
for string in A:
start_letter, end_letter = string[0], string[-1]
if start_letter == end_letter:
if start_letter not in same_letter:
same_letter[start_letter] = 0
same_letter[start_letter] += 1
else:
if start_letter not in start:
start[start_letter] = 0
if end_letter not in end:
end[end_letter] = 0
start[start_letter] += 1
end[end_letter] += 1
if len(same_letter) == 1 and not start and not end:
return 1
for letter in same_letter:
if letter not in start:
return 0
return int(start == end) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR RETURN NUMBER FOR VAR VAR IF VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR |
Given an array of lowercase strings A[] of size N, determine if the strings can be chained together to form a circle.
A string X can be chained together with another string Y if the last character of X is same as first character of Y. If every string of the array can be chained with exactly two strings of the array(one with the first character and second with the last character of the string), it will form a circle.
For example, for the array arr[] = {"for", "geek", "rig", "kaf"} the answer will be Yes as the given strings can be chained as "for", "rig", "geek" and "kaf"
Example 1:
Input:
N = 3
A[] = { "abc", "bcd", "cdf" }
Output:
0
Explaination:
These strings can't form a circle
because no string has 'd'at the starting index.
Example 2:
Input:
N = 4
A[] = { "ab" , "bc", "cd", "da" }
Output:
1
Explaination:
These strings can form a circle
of strings.
Your Task:
You don't need to read input or print output. Your task is to complete the function isCircle() which takes the length of the array N and the array A as input parameters and returns 1 if we can form a circle or 0 if we cannot.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{4}
1 ≤ Length of strings ≤ 20 | class Solution:
def isCircle(self, N, A):
adj = [[] for i in range(26)]
isVisited = [False] * 26
vertexSet = set()
inDegree = [0] * 26
outDegree = [0] * 26
for str in A:
start = ord(str[0]) - 97
end = ord(str[len(str) - 1]) - 97
vertexSet.add(start)
vertexSet.add(end)
inDegree[end] += 1
outDegree[start] += 1
adj[start].append(end)
startPoint = 0
for i in range(26):
if inDegree[i] != outDegree[i]:
return 0
for i in range(26):
if len(adj[i]) != 0:
startPoint = i
self.dfs(adj, isVisited, startPoint)
visitedNum = 0
for i in range(26):
if isVisited[i]:
visitedNum += 1
return 0 if visitedNum != len(vertexSet) else 1
def dfs(self, adj, isVisited, startPoint):
isVisited[startPoint] = True
for num in adj[startPoint]:
if not isVisited[num]:
self.dfs(adj, isVisited, num) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR FUNC_CALL VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR |
Given an array of lowercase strings A[] of size N, determine if the strings can be chained together to form a circle.
A string X can be chained together with another string Y if the last character of X is same as first character of Y. If every string of the array can be chained with exactly two strings of the array(one with the first character and second with the last character of the string), it will form a circle.
For example, for the array arr[] = {"for", "geek", "rig", "kaf"} the answer will be Yes as the given strings can be chained as "for", "rig", "geek" and "kaf"
Example 1:
Input:
N = 3
A[] = { "abc", "bcd", "cdf" }
Output:
0
Explaination:
These strings can't form a circle
because no string has 'd'at the starting index.
Example 2:
Input:
N = 4
A[] = { "ab" , "bc", "cd", "da" }
Output:
1
Explaination:
These strings can form a circle
of strings.
Your Task:
You don't need to read input or print output. Your task is to complete the function isCircle() which takes the length of the array N and the array A as input parameters and returns 1 if we can form a circle or 0 if we cannot.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{4}
1 ≤ Length of strings ≤ 20 | class Solution:
def isCircle(self, N, A):
a = ord("a")
V = 26
graph = [[] for _ in range(V)]
inv_graph = [[] for _ in range(V)]
for word in A:
graph[ord(word[0]) - a].append(ord(word[-1]) - a)
inv_graph[ord(word[-1]) - a].append(ord(word[0]) - a)
for i in range(V):
if len(graph[i]) != len(inv_graph[i]):
return 0
def dfs(node, graph, visited):
visited[node] = True
for nei in graph[node]:
if not visited[nei]:
dfs(nei, graph, visited)
def checkConnected(graph, node):
visited = [False] * V
dfs(node, graph, visited)
for i in range(V):
if not visited[i] and len(graph[i]) > 0:
return False
return True
node = 0
for node in range(V):
if len(graph[node]) > 0:
break
if not checkConnected(graph, node):
return 0
if not checkConnected(inv_graph, node):
return 0
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER |
Given an array of lowercase strings A[] of size N, determine if the strings can be chained together to form a circle.
A string X can be chained together with another string Y if the last character of X is same as first character of Y. If every string of the array can be chained with exactly two strings of the array(one with the first character and second with the last character of the string), it will form a circle.
For example, for the array arr[] = {"for", "geek", "rig", "kaf"} the answer will be Yes as the given strings can be chained as "for", "rig", "geek" and "kaf"
Example 1:
Input:
N = 3
A[] = { "abc", "bcd", "cdf" }
Output:
0
Explaination:
These strings can't form a circle
because no string has 'd'at the starting index.
Example 2:
Input:
N = 4
A[] = { "ab" , "bc", "cd", "da" }
Output:
1
Explaination:
These strings can form a circle
of strings.
Your Task:
You don't need to read input or print output. Your task is to complete the function isCircle() which takes the length of the array N and the array A as input parameters and returns 1 if we can form a circle or 0 if we cannot.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{4}
1 ≤ Length of strings ≤ 20 | class Solution:
def isCircle(self, N, A):
graph = dict()
for string in A:
src = string[0]
dest = string[-1]
if src in graph:
graph[src].append(dest)
else:
graph[src] = [dest]
degrees = dict()
for string in A:
src = string[0]
dest = string[-1]
if src in degrees:
temp = degrees[src][0] + 1, degrees[src][1]
degrees[src] = temp
else:
degrees[src] = 1, 0
if dest in degrees:
temp = degrees[dest][0], degrees[dest][1] + 1
degrees[dest] = temp
else:
degrees[dest] = 0, 1
for vertex, (inDeg, outDeg) in degrees.items():
if inDeg != outDeg:
return 0
visited = dict()
for vertex in graph.keys():
visited[vertex] = False
srcVertex = A[0][0]
def dfs(srcVertex):
visited[srcVertex] = True
for ngh in graph[srcVertex]:
if visited[ngh] == False:
dfs(ngh)
dfs(srcVertex)
for vertex in visited.keys():
if visited[vertex] == False:
return 0
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR FUNC_CALL VAR IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR IF VAR VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given an array of lowercase strings A[] of size N, determine if the strings can be chained together to form a circle.
A string X can be chained together with another string Y if the last character of X is same as first character of Y. If every string of the array can be chained with exactly two strings of the array(one with the first character and second with the last character of the string), it will form a circle.
For example, for the array arr[] = {"for", "geek", "rig", "kaf"} the answer will be Yes as the given strings can be chained as "for", "rig", "geek" and "kaf"
Example 1:
Input:
N = 3
A[] = { "abc", "bcd", "cdf" }
Output:
0
Explaination:
These strings can't form a circle
because no string has 'd'at the starting index.
Example 2:
Input:
N = 4
A[] = { "ab" , "bc", "cd", "da" }
Output:
1
Explaination:
These strings can form a circle
of strings.
Your Task:
You don't need to read input or print output. Your task is to complete the function isCircle() which takes the length of the array N and the array A as input parameters and returns 1 if we can form a circle or 0 if we cannot.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{4}
1 ≤ Length of strings ≤ 20 | class Solution:
def isCircle(self, N, A):
def dfs(node, vis, d):
vis[node] = 1
for ele in d[node]:
if not vis[ele]:
dfs(ele, vis, d)
alp = "abcdefghijklmnopqrstuvwxyz"
d = {}
for i in range(N):
if A[i][0] not in d:
d[A[i][0]] = [A[i][-1]]
else:
d[A[i][0]] += [A[i][-1]]
if A[i][-1] not in d:
d[A[i][-1]] = [A[i][0]]
else:
d[A[i][-1]] += [A[i][0]]
vis = {}
for ele in alp:
vis[ele] = 0
cnt = 0
for ele in alp:
if not vis[ele] and ele in d:
dfs(ele, vis, d)
cnt += 1
if cnt > 1:
return 0
for ele in alp:
if ele in d:
if len(d[ele]) % 2:
return 0
return 1 | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER LIST VAR VAR NUMBER VAR VAR VAR NUMBER LIST VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER LIST VAR VAR NUMBER VAR VAR VAR NUMBER LIST VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR VAR IF VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given an array of lowercase strings A[] of size N, determine if the strings can be chained together to form a circle.
A string X can be chained together with another string Y if the last character of X is same as first character of Y. If every string of the array can be chained with exactly two strings of the array(one with the first character and second with the last character of the string), it will form a circle.
For example, for the array arr[] = {"for", "geek", "rig", "kaf"} the answer will be Yes as the given strings can be chained as "for", "rig", "geek" and "kaf"
Example 1:
Input:
N = 3
A[] = { "abc", "bcd", "cdf" }
Output:
0
Explaination:
These strings can't form a circle
because no string has 'd'at the starting index.
Example 2:
Input:
N = 4
A[] = { "ab" , "bc", "cd", "da" }
Output:
1
Explaination:
These strings can form a circle
of strings.
Your Task:
You don't need to read input or print output. Your task is to complete the function isCircle() which takes the length of the array N and the array A as input parameters and returns 1 if we can form a circle or 0 if we cannot.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{4}
1 ≤ Length of strings ≤ 20 | import sys
class Solution:
def isCircle(self, N, A):
graph = [[] for k in range(26)]
indegree = [0] * 26
for el in A:
graph[ord(el[0]) - ord("a")].append(ord(el[-1]) - ord("a"))
indegree[ord(el[-1]) - ord("a")] += 1
visited = [False] * 26
trans = [[] for k in range(26)]
def dfs1(v):
visited[v] = True
for k in graph[v]:
if visited[k] == False:
dfs1(k)
def transpose():
for el in A:
trans[ord(el[-1]) - ord("a")].append(ord(el[0]) - ord("a"))
for v in range(26):
if len(graph[v]) > 0:
dfs1(v)
break
for v in range(26):
if len(graph[v]) and visited[v] == False:
return 0
transpose()
visited = [(False) for k in range(26)]
for v in range(26):
if len(trans[v]) > 0:
dfs1(v)
break
for v in range(26):
if len(trans[v]) and visited[v] == False:
return 0
for v in range(26):
if indegree[v] != len(graph[v]):
return 0
return 1
sys.setrecursionlimit(10**6)
if __name__ == "__main__":
t = int(input())
for _ in range(t):
N = int(input())
A = input().split()
ob = Solution()
print(ob.isCircle(N, A)) | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Given an array of lowercase strings A[] of size N, determine if the strings can be chained together to form a circle.
A string X can be chained together with another string Y if the last character of X is same as first character of Y. If every string of the array can be chained with exactly two strings of the array(one with the first character and second with the last character of the string), it will form a circle.
For example, for the array arr[] = {"for", "geek", "rig", "kaf"} the answer will be Yes as the given strings can be chained as "for", "rig", "geek" and "kaf"
Example 1:
Input:
N = 3
A[] = { "abc", "bcd", "cdf" }
Output:
0
Explaination:
These strings can't form a circle
because no string has 'd'at the starting index.
Example 2:
Input:
N = 4
A[] = { "ab" , "bc", "cd", "da" }
Output:
1
Explaination:
These strings can form a circle
of strings.
Your Task:
You don't need to read input or print output. Your task is to complete the function isCircle() which takes the length of the array N and the array A as input parameters and returns 1 if we can form a circle or 0 if we cannot.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{4}
1 ≤ Length of strings ≤ 20 | class Solution:
def conn(self, node, adj):
ref_ver[node] = True
if node in adj:
for x in adj[node]:
if not ref_ver[x]:
self.conn(x, adj)
return
def isCircle(self, N, A):
global ref_ver
adj = {}
ref_ver = {}
ref_in = {}
ref_out = {}
count = 0
for x in A:
ref_ver[x[0]] = False
ref_ver[x[-1]] = False
if x[0] not in adj:
adj[x[0]] = [x[-1]]
else:
adj[x[0]].append(x[-1])
if x[0] not in ref_out:
ref_out[x[0]] = 1
else:
ref_out[x[0]] += 1
if x[-1] not in ref_in:
ref_in[x[-1]] = 1
else:
ref_in[x[-1]] += 1
for x in ref_out:
if x not in ref_in:
return 0
elif ref_in[x] != ref_out[x]:
return 0
for x in ref_ver:
if not ref_ver[x]:
self.conn(x, adj)
count += 1
if count == 1:
return 1
else:
return 0
print(adj, ref_ver) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER IF VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER LIST VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR |
Given an array of lowercase strings A[] of size N, determine if the strings can be chained together to form a circle.
A string X can be chained together with another string Y if the last character of X is same as first character of Y. If every string of the array can be chained with exactly two strings of the array(one with the first character and second with the last character of the string), it will form a circle.
For example, for the array arr[] = {"for", "geek", "rig", "kaf"} the answer will be Yes as the given strings can be chained as "for", "rig", "geek" and "kaf"
Example 1:
Input:
N = 3
A[] = { "abc", "bcd", "cdf" }
Output:
0
Explaination:
These strings can't form a circle
because no string has 'd'at the starting index.
Example 2:
Input:
N = 4
A[] = { "ab" , "bc", "cd", "da" }
Output:
1
Explaination:
These strings can form a circle
of strings.
Your Task:
You don't need to read input or print output. Your task is to complete the function isCircle() which takes the length of the array N and the array A as input parameters and returns 1 if we can form a circle or 0 if we cannot.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{4}
1 ≤ Length of strings ≤ 20 | import sys
class Solution:
def isCircle(self, N, A):
adj = [[] for i in range(26)]
deg = [0] * 26
diff = 97
dic = {}
c = 0
for i in range(N):
inx = ord(A[i][0]) - diff
if inx not in dic:
dic[inx] = 1
c += 1
n = len(A[i])
adj[inx].append(A[i][n - 1])
adj[ord(A[i][n - 1]) - diff].append(A[i][0])
visited = [0] * 26
stack = [0] * 26
def dfs(i):
visited[i] = 1
for k in adj[i]:
if visited[ord(k) - diff] == 0:
dfs(ord(k) - diff)
count = 0
for i in range(26):
if visited[i] == 0 and adj[i]:
dfs(i)
count += 1
if count > 1:
return 0
for i in range(26):
if len(adj[i]) % 2 == 1:
return 0
return 1
sys.setrecursionlimit(10**6)
if __name__ == "__main__":
t = int(input())
for _ in range(t):
N = int(input())
A = input().split()
ob = Solution()
print(ob.isCircle(N, A)) | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Given an array of lowercase strings A[] of size N, determine if the strings can be chained together to form a circle.
A string X can be chained together with another string Y if the last character of X is same as first character of Y. If every string of the array can be chained with exactly two strings of the array(one with the first character and second with the last character of the string), it will form a circle.
For example, for the array arr[] = {"for", "geek", "rig", "kaf"} the answer will be Yes as the given strings can be chained as "for", "rig", "geek" and "kaf"
Example 1:
Input:
N = 3
A[] = { "abc", "bcd", "cdf" }
Output:
0
Explaination:
These strings can't form a circle
because no string has 'd'at the starting index.
Example 2:
Input:
N = 4
A[] = { "ab" , "bc", "cd", "da" }
Output:
1
Explaination:
These strings can form a circle
of strings.
Your Task:
You don't need to read input or print output. Your task is to complete the function isCircle() which takes the length of the array N and the array A as input parameters and returns 1 if we can form a circle or 0 if we cannot.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{4}
1 ≤ Length of strings ≤ 20 | class Graph(object):
def __init__(self, V):
self.V = V
self.adj = [[] for x in range(V)]
self.inp = [(0) for _ in range(V)]
def addEdge(self, v, w):
self.adj[v].append(w)
self.inp[w] += 1
def isEulerCycle(self):
if not self.isSC():
return False
for i in range(self.V):
if len(self.adj[i]) != self.inp[i]:
return False
return True
def isSC(self):
visited = [(False) for _ in range(self.V)]
n = 0
while n < self.V:
if len(self.adj[n]) > 0:
break
n += 1
self.DFSUtil(n, visited)
for i in range(self.V):
if len(self.adj[i]) > 0 and not visited[i]:
return False
reverse_graph = self.getTranspose()
visited = [(False) for _ in range(self.V)]
reverse_graph.DFSUtil(n, visited)
for i in range(self.V):
if len(self.adj[i]) > 0 and not visited[i]:
return False
return True
def DFSUtil(self, vertex, visited):
visited[vertex] = True
for nextVertex in self.adj[vertex]:
if not visited[nextVertex]:
self.DFSUtil(nextVertex, visited)
def getTranspose(self):
newGraph = Graph(self.V)
for parentVertex, curAdj in enumerate(self.adj):
for childVertex in curAdj:
newGraph.addEdge(childVertex, parentVertex)
return newGraph
class Solution:
def isCircle(self, N, string_arr):
NCHAR = 26
graph = Graph(NCHAR)
for string in string_arr:
graph.addEdge(ord(string[0]) - ord("a"), ord(string[-1]) - ord("a"))
return 1 if graph.isEulerCycle() else 0 | CLASS_DEF VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FUNC_DEF IF FUNC_CALL VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING RETURN FUNC_CALL VAR NUMBER NUMBER |
Given an array of lowercase strings A[] of size N, determine if the strings can be chained together to form a circle.
A string X can be chained together with another string Y if the last character of X is same as first character of Y. If every string of the array can be chained with exactly two strings of the array(one with the first character and second with the last character of the string), it will form a circle.
For example, for the array arr[] = {"for", "geek", "rig", "kaf"} the answer will be Yes as the given strings can be chained as "for", "rig", "geek" and "kaf"
Example 1:
Input:
N = 3
A[] = { "abc", "bcd", "cdf" }
Output:
0
Explaination:
These strings can't form a circle
because no string has 'd'at the starting index.
Example 2:
Input:
N = 4
A[] = { "ab" , "bc", "cd", "da" }
Output:
1
Explaination:
These strings can form a circle
of strings.
Your Task:
You don't need to read input or print output. Your task is to complete the function isCircle() which takes the length of the array N and the array A as input parameters and returns 1 if we can form a circle or 0 if we cannot.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{4}
1 ≤ Length of strings ≤ 20 | MAX_CHARS = 26
class Solution:
def dfs(self, u, adj, visited):
visited[u] = True
for v in adj[u]:
if not visited[v]:
self.dfs(v, adj, visited)
def is_all_edges_connected(self, src, adj, mark, V):
visited = [False] * V
self.dfs(src, adj, visited)
for u in range(V):
if mark[u] and not visited[u]:
return False
return True
def isCircle(self, N, A):
V = MAX_CHARS
adj = {u: [] for u in range(V)}
In = [0] * V
out = [0] * V
mark = [False] * V
for i in range(N):
s = A[i]
f = s[0]
l = s[-1]
u = ord(f) - ord("a")
v = ord(l) - ord("a")
mark[u] = True
mark[v] = True
adj[u].append(v)
out[u] += 1
In[v] += 1
for u in range(V):
if In[u] != out[u]:
return 0
src = ord(A[0][0]) - ord("a")
if self.is_all_edges_connected(src, adj, mark, V):
return 1
return 0 | ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER |
Given an array of lowercase strings A[] of size N, determine if the strings can be chained together to form a circle.
A string X can be chained together with another string Y if the last character of X is same as first character of Y. If every string of the array can be chained with exactly two strings of the array(one with the first character and second with the last character of the string), it will form a circle.
For example, for the array arr[] = {"for", "geek", "rig", "kaf"} the answer will be Yes as the given strings can be chained as "for", "rig", "geek" and "kaf"
Example 1:
Input:
N = 3
A[] = { "abc", "bcd", "cdf" }
Output:
0
Explaination:
These strings can't form a circle
because no string has 'd'at the starting index.
Example 2:
Input:
N = 4
A[] = { "ab" , "bc", "cd", "da" }
Output:
1
Explaination:
These strings can form a circle
of strings.
Your Task:
You don't need to read input or print output. Your task is to complete the function isCircle() which takes the length of the array N and the array A as input parameters and returns 1 if we can form a circle or 0 if we cannot.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{4}
1 ≤ Length of strings ≤ 20 | class Solution:
def kosaraju(self, V, adj):
badj = [None] * V
for i in adj:
badj[i] = list(adj[i])
adj = badj
def trans(adj, V):
tr = {}
for i, val in enumerate(adj):
if val:
for V in val:
if V not in tr:
tr[V] = [i]
else:
tr[V].append(i)
return tr
def dfs(u):
vi[u] = 1
v = adj[u]
if v:
for V in v:
if not vi[V]:
dfs(V)
st.append(u)
def dfs_(u):
if c[0] not in ans:
ans[c[0]] = [u]
else:
ans[c[0]].append(u)
vi[u] = 1
if u in adj:
v = adj[u]
for V in v:
if not vi[V]:
dfs(V)
vi = [0] * V
st = []
for i in range(V):
if not vi[i]:
dfs(i)
nadj = trans(adj, V)
adj = nadj
vi = [0] * V
c = [0]
ans = {}
for i in st[::-1]:
if not vi[i]:
dfs_(i)
c[0] += 1
return c[0]
def isCircle(self, N, A):
def graph(A):
adj = {}
ind = [0] * 26
out = [0] * 26
for i in A:
if ord(i[0]) - ord("a") in adj:
adj[ord(i[0]) - ord("a")].add(ord(i[-1]) - ord("a"))
else:
adj[ord(i[0]) - ord("a")] = set([ord(i[-1]) - ord("a")])
ind[ord(i[-1]) - ord("a")] += 1
out[ord(i[0]) - ord("a")] += 1
c = len(adj)
return adj, ind, out, c
adj, ind, out, v = graph(A)
c = self.kosaraju(26, adj)
def degcheck():
for i in range(26):
if ind[i] != out[i]:
return False
return True
flag = degcheck()
if c == 1 and flag:
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER LIST VAR EXPR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST NUMBER ASSIGN VAR DICT FOR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER RETURN VAR NUMBER FUNC_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING FUNC_CALL VAR LIST BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER VAR RETURN NUMBER RETURN NUMBER |
Given an array of lowercase strings A[] of size N, determine if the strings can be chained together to form a circle.
A string X can be chained together with another string Y if the last character of X is same as first character of Y. If every string of the array can be chained with exactly two strings of the array(one with the first character and second with the last character of the string), it will form a circle.
For example, for the array arr[] = {"for", "geek", "rig", "kaf"} the answer will be Yes as the given strings can be chained as "for", "rig", "geek" and "kaf"
Example 1:
Input:
N = 3
A[] = { "abc", "bcd", "cdf" }
Output:
0
Explaination:
These strings can't form a circle
because no string has 'd'at the starting index.
Example 2:
Input:
N = 4
A[] = { "ab" , "bc", "cd", "da" }
Output:
1
Explaination:
These strings can form a circle
of strings.
Your Task:
You don't need to read input or print output. Your task is to complete the function isCircle() which takes the length of the array N and the array A as input parameters and returns 1 if we can form a circle or 0 if we cannot.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{4}
1 ≤ Length of strings ≤ 20 | class Graph:
def __init__(self):
self.adj = [[] for i in range(26)]
self.in_degree = [(0) for i in range(26)]
self.out_degree = [(0) for i in range(26)]
self.no_vertices = 26
class Solution:
def DFS(self, node, adj_lst, visited):
visited[node] = True
for ch in adj_lst[node]:
if not visited[ch]:
self.DFS(ch, adj_lst, visited)
def get_transpose(self, adj_lst):
trans_graph = Graph()
for i in range(0, len(adj_lst)):
for j in range(0, len(adj_lst[i])):
trans_graph.adj[adj_lst[i][j]].append(i)
return trans_graph
def is_a_single_component(self, N, graph_obj):
adj_lst = graph_obj.adj
visited = [(False) for i in range(graph_obj.no_vertices)]
node = -1
for indx in range(0, len(adj_lst)):
if len(adj_lst[indx]) > 0:
node = indx
break
if node == -1:
return False
self.DFS(indx, adj_lst, visited)
for indx in range(0, len(visited)):
if visited[indx] == False and len(adj_lst[indx]) > 0:
return False
for i in range(0, len(visited)):
visited[i] = False
trans_graph = self.get_transpose(adj_lst)
self.DFS(node, trans_graph.adj, visited)
for indx in range(0, len(visited)):
if visited[indx] == False and len(adj_lst[indx]) > 0:
return False
return True
def add_edge(self, adj_lst, a, b):
adj_lst[a].append(b)
def isCircle(self, N, A):
graph_obj = Graph()
for i in range(0, N):
st = ord(A[i][0]) - ord("a")
end = ord(A[i][len(A[i]) - 1]) - ord("a")
self.add_edge(graph_obj.adj, st, end)
graph_obj.in_degree[st] += 1
graph_obj.out_degree[end] += 1
if not self.is_a_single_component(N, graph_obj):
return 0
for i in range(26):
if graph_obj.in_degree[i] != graph_obj.out_degree[i]:
return 0
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def isRepeat(self, s):
for i in range(1, len(s)):
mul = int(len(s) / i)
if s[0:i] * mul == s:
return 1
return 0 | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR RETURN NUMBER RETURN NUMBER |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def isRepeat(self, s):
l = len(s)
for i in range(1, l // 2 + 1):
if l % i == 0:
j = l // i
x = s[:i] * j
if x == s:
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def isRepeat(self, s):
sub_s = ""
for i in range(len(s) - 1):
sub_s += s[i]
construc_s = sub_s * (len(s) // len(sub_s))
if construc_s == s:
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def isRepeat(self, s):
def _r(s):
lps = [0]
le = 0
i = 1
m = len(s)
while i < m:
if s[i] == s[le]:
le += 1
i += 1
lps.append(le)
elif le == 0:
i += 1
lps.append(0)
else:
le = lps[le - 1]
if lps:
return s[0 : lps[len(lps) - 1]], lps.pop()
else:
return "", 0
temp, div = _r(s)
n = len(s)
if div == 0:
return 0
elif n % (n - div) != 0:
return 0
else:
return 1 | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR RETURN VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR RETURN STRING NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def isRepeat(self, s):
temp = ""
cnt = 0
n = len(s)
for i in s:
temp += i
cnt += 1
l = n // cnt
if l > 1 and temp * l == s:
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def isRepeat(self, str1):
n = len(str1)
flag = 0
for i in range(0, len(str1) // 2):
sub = str1[0 : i + 1]
l = len(sub)
exp = n // l
if sub * exp == str1:
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def isRepeat(self, s):
c = 0
for i in s:
n = len(s)
a = s[0 : n // 2 - c]
if len(a) > 0:
x = n // len(a)
c += 1
if a * x == s:
return 1
else:
ans = False
else:
return 0
if ans == False:
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def isRepeat(self, s):
if len(s) <= 1:
return 0
for i in range(len(s) // 2):
sub = s[: i + 1]
currLen = int(len(s) / len(sub))
if sub * currLen == s:
return 1
return 0 | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def zalgo(self, s):
res = [(0) for i in s]
l, r, k = 0, 0, 0
for i in range(1, len(s)):
if i > r:
l, r = i, i
while r < len(s) and s[r] == s[r - l]:
r += 1
res[i] = r - l
r -= 1
elif i + res[i - l] <= r:
res[i] = res[i - l]
else:
l = i
while r < len(s) and s[r] == s[r - l]:
r += 1
res[i] = r - l
r -= 1
return res
def isRepeat(self, s):
z = self.zalgo(s)
l = max(z)
if l == 0:
return 0
if z[-l] == l:
return int(len(s) % (len(s) - l) == 0)
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def isRepeat(self, s):
p = ""
c = 0
n = len(s)
for i in s:
p += i
c += 1
l = n // c
if l > 1 and s == p * l:
return 1
return 0
if __name__ == "__main__":
T = int(input())
for i in range(T):
s = input()
ob = Solution()
answer = ob.isRepeat(s)
print(answer) | CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR BIN_OP VAR VAR RETURN NUMBER RETURN NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def isRepeat(self, s):
for i in range(len(s) // 2):
ch = s[0 : i + 1]
left = len(s) - (i + 1)
if left % len(ch) == 0:
repeat = ch * (left // len(ch) + 1)
if repeat == s:
return 1
return 0 | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def isRepeat(self, s):
r = len(s) // 2
if s[:r] == s[r:]:
return 1
else:
if r != 1:
r -= 1
while r > 0:
if (len(s) - r) % r != 0:
r -= 1
else:
t = s[:r]
i = r
while i < len(s):
if s[i : i + len(t)] != t:
r -= 1
break
i += len(t)
if i == len(s):
return 1
return 0
if __name__ == "__main__":
T = int(input())
for i in range(T):
s = input()
ob = Solution()
answer = ob.isRepeat(s)
print(answer) | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR NUMBER VAR NUMBER WHILE VAR NUMBER IF BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def isRepeat(self, s):
if len(s) <= 1:
return 0
p = ""
c = 0
l = len(s)
for i in s:
p = p + i
c = c + 1
k = l // c
if k > 1 and s == p * k:
return 1
return 0 | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR BIN_OP VAR VAR RETURN NUMBER RETURN NUMBER |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def isRepeat(self, s):
temp = ""
ans = ""
for i in range(len(s) - 1):
temp += s[i]
if len(s) % len(temp) == 0 and temp * (len(s) // len(temp)) == s:
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def isRepeat(self, s):
n = len(s)
for i in range(1, n // 2 + 1):
if n % i:
continue
flag = True
for j in range(i, n, i):
if s[:i] != s[j : j + i]:
flag = False
break
if flag:
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR RETURN NUMBER RETURN NUMBER |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def isRepeat(self, pat):
m = len(pat)
lps = [0] * m
length = 0
i = 1
while i < m:
if pat[i] == pat[length]:
length += 1
lps[i] = length
i += 1
elif length == 0:
lps[i] = 0
i += 1
else:
length = lps[length - 1]
if lps[-1] == 0:
return 0
lengthPat = m - lps[-1]
if m % lengthPat == 0:
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def isRepeat(self, s):
mp = {}
n = len(s)
temp = ""
for i in range(n // 2):
temp += s[i]
if len(s) % len(temp) == 0:
if temp * (len(s) // len(temp)) == s:
return 1
return 0
if __name__ == "__main__":
T = int(input())
for i in range(T):
s = input()
ob = Solution()
answer = ob.isRepeat(s)
print(answer) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def computeLPSArray(self, str, M, lps):
lps[0] = 0
i = 1
length = 0
while i < M:
if str[i] == str[length]:
length += 1
lps[i] = length
i += 1
elif length != 0:
length = lps[length - 1]
else:
lps[i] = 0
i += 1
def isRepeat(self, string):
n = len(string)
lps = [0] * n
Solution.computeLPSArray(self, string, n, lps)
length = lps[n - 1]
if length > 0 and n % (n - length) == 0:
return 1
else:
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def isRepeat(self, s):
factors = []
string_len = len(s)
for i in range(1, len(s)):
if string_len % i == 0:
factors.append(i)
for i in factors:
if s[:i] * int(len(s) / i) == s:
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def isRepeat(self, s):
n = len(s)
k = 0
j = 1
while k + j < n:
if s[k] == s[k + j]:
k += 1
elif s[k] != s[k + j]:
j += 1
k = 0
if k > 0 and n % (n - k) == 0:
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def p(self, s):
pi = [(0) for i in range(len(s))]
q = 0
for i in range(1, len(s)):
while q > 0 and s[q] != s[i]:
q = pi[q - 1]
if s[i] == s[q]:
q += 1
pi[i] = q
return pi
def isRepeat(self, s):
n = len(s)
pi = self.p(s)
x = n - pi[n - 1]
if x != n and n % x == 0:
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def isRepeat(self, s):
l = len(s)
factors = []
k = 1
def check_rep(n):
l = int(len(s) / n)
s2 = ""
s3 = ""
for i in range(0, n):
s2 += s[i]
for t in range(0, l):
s3 += s2
t += 1
if s3 == s:
return 1
else:
return 0
for k in range(1, l):
if l % k == 0:
factors.append(k)
for factor in factors:
if check_rep(factor):
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def isRepeat(self, A):
n = len(A)
lps = [0] * n
for i in range(1, n):
x = lps[i - 1]
while A[x] != A[i]:
if x == 0:
x = -1
break
x = lps[x - 1]
lps[i] = x + 1
if lps[-1] > 0 and n % (n - lps[-1]) == 0:
return 1
else:
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def isRepeat(self, s):
k = 1
i = 0
if len(s) == 2:
if s[i] == s[i + 1]:
return 1
while k < len(s):
if s[i] == s[k]:
i += 1
k += 1
m = k
while k < len(s):
if s[i] == s[k]:
if k == len(s) - 1:
if s[0 : k - i] == s[i + 1 : k + 1]:
return 1
else:
return 0
i += 1
k += 1
else:
i = 0
k = m
break
else:
k += 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def isRepeat(self, s):
def check(s, s1):
k = len(s1)
for i in range(0, len(s), k):
s2 = s[i : i + k]
if s2 != s1:
return False
return True
sum = 0
for i in s:
sum = sum + ord(i)
s1 = ""
checksum = 0
for i in s:
checksum = checksum + ord(i)
s1 = s1 + i
if (
sum % checksum == 0
and len(s) % len(s1) == 0
and s != s1
and check(s, s1)
):
return 1
return 0 | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def isRepeat(self, s):
p = ""
c = 0
d = 0
for j in s:
p += j
c += 1
d = len(s) // c
if d > 1 and s == p * d:
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR VAR RETURN NUMBER RETURN NUMBER |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def isRepeat(self, s):
lp = [0] * len(s)
lp[0] = 0
leni = 0
i = 1
m = len(s)
while i < m:
if s[leni] == s[i]:
lp[i] = leni + 1
i += 1
leni += 1
elif leni != 0:
leni = lp[leni - 1]
else:
lp[i] = 0
i += 1
length = lp[len(s) - 1]
if length > 0 and len(s) % (len(s) - length) == 0:
return 1
return 0
if __name__ == "__main__":
T = int(input())
for i in range(T):
s = input()
ob = Solution()
answer = ob.isRepeat(s)
print(answer) | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def isRepeat(self, S):
if len(S) == 1:
return 0
else:
ans = 0
for i in range(1, len(S) // 2 + 1):
p = s[:i]
if len(S) % len(p) == 0 and S.count(p) == len(S) // len(p):
return 1
return 0
if __name__ == "__main__":
T = int(input())
for i in range(T):
s = input()
ob = Solution()
answer = ob.isRepeat(s)
print(answer) | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def isRepeat(self, s):
n = len(s)
a = []
for i in range(1, n // 2 + 1):
if n % i == 0:
a.append(i)
i = len(a) - 1
while i >= 0:
j = 0
while (j + 2) * a[i] <= len(s) and s[j * a[i] : (j + 1) * a[i]] == s[
(j + 1) * a[i] : (j + 2) * a[i]
]:
j += 1
if (j + 2) * a[i] > len(s):
return 1
i -= 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR RETURN NUMBER VAR NUMBER RETURN NUMBER |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def isRepeat(self, s):
for i in range(1, len(s) // 2 + 1):
if len(s) % i == 0:
substring = s[:i]
if substring * (len(s) // len(substring)) == s:
return 1
return 0 | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def isRepeat(self, s):
n = len(s)
for i in range(1, n):
if n % i == 0 and s[:i] * (n // i) == s:
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def isRepeat(self, s):
if len(s) <= 1:
return 0
else:
def rep(sub, s):
re = ""
n = len(s) // len(sub)
re = sub * n
if re == s:
return True
else:
return False
c = 0
for i in range(len(s) // 2):
sub = s[0 : i + 1]
ans = rep(sub, s)
if ans:
c = 1
break
if c == 1:
return 1
else:
return 0 | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | def is_possible(string, pat):
if len(string) == 0:
return 1
if len(string) < len(pat):
return 0
pat_len = len(pat)
if string[:pat_len] != pat:
return 0
return is_possible(string[pat_len:], pat)
class Solution:
def isRepeat(self, s):
s_len = len(s)
for i in range(1, s_len):
if s_len % i != 0:
continue
if is_possible(s[i:], s[:i]):
return 1
return 0 | FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def isRepeat(self, s):
if len(s) <= 1:
return 0
else:
sub_s = ""
len_s = len(s)
for i in range(0, int(len_s / 2)):
sub_s += s[i]
check_s = sub_s * int(len_s / len(sub_s))
if check_s == s:
return 1
return 0 | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def isRepeat(self, s):
l = len(s)
for i in range(l // 2, 0, -1):
if l % i == 0:
r = l // i
st = s[0:i]
m = ""
while r != 0:
m = m + st
r = r - 1
if m == s:
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR STRING WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def calc(self, s):
n = len(s)
lps = [0] * n
for i in range(1, n):
j = lps[i - 1]
while j > 0 and s[j] != s[i]:
j = lps[j - 1]
if s[i] == s[j]:
j += 1
lps[i] = j
return lps
def isRepeat(self, s):
lps = self.calc(s)
n = len(s)
return 1 if lps[n - 1] != 0 and n % (n - lps[n - 1]) == 0 else 0 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def isRepeat(self, s):
M = len(s)
lps = [0] * M
length = 0
lps[0] = 0
i = 1
while i < M:
if s[i] == s[length]:
length += 1
lps[i] = length
i += 1
elif length != 0:
length = lps[length - 1]
else:
lps[i] = 0
i += 1
length = lps[M - 1]
return 1 if length > 0 and M % (M - length) == 0 else 0 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def isRepeat(self, s):
for l in range(len(s) // 2, 0, -1):
if len(s) % l == 0:
i = 0
while i + l < len(s) and s[i] == s[i + l]:
i += 1
if l + i == len(s):
return 1
return 0 | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def isRepeat(self, s):
n = len(s)
for i in range(1, n):
if n % i == 0:
substring = s[:i]
new_str = substring
while len(new_str) < n:
new_str += substring
if new_str == s:
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def isRepeat(self, s):
r = len(s) // 2
if s[:r] == s[r:]:
return 1
else:
if r != 1:
r -= 1
while r > 0:
if (len(s) - r) % r != 0:
r -= 1
else:
t = s[:r]
i = r
while i < len(s):
if s[i : i + len(t)] != t:
r -= 1
break
i += len(t)
if i == len(s):
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR NUMBER VAR NUMBER WHILE VAR NUMBER IF BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def constructLPS(self, s):
n = len(s)
lps = [0] * n
l = 0
i = 1
while i < n:
if s[i] == s[l]:
l += 1
lps[i] = l
i += 1
elif l != 0:
l = lps[l - 1]
else:
lps[i] = 0
i = i + 1
return lps
def isRepeat(self, s):
lps = self.constructLPS(s)
last = lps[-1]
res = last > 0 and len(s) % (len(s) - last) == 0
return 1 if res else 0
if __name__ == "__main__":
T = int(input())
for i in range(T):
s = input()
ob = Solution()
answer = ob.isRepeat(s)
print(answer) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN VAR NUMBER NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def isRepeat(self, s):
cur = s[0]
k = len(s) // 2
for i in range(1, k + 1):
if cur * (len(s) // i) == s:
return 1
else:
cur += s[i]
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR VAR RETURN NUMBER VAR VAR VAR RETURN NUMBER |
Given a string s, the task is to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "ababab"
Output: 1
Explanation: It is contructed by
appending "ab" 3 times
Example 2:
Input: s = "ababac"
Output: 0
Explanation: Not possible to construct
User Task:
Your task is to complete the function isRepeat() which takes a single string as input and returns 1 if possible to construct, otherwise 0. You do not need to take any input or print anything.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 <= |s| <= 10^{5} | class Solution:
def isRepeat(self, s):
len_s = len(s)
if len_s <= 1:
return 0
i = len_s // 2
j = 0
while j < i:
if s[: j + 1] * (len_s // (j + 1)) == s:
return 1
j += 1
return 0
if __name__ == "__main__":
T = int(input())
for i in range(T):
s = input()
ob = Solution()
answer = ob.isRepeat(s)
print(answer) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN NUMBER VAR NUMBER RETURN NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Given an unsorted array arr[ ] of size n, you need to find the maximum difference of absolute values of elements and indexes, i.e., for i <= j, calculate maximum of | arr[ i ] - arr[ j ] | + | i - j |.
Example 1:
Input :
n = 3
arr[ ] = {1, 3, -1}
Output: 5
Explanation:
Maximum difference comes from indexes
1, 2 i.e | 3 - (-1) | + | 1 - 2 | = 5
Example 2:
Input :
n = 4
arr[ ] = {5, 9, 2, 6}
Output: 8
Explanation:
Maximum difference comes from indexes
1, 2 i.e | 9 - 2 | + | 1 - 2 | = 8
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function maxDistance() that takes an array (arr), sizeOfArray (n), and return the maximum difference as given in the question. The driver code takes care of the printing.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 <= n <= 5*(10^5)
-10^6 <= arr[ i ] <= 10^6 | import sys
class Solution:
def maxDistance(self, arr, n):
max1 = -sys.maxsize
min1 = sys.maxsize
max2 = -sys.maxsize
min2 = sys.maxsize
for i in range(n):
max1 = max(max1, arr[i] - i)
min1 = min(min1, arr[i] - i)
max2 = max(max2, arr[i] + i)
min2 = min(min2, arr[i] + i)
ans = max(max1 - min1, max2 - min2)
return ans | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR |
Given an unsorted array arr[ ] of size n, you need to find the maximum difference of absolute values of elements and indexes, i.e., for i <= j, calculate maximum of | arr[ i ] - arr[ j ] | + | i - j |.
Example 1:
Input :
n = 3
arr[ ] = {1, 3, -1}
Output: 5
Explanation:
Maximum difference comes from indexes
1, 2 i.e | 3 - (-1) | + | 1 - 2 | = 5
Example 2:
Input :
n = 4
arr[ ] = {5, 9, 2, 6}
Output: 8
Explanation:
Maximum difference comes from indexes
1, 2 i.e | 9 - 2 | + | 1 - 2 | = 8
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function maxDistance() that takes an array (arr), sizeOfArray (n), and return the maximum difference as given in the question. The driver code takes care of the printing.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 <= n <= 5*(10^5)
-10^6 <= arr[ i ] <= 10^6 | class Solution:
def maxDistance(self, arr, n):
max_diff = 0
min_diff = 0
start_i = 0
for end_i in range(len(arr)):
max_diff = max(max_diff, arr[end_i] - arr[start_i] + end_i - start_i)
if arr[end_i] - arr[start_i] + end_i - start_i <= 0:
start_i = end_i
start_i
for end_i in range(len(arr)):
min_diff = min(min_diff, arr[end_i] - arr[start_i] - end_i + start_i)
if arr[end_i] - arr[start_i] - end_i + start_i >= 0:
start_i = end_i
return max(max_diff, -min_diff) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given an unsorted array arr[ ] of size n, you need to find the maximum difference of absolute values of elements and indexes, i.e., for i <= j, calculate maximum of | arr[ i ] - arr[ j ] | + | i - j |.
Example 1:
Input :
n = 3
arr[ ] = {1, 3, -1}
Output: 5
Explanation:
Maximum difference comes from indexes
1, 2 i.e | 3 - (-1) | + | 1 - 2 | = 5
Example 2:
Input :
n = 4
arr[ ] = {5, 9, 2, 6}
Output: 8
Explanation:
Maximum difference comes from indexes
1, 2 i.e | 9 - 2 | + | 1 - 2 | = 8
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function maxDistance() that takes an array (arr), sizeOfArray (n), and return the maximum difference as given in the question. The driver code takes care of the printing.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 <= n <= 5*(10^5)
-10^6 <= arr[ i ] <= 10^6 | class Solution:
def maxDistance(self, arr, n):
ans = 0
for index1 in range(n):
for index2 in range(index1 + 1, n):
ans = max(ans, abs(arr[index1] - arr[index2]) + abs(index1 - index2))
return ans
def maxDistance(self, arr, n):
max1 = -32767
max2 = -32767
min1 = 32767
min2 = 32767
if n == 1:
return 0
for index in range(n):
max1 = max(max1, arr[index] + index)
min1 = min(min1, arr[index] + index)
max2 = max(max2, arr[index] - index)
min2 = min(min2, arr[index] - index)
ans = max(max1 - min1, max2 - min2)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR |
Given an unsorted array arr[ ] of size n, you need to find the maximum difference of absolute values of elements and indexes, i.e., for i <= j, calculate maximum of | arr[ i ] - arr[ j ] | + | i - j |.
Example 1:
Input :
n = 3
arr[ ] = {1, 3, -1}
Output: 5
Explanation:
Maximum difference comes from indexes
1, 2 i.e | 3 - (-1) | + | 1 - 2 | = 5
Example 2:
Input :
n = 4
arr[ ] = {5, 9, 2, 6}
Output: 8
Explanation:
Maximum difference comes from indexes
1, 2 i.e | 9 - 2 | + | 1 - 2 | = 8
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function maxDistance() that takes an array (arr), sizeOfArray (n), and return the maximum difference as given in the question. The driver code takes care of the printing.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 <= n <= 5*(10^5)
-10^6 <= arr[ i ] <= 10^6 | class Solution:
def maxDistance(self, arr, n):
l = []
m = []
for i in range(n):
l.append(arr[i] + i)
m.append(arr[i] - i)
k = max(l) - min(l)
e = max(m) - min(m)
return max(k, e) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given an unsorted array arr[ ] of size n, you need to find the maximum difference of absolute values of elements and indexes, i.e., for i <= j, calculate maximum of | arr[ i ] - arr[ j ] | + | i - j |.
Example 1:
Input :
n = 3
arr[ ] = {1, 3, -1}
Output: 5
Explanation:
Maximum difference comes from indexes
1, 2 i.e | 3 - (-1) | + | 1 - 2 | = 5
Example 2:
Input :
n = 4
arr[ ] = {5, 9, 2, 6}
Output: 8
Explanation:
Maximum difference comes from indexes
1, 2 i.e | 9 - 2 | + | 1 - 2 | = 8
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function maxDistance() that takes an array (arr), sizeOfArray (n), and return the maximum difference as given in the question. The driver code takes care of the printing.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 <= n <= 5*(10^5)
-10^6 <= arr[ i ] <= 10^6 | class Solution:
def maxDistance(self, arr, n):
mx1 = float("-inf")
mn1 = float("inf")
mx2 = float("-inf")
mn2 = float("inf")
for i in range(n):
mx1 = max(mx1, arr[i] - i)
mn1 = min(mn1, arr[i] - i)
mx2 = max(mx2, arr[i] + i)
mn2 = min(mn2, arr[i] + i)
return max(abs(mx1 - mn1), abs(mx2 - mn2)) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR |
Given an unsorted array arr[ ] of size n, you need to find the maximum difference of absolute values of elements and indexes, i.e., for i <= j, calculate maximum of | arr[ i ] - arr[ j ] | + | i - j |.
Example 1:
Input :
n = 3
arr[ ] = {1, 3, -1}
Output: 5
Explanation:
Maximum difference comes from indexes
1, 2 i.e | 3 - (-1) | + | 1 - 2 | = 5
Example 2:
Input :
n = 4
arr[ ] = {5, 9, 2, 6}
Output: 8
Explanation:
Maximum difference comes from indexes
1, 2 i.e | 9 - 2 | + | 1 - 2 | = 8
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function maxDistance() that takes an array (arr), sizeOfArray (n), and return the maximum difference as given in the question. The driver code takes care of the printing.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 <= n <= 5*(10^5)
-10^6 <= arr[ i ] <= 10^6 | class Solution:
def maxDistance(self, A, num):
case1Max = caseMax = float("-inf")
case2Min = case4Min = float("inf")
for i, num in enumerate(A):
case1Max = max(case1Max, num + i)
case2Min = min(case2Min, num + i)
caseMax = max(caseMax, num - i)
case4Min = min(case4Min, num - i)
return max(case1Max - case2Min, caseMax - case4Min) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR |
Given an unsorted array arr[ ] of size n, you need to find the maximum difference of absolute values of elements and indexes, i.e., for i <= j, calculate maximum of | arr[ i ] - arr[ j ] | + | i - j |.
Example 1:
Input :
n = 3
arr[ ] = {1, 3, -1}
Output: 5
Explanation:
Maximum difference comes from indexes
1, 2 i.e | 3 - (-1) | + | 1 - 2 | = 5
Example 2:
Input :
n = 4
arr[ ] = {5, 9, 2, 6}
Output: 8
Explanation:
Maximum difference comes from indexes
1, 2 i.e | 9 - 2 | + | 1 - 2 | = 8
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function maxDistance() that takes an array (arr), sizeOfArray (n), and return the maximum difference as given in the question. The driver code takes care of the printing.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 <= n <= 5*(10^5)
-10^6 <= arr[ i ] <= 10^6 | import sys
class Solution:
def maxDistance(self, arr, n):
min1 = sys.maxsize
min2 = sys.maxsize
max1 = -sys.maxsize - 1
max2 = -sys.maxsize - 1
if n == 1:
return 0
for i in range(n):
max1 = max(max1, arr[i] + i)
min1 = min(min1, arr[i] + i)
max2 = max(max2, arr[i] - i)
min2 = min(min2, arr[i] - i)
ans = max(abs(max1 - min1), abs(max2 - min2))
return ans | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR |
Given an unsorted array arr[ ] of size n, you need to find the maximum difference of absolute values of elements and indexes, i.e., for i <= j, calculate maximum of | arr[ i ] - arr[ j ] | + | i - j |.
Example 1:
Input :
n = 3
arr[ ] = {1, 3, -1}
Output: 5
Explanation:
Maximum difference comes from indexes
1, 2 i.e | 3 - (-1) | + | 1 - 2 | = 5
Example 2:
Input :
n = 4
arr[ ] = {5, 9, 2, 6}
Output: 8
Explanation:
Maximum difference comes from indexes
1, 2 i.e | 9 - 2 | + | 1 - 2 | = 8
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function maxDistance() that takes an array (arr), sizeOfArray (n), and return the maximum difference as given in the question. The driver code takes care of the printing.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 <= n <= 5*(10^5)
-10^6 <= arr[ i ] <= 10^6 | class Solution:
def maxDistance(self, arr, n):
n = len(arr)
maxi1, maxi2 = -float("inf"), -float("inf")
for i in range(n):
f_i_1 = arr[i] - i
f_i_2 = -arr[i] - i
maxi1 = max(maxi1, f_i_1)
maxi2 = max(maxi2, f_i_2)
maxj1, maxj2 = -float("inf"), -float("inf")
for j in range(n):
f_j_1 = -arr[j] + j
f_j_2 = arr[j] + j
maxj1 = max(maxj1, f_j_1)
maxj2 = max(maxj2, f_j_2)
ans1 = maxi1 + maxj1
ans2 = maxi2 + maxj2
ans = max(ans1, ans2)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an unsorted array arr[ ] of size n, you need to find the maximum difference of absolute values of elements and indexes, i.e., for i <= j, calculate maximum of | arr[ i ] - arr[ j ] | + | i - j |.
Example 1:
Input :
n = 3
arr[ ] = {1, 3, -1}
Output: 5
Explanation:
Maximum difference comes from indexes
1, 2 i.e | 3 - (-1) | + | 1 - 2 | = 5
Example 2:
Input :
n = 4
arr[ ] = {5, 9, 2, 6}
Output: 8
Explanation:
Maximum difference comes from indexes
1, 2 i.e | 9 - 2 | + | 1 - 2 | = 8
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function maxDistance() that takes an array (arr), sizeOfArray (n), and return the maximum difference as given in the question. The driver code takes care of the printing.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 <= n <= 5*(10^5)
-10^6 <= arr[ i ] <= 10^6 | class Solution:
def maxDistance(self, arr, n):
p = []
s = []
for i in range(n):
k = arr[i] - i
d = arr[i] + i
s.append(k)
p.append(d)
p.sort()
s.sort()
x1 = p[len(p) - 1] - p[0]
x2 = s[len(s) - 1] - s[0]
ans = max(x1, x2)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an unsorted array arr[ ] of size n, you need to find the maximum difference of absolute values of elements and indexes, i.e., for i <= j, calculate maximum of | arr[ i ] - arr[ j ] | + | i - j |.
Example 1:
Input :
n = 3
arr[ ] = {1, 3, -1}
Output: 5
Explanation:
Maximum difference comes from indexes
1, 2 i.e | 3 - (-1) | + | 1 - 2 | = 5
Example 2:
Input :
n = 4
arr[ ] = {5, 9, 2, 6}
Output: 8
Explanation:
Maximum difference comes from indexes
1, 2 i.e | 9 - 2 | + | 1 - 2 | = 8
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function maxDistance() that takes an array (arr), sizeOfArray (n), and return the maximum difference as given in the question. The driver code takes care of the printing.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 <= n <= 5*(10^5)
-10^6 <= arr[ i ] <= 10^6 | class Solution:
def maxDistance(self, arr, n):
r0 = arr[0]
rn = arr[n - 1]
v0p = v0m = vnp = vnm = 0
i0p = i0m = 0
inp = inm = n - 1
for i in range(n):
if arr[i] - r0 - i > v0p:
v0p = arr[i] - r0 - i
i0p = i
if arr[i] - r0 + i < v0m:
v0m = arr[i] - r0 + i
i0m = i
if arr[i] - rn - (n - 1 - i) > vnp:
vnp = arr[i] - rn - (n - 1 - i)
inp = i
if arr[i] - rn + (n - 1 - i) < vnm:
vnm = arr[i] - rn + (n - 1 - i)
inm = i
x1 = abs(arr[inp] - arr[i0m]) + abs(inp - i0m)
x2 = abs(arr[inm] - arr[i0p]) + abs(inm - i0p)
return max(x1, x2) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given an unsorted array arr[ ] of size n, you need to find the maximum difference of absolute values of elements and indexes, i.e., for i <= j, calculate maximum of | arr[ i ] - arr[ j ] | + | i - j |.
Example 1:
Input :
n = 3
arr[ ] = {1, 3, -1}
Output: 5
Explanation:
Maximum difference comes from indexes
1, 2 i.e | 3 - (-1) | + | 1 - 2 | = 5
Example 2:
Input :
n = 4
arr[ ] = {5, 9, 2, 6}
Output: 8
Explanation:
Maximum difference comes from indexes
1, 2 i.e | 9 - 2 | + | 1 - 2 | = 8
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function maxDistance() that takes an array (arr), sizeOfArray (n), and return the maximum difference as given in the question. The driver code takes care of the printing.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 <= n <= 5*(10^5)
-10^6 <= arr[ i ] <= 10^6 | class Solution:
def maxDistance(self, arr, n):
while len(arr) > 1:
max1 = -2147483648
max2 = -2147483648
min1 = arr[0] + 0
min2 = arr[0] - 0
sum = 0
diff = 0
for i in range(n):
sum = arr[i] + i
if sum > max1:
max1 = sum
if sum < min1:
min1 = sum
diff = arr[i] - i
if diff > max2:
max2 = diff
if diff < min2:
min2 = diff
return max(max1 - min1, max2 - min2)
return 0 | CLASS_DEF FUNC_DEF WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN NUMBER |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.