Abhishek, a blind man has N distinct binary strings all of the equal lengths. A binary string only contains '0's and '1's. The strings are numbered from 1 to N and all are distinct strings. Abhishek can only differentiate between these strings by touching them. In one touch Abhishek can identify one character at a position of any particular string from the set. Your task is to find the minimum number of touches Abhishek has to make so that he finds that all strings are different.
The first line of input contains an integer T, the number of test cases.
The first line of each test case contains a single integer ‘N’ denoting the number of strings given.
From the second line onwards the next line ‘N’ lines of the test case contains distinct strings.
Output Format:
Return an integer denoting the minimum number of touches Abhishek has to make to distinguish between all given ‘N’ strings.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
3 <= N <= 10
1 <= k <= 100
Where k is the length of the string.
All the strings are distinct.
Time Limit: 1 sec
2
3
11100
11101
01100
2
111
000
5
2
For the first test case :
There are three strings.
First Abhishkek will touch the last character of all strings.
For the first and third-string Abhishek will identify 0 for the first and third strings and for the second string Abhishek will identify 1. In this way, Abhishek will be able to distinguish the 2nd string.
Next Abhishek will touch 1st character of the 1st and 3rd string. Abhishek will identify 1 for 1st string and 0 for the third string. In this way, Abhishek will distinguish 1st and 3rd strings.
Thus the total number of touches Abhishek make is 5.
For the second test case :
There are two strings.
Abhishek will touch the first character of the first and second string. Abhishek will identify 1 for 1st string and 0 for the second string. In this way, Abhishek will distinguish 1st and 2nd strings.
2
4
10010
11010
01010
01100
3
000101
100010
111000
8
5
Try bruteforce using bitmasks
Algorithm:-
O( (2^k)* n *k ) where n is the number of strings and k is the length of the string.
There will be a total of 2^k recursion calls and in each recursion call, we are running a loop of ‘k’ steps for all ‘n’ strings. Thus the time complexity will be ((2^k)*n*k).
O((2^n) ) where n is the number of strings.
The recursion stack will have space complexity O(2^n). hence the space complexity will be (2^n).