One day ninja is thinking of a competition between two strings wherein each round two strings take part and there are some rules of that competition if the strings are able to tell that the minimum numbers of characters they need to change to satisfy at least one of the rules they will be declared as the winner.
Rules for the competition are as follows :
The character of every string of ‘STR1’ is strictly less than every character of every string of ‘STR2’ and vice versa.
‘STR1’ and ‘STR2’ only differ in one distinct character.
So your task is to find the minimum numbers of characters we need to change to satisfy and of the given rule.
Input Format :
The first line contains an integer 'T' which denotes the number of test cases or queries to be run.
The first line of each test case contains two strings ‘STR1’ and ‘STR2’, representing both the strings.
Output Format :
For each test case, print a single line as the minimum number of characters needed to be changed.
The output of each test case will be printed in a separate line.
Note :
You do not need to print anything; it has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 5
1 <= |STR1|, |STR2| <= 5000
Where ‘T’ represents the number of test cases and ‘STR1’ represents the given first string and ‘STR2’ represents the given second string.
Time Limit: 1 second
2
bab cbb
badabb cba
2
3
Test Case 1 :
For the first test case, given ‘STR1 = bab’ and ‘STR2 = cbb’
We can change ‘STR2 = ccc’ by changing both ‘b’ in ‘STR2’ to ‘c’ in two steps.
We can change ‘STR1 = aaa’ and ‘STR2 = bbb’ in three steps.
We can change ‘STR1 = bbb’ and ‘STR2 = bbb’ in two steps.
So we return ‘2’ as it is the minimum number of steps to satisfy one of the given rules.
Test Case 2 :
For this test case, given ‘STR1 = badabb’ and ‘STR2 = cba’
We can change ‘STR2 = eee’ and satisfy the first rule so the minimum number of steps required is ‘3’.
1
ab bb
1
Test Case 1 :
For this test case, we can change ‘STR1 = aa’ so we return ‘1’ as the answer.
Can you check all the given rules and compare their results?
The idea here is to count the number of steps required to make all characters of ‘STR2’ strictly greater than ‘STR1’ and vice versa and also to count the number of steps required to satisfy rule ‘2’ i.e ‘STR1’ and ‘STR2’ only differ by one distinct character and after that, we return the minimum number of steps required from both the condition.
O(L1 + L2), where L1 represents the length of ‘STR1’ and L2 represents the length of ‘STR2’.
As we are running a constant loop from ‘a’ to ‘z’ and for checking both the rules we are running a loop for both the length of the string so overall complexity becomes equal to O(L1 + L2).
O(1).
As no extra space is required.