allcoding1
27.7K subscribers
2.2K photos
2 videos
74 files
850 links
Download Telegram
def count_distinct_strings(S):
distinct_strings = set()

for i in range(len(S) - 1):
new_string = S[:i] + S[i+2:]
distinct_strings.add(new_string)

return len(distinct_strings)

# Read input string
S = input().strip()

# Get the number of distinct strings that can be generated
result = count_distinct_strings(S)
print(result)
👍1
Minimum substring ..
def solve(N, A):
unique_sums = set()

for start in range(N):
current_sum = 0
for end in range(start, N):
current_sum += A[end]
unique_sums.add(current_sum)

print(len(unique_sums))
import sys

det solve(N, A)

for i in range(N):

if A[i]=0;

A[1]

ps=0

M-1

pm={0:-1)

for i in range(N):

ps+=A[i]

if ps in pm:

m=max(m,i-pm[ps])

else:

pm[ps]=i

return m

def main():

Nint(sys.stdin.readline().strip())

A-[]

for_ in range(N):

A.append(int(sys.stdin.readline().strip()))

result = solve(N, A)

print(result)




Largest Subarray with equal number


Infosys
👍3
def max_sum_of_distinct_characters(S):

n = len(S)

left_chars = set()

right_chars = set()

left_count = [0] * n

right_count = [0] * n

for i in range(n):

left_chars.add(S[i])

left_count[i] = len(left_chars)

for i in range(n-1, -1, -1):

right_chars.add(S[i])

right_count[i] = len(right_char)

max_sum = 0

for i in range(n-1):

max_sum = max(max_sum, left_count[i] + right_count[i+1])

return n- max_sum



Split String code

Python 3

All passed
👍3
def max_cost_split(s):
n = len(s)

max_cost = 0

for i in range(1, n):
a = s[:i]
b = s[i:]
cost_a = len(set(a))
cost_b = len(set(b))
max_cost = max(max_cost, cost_a + cost_b)

return n - max_cost




Infosys max cost split code in python
👍1
Minimum unique sum
Equilibrium point Infosys all test cases passed
Minimum difference pairs in python
Sub set with LCM
👎4
Python
👍2
class TreeNode:
def init(self, value=0, left=None, right=None):
self.value = value
self.left = left
self.right = right

def count_nodes(node, counts):
if node is None:
return
if node.value in counts:
counts[node.value] += 1
else:
counts[node.value] = 1
count_nodes(node.left, counts)
count_nodes(node.right, counts)

def find_double_roots(root):
counts = {}
count_nodes(root, counts)
double_roots = [value for value, count in counts.items() if count > 1]
return double_roots

def main():
# Example tree:
# 1
# / \
# 2 3
# / \
# 2 4
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.left = TreeNode(2)
root.left.right = TreeNode(4)

result = find_double_roots(root)
print("Nodes with double roots:", result)

if name == "main":
main()
👍31
def longest_equal_subarray():
n = int(input())
A = [int(input()) for _ in range(n)]
A = [-1 if x == 0 else 1 for x in A]
prefix_sum_map = {}
prefix_sum = 0
max_length = 0

for i in range(n):
prefix_sum += A[i]

if prefix_sum == 0:
max_length = i + 1

if prefix_sum in prefix_sum_map:
max_length = max(max_length, i - prefix_sum_map[prefix_sum])
else:
prefix_sum_map[prefix_sum] = i

return max_length

print(longest_equal_subarray())



Infosys Longest Subarray code
👍4
This is the code


Everyone write neatly

All test cases passed

Python 3 Infosys
Music melodies
Python 3 Music melodies
import sys

def get_ans(N, K, A):



def main():

  N = int(sys.stdin.readline().strip())

  K = int(sys.stdin.readline().strip())

  A = []

  for _ in range(N):

    A.append(int(sys.stdin.readline().strip()))

  result = get_ans(N, K, A)

  print(result)


if name == "main":

  main()


Subset with LCM code in python
👎4👍2
Tree lis Query
👍1