Input: ‘N’ = 3, ‘M’ = 4, ‘STR1’ = xyz, ‘STR2’ = abcd
Output: 1
In this case, ‘STR1[0]>STR2[0]’ hence ‘STR1’ is better than ‘STR2’. Hence the output will be ‘1’.
The first line will contain the integer 'T', the number of test cases.
Each test case consists of two lines.
The first line of input contains two integers, ‘N’ and ‘M’ separated by spaces.
Followed by two lines containing strings ‘STR1’ of length ‘N’ and ‘STR2’ of length ‘M’.
For each test case, print ‘1’ or ‘0’ or ‘-1’ depending on the relation between ‘STR1’ and ‘STR2’.
You don't need to print anything. It has already been taken care of. Just implement the given function.
1 <= ‘T’ <= 10
1 <= ‘N’ <= 10^5
1 <= ‘M’ <= 10^5
‘STR1’ and ‘STR2’ consists of lowercase letters.
It is guaranteed that sum of ‘N’ over all test cases is <= 10^5
It is guaranteed that sum of ‘M’ over all test cases is <= 10^5
Time Limit: 1 sec
The idea for this approach is to iterate on the given string ‘STR1’ and ‘STR2’ with the same pointer and check if the elements at some index are not equal then we return the appropriate answer. And if it remains the same we keep comparing it until the pointer goes out of bound for any one of the strings.
If it goes out of bound for both strings we return ‘0’, else if it exceeds ‘N’ we return ‘-1’, or if it exceeds ‘M’ we return ‘1’.
Algorithm:
// The function will return the relation between ‘STR1’ and ‘STR2’.
Int stringMania(N, M, STR1, STR2)