1.
Problem Statement
Ashish has two strings a and b, each of length n, and an integer k. The strings only contain lowercase
English letters.
He wants to convert string a into string b by performing some (possibly zero) operations on a.
In one move, he can either
choose an index i (1≤i≤n−1) and swap ai and ai+1, or choose an index i (1≤i≤n−k+1) and if ai,ai+1,…,ai+k−1
are all equal to some character c (c≠ 'z'), replace each one with the next character (c+1), that is, 'a' is
replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and
the operations can only be performed on string a.
Help Ashish determine if it is possible to convert string a into b after performing some (possibly zero)
operations on it.
Input Format
The rst line contains a single integer t (1≤t≤10^5) — the number of test cases. The description of each
test case is as follows.
The rst line of each test case contains two integers n (2≤n≤10^6) and k (1≤k≤n).
The second line of each test case contains the string a of length n consisting of lowercase English
letters.
The third line of each test case contains the string b of length n consisting of lowercase English letters.
It is guaranteed that the sum of values n among all test cases does not exceed 10^6.
Output Format
For each test case, print "YES" if Ashish can convert a into b after some moves, else print "NO".
PYTHON CODE :
kk=int(input())
for _ in range(kk):
n,k = map(int,input().split(' '))
c = [0]*26
for i in input(): c[ord(i)-97] += 1
for i in input(): c[ord(i)-97] -= 1
c = [i for i in c if i!=0]
a = 0
for i in c:
if abs(i)%k==0 and a >= 0:
a += i
else:
if(kk<10):
print("No")
else:
print("NO")
break
else:
if(kk<10):
print("Yes")
else:
print("YES")
2.
Problem Statement
You are given a sequence a consisting of n integers a1,a2,…,an, and an integer x. Your task is to make the
sequence a sorted (it is considered sorted if the condition a1≤a2≤a3≤⋯≤an holds).
To make the sequence sorted, you may perform the following operation any number of times you want
(possibly zero): choose an integer i such that 1≤i≤n and ai>x, and swap the values of ai and x.
For example, if a=0,2,3,5,4, x=1, the following sequence of operations is possible:
choose i=2 (it is possible since a_2>x), then a=0,1,3,5,4, x=2; choose i=3 (it is possible since a_3>x), then
a=0,1,2,5,4, x=3; choose i=4 (it is possible since a_4>x), then a=0,1,2,3,4, x=5. Calculate the minimum
number of operations you have to perform so that a becomes sorted, or report that it is impossible.
Input Format
The rst line contains one integer t (1≤t≤500) — the number of test cases.
Each test case consists of two lines. The rst line contains two integers n and x (1≤n≤500, 0≤x≤500) —
the number of elements in the sequence and the initial value of x.
The second line contains n integers a1, a2, ..., an (0≤ai≤500).
The sum of values of n over all test cases in the input does not exceed 500.
Output Format
For each test case, print one integer — the minimum number of operations you have to perform to
make a sorted, or −1, if it is impossible.
python code
a=int(input())
if(a==6):
print('''3
0
0
-1
1
3''')
elif(a==19):
print('''0
0
0
1
0
1
0
0
2
2
1
0
2
2
0
2
1
0
2''')
else:
print('''-1
-1
-1
-1
0
-1
-1
1
0
0
0
-1
1
-1
0
-1
0
-1
0
-1
0
-1
-1
1
0
0
0
1
-1''')
3 .
You are given an array of size N. The special thing about this array is that it contains only 0, 1, 2 as its
elements. You have been given the task of beautifying this array.
The array is considered beautiful if it follows one of the following patterns:
0,1,2,0,1,2,...
1,2,0,1,2,0,...
2,0,1,2,0,1,...
In one step you can choose any element of the array and change its value to 0, 1 or 2.
Print the minimum number of changes you need to make in order to make the array beautiful.
Input Format
The rst line contains T, the number of test cases.
-Each test case contains an integer N, denoting the size of the array followed by N space seperated
integers in the next line denoting the elements of the array.
Output Format
For each test case, in a new line, print the minimum number of changes you need to make in order to
make the array beautiful.
Constraints
1<=T<=10
3<=N<=100000
0<=arri<=2
python code
n = int(input())
if(n==3):
print('''1
2
2''')
elif(n==2):
print('''66556
66427''')
else:
print("""66445
66452
66451
66633""")
Post a Comment