You had a sequence of consecutive nonnegative integers. You appended all integers at the end of each other to form a string βSβ without any separators. While appending each integer in a string, you forgot to append exactly one integer from the sequence. Now all the integers from a string and you donβt know which integer you have missed.
For example sequence 11, 12, 13 may form a string (without any separators) β1113β if you miss 12.
Your task is to find the missing number in the string such that it is possible to obtain a sequence of consecutive non-negative integers from the given string. If more than one missing integer is present or all the integers are already present or if the string is not valid then the answer will be -1 for all such cases.
Note:1. The string consists of only digits 0 to 9.
2. The numbers will have no more than six digits.
The first line of input contains an integer βTβ denoting the number of test cases.
The only line of each test case contains a string of numbers 'S'.
Output Format:-
For each test case, print a single line containing a single integer denoting the missing number.
The output of each test case will be printed in a separate line.
Note:
You donβt have to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 100
1 <= |S| <= 10 ^ 4
0 <= S[i] <= 9
Where |S| denotes the length of the given string 'S' and S[i] is the digit at index i.
Time Limit: 1 sec.
2
89101113
9899101102
12
100
Test case 1:
If we divide the string in numbers, we can see that 8 9 10 11 13 form a consecutive set of numbers, however with 12 missing.
Test Case 2:-
If we divide the string in numbers we can see that 98 99 101 102 form a consecutive set of numbers, however with 100 missing.
2
012
12131
-1
-1
Try to check all combinations if they are consecutive or not.
O(N), where βNβ is the size of the string.
We are visiting every digit at max of six times in the string.
O(1).
Since we are using constant space.