You are given a string, ‘S’. You have to tell whether this string is a valid number or not. If the string is valid print “Valid” else print “Invalid” without quotes.
A number is said to be valid if it can be split in the following two components: i) A decimal number or an integer ii) (Optional) An 'e' or 'E', followed by an integer.
A decimal number can be split up into these components (in order): i) (Optional) A sign character (either '+' or '-') ii) One of the following formats: At least one digit, followed by a dot '.' or At least one digit, followed by a dot '.', followed by at least one digit. or A dot '.', followed by at least one digit.
An integer can be split up into these components (in order): i) (Optional) A sign character (either '+' or '-') ii) At least one digit.
The first line of the input contains ‘T’ denoting the number of test cases.
The first line of each test case contains a string ‘S’.
Output Format :
For each test case return “Valid” if the string is a Valid Number else return “Invalid”.
Note: Don't print anything it has already been taken care of. Just implement the given function
1 <= T <= 5
0 <= |S| <= 2000
Where ‘T’ denotes the number of test cases and |S| denotes the length of string S
Time Limit: 1 second
3
35
.64
e
Valid
Valid
Invalid
In test case 1:
The string is an integer thus it is a valid number.
In test case 2:
The string a decimal where we have a dot (‘.’) followed by at least an integer thus it is a valid number.
In test case 3:
The string is not a valid number as before ‘e’ we should have a decimal or an integer and after ‘e’ we should have an integer.
3
44-
3.E830
.631
Invalid
Valid
Valid
keep breaking the string into parts and solve recursively
Breaking down the original number into chunks of decimal and integers:
Now to check if a number is an integer or not:
Now to check if a number is a decimal or not:
Algorithm:
valid = ( isDecimal(prefix) or isInteger(prefix) ) and isInteger(suffix)
Implementing isInteger(s):
Implementing isDecimal(s):
O(N), where N is the length of the string
In the worst case, we will end up dividing the strings into different strings of length 1, there will be ‘N’ such strings, thus time complexity will be O(N)
O(N), where N is the length of the string
We are creating prefix and suffix strings which can be of length O(N)