Find unique string size - arrays

Given two strings A and B of equal lengths n.
For every index 0<=i<n I can swap characters of A and B.
We need to minimize the number of unique characters in each of A and B.
if num(A) = unique characters in string A
and num(B) = unique characters in string B
after all the operations.
print max(num(A), num(B)).
eg:
1). input: A= ababa B= babab output: 1
2). input: A= abaaa B= baabb output: 2
3). input: A= abcdefb B= bfedcba output: 4
In 3). case, initially max(n(abcdefb),n(bfedcba)) = 6. You can modify the two strings such that they are "bbcdcbb" and "afedefa". String A has 3 unique characters and String B has 4 unique characters. Hence answer is 4. You cannot do better.

Related

Take two integers a,b and print all the even numbers in between them, excluding the input integers

Take two integers a,b and print all the even numbers in between them, excluding the input integers.
Constraints:
0 <= a < b <= 100
Input:
Two integers each in a new line.
Output:
Each line in the output contains an even integer between a,b in ascending order.
Example:
Input:
3
12
Output:
4
6
8
10
I could not find it's logic.
I don't get the question that well, but here's what I got out of it:
for i in range(a + 1, b):
if i % 2 == 0:
print(i)
This loop goes through every letter between your 2 constraints (a and b), checks if its divisible by 2, and if it is (which means it's even), it prints it.

String into Array- efficient way

Given a string : "A,B,C\n1,2,3\n,4,9,6" how can I convert it into this 2D array (in C)?
[A B C
1 2 3
4 9 6]
I thought to count the number of rows (by checking how many \ns there are +1) then count number of columns (by checking how many ',' there are +1) then, to allocate the 2D array. And then to put the values into the array.
Is there any way to do it in more efficient way? because I'm going through the same string many times.

unable to understand the row or column sum matrix problem

I am new here can anyone solve this problem because I didn't understand this question which column and row sum happen???
Row or Column sum
John likes to play with numbers. He is playing a game where he writes numbers starting 1 till N in multiple rows. Initially he chooses a number W and he writes W numbers in each row except probably in the last row when he is done writing till number N. Given N and W and any row or column in this arrangement, print the sum of all the numbers in this row or column.
Input
First line of input will contain a number T = number of test cases. For each test case, the first line will contain two numbers N and W. Another line will contain a number X and letter 'R' or 'C' separated by space. If letter is 'R', print the sum of numbers in row number X , else if letter is 'C', print the sum of numbers in column number X. Rows and columns are numbered in 1-based index ( as 1,2,3,..)
Output
For each test case, print the sum of numbers in the row or column on a single line.
Sample Input
2
5 2
2 C
10 3
4 R
Sample Output
6
10

Taking x number integers and putting them into an array

I need to take x amount of input lines (x is specified by the user) then put them into a 2d array. Each line contains x amount of integers separated by spaces.
For example;
Input:
3
4 3 1
6 5 2
9 7 3
I need to take that input and put them into a 2d array, how do I do this?
Assuming your numbers are separated exactly by one space:
n = int(input('enter size'))
print([[int(i) for i in input().split(' ')]
for __ in range(n)])
Suppose you have your input stored in a file named 'input.txt'
n=2 #number specified by user
with open('input.txt', 'r') as file:
result = [[int(char) for char in lines.split(' ')]for lines in file.read().splitlines()[:2*n:2] ]

How to check if all the entries in columns of a matrix are equal (in MATLAB)?

I have a matrix of growing length for example a 4-by-x matrix A where x is increasing in a loop. I want to find the smallest column c where all columns before that, each, carry one single number. The matrix A can look like:
A = [1 2 3 4;
1 2 3 5;
1 2 3 1;
1 2 3 0];
where c=3, and x=4.
At each iteration of the loop where A grows in length, the value of index c grows as well. Therefore, at each iteration, I want to update the value of c. How efficiently can I code this in Matlab?
Let's say you had the matrix A and you wanted to check a particular column iito see if all its elements are the same. The code would be:
all(A(:, ii)==A(1, ii)) % checks if all elements in column are same as first element
Also, keep in mind that once the condition is broken, x cannot be updated anymore. Therefore, your code should be:
x = 0;
while true
%% expand A by one column
if ~all(A(:, x)==A(1, x)) % true if all elements in column are not the same as first element
break;
end
x = x+1;
end
You could use this:
c = find(arrayfun(#(ind)all(A(1, ind)==A(:, ind)), 1:x), 1, 'first');
This finds the first column where not all values are the same. If you run this in a loop, you can detect when entries in a column start to differ:
for x = 1:maxX
% grow A
c = find(arrayfun(#(ind)~all(A(1, ind)==A(:, ind)), 1:x), 1, 'first');
% If c is empty, all columns have values equal to first row.
% Otherwise, we have to subtract 1 to get the number of columns with equal values
if isempty(c)
c = x;
else
c = c - 1;
end
end
Let me give a try as well:
% Find the columns which's elements are same and sum the logical array up
c = sum(A(1,:) == power(prod(A,1), 1/size(A,1)))
d=size(A,2)
To find the last column such that each column up to that one consists of equal values:
c = find(any(diff(A,1,1),1),1)-1;
or
c = find(any(bsxfun(#ne, A, A(1,:)),1),1)-1;
For example:
>> A = [1 2 3 4 5 6;
1 2 3 5 5 7;
1 2 3 1 5 0;
1 2 3 0 5 8];
>> c = find(any(diff(A,1,1),1),1)-1
c =
3
You can try this (easy and fast):
Equal_test = A(1,:)==A(2,:)& A(2,:)==A(3,:)&A(3,:)==A(4,:);
c=find(Equal_test==false,1,'first')-1;
You can also check the result of find if you want.

Resources