Valid Number

Hard
0/120
Average time to solve is 15m
profile
Contributed by
4 upvotes
Asked in companies
FacebookScalerVFISLK Global Services

Problem statement

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.

Detailed explanation ( Input/output format, Notes, Images )
Input Format :
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
Constraints :
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
Sample Input 1 :
3
35
.64
e
Sample Output 1 :
Valid
Valid
Invalid
Explanation for Sample Input 1 :
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.
Sample Input 2 :
3
44-
3.E830
.631
Sample Output 2 :
Invalid
Valid
Valid
Hint

keep breaking the string into parts and solve recursively

Approaches (1)
Approach 1

Breaking down the original number into chunks of decimal and integers:

  • A valid number may or may not have ‘e’ or ‘E’ in it.
  • If it does not have ‘e’ or ‘E’, it means the number must be an integer or decimal.
  • If it does have ‘e’ or ‘E’ it means we can divide the string into two parts, the string on the left of ‘e’ will be our prefix string, and the string on the right of ‘e’ will be our suffix string. Neither suffix nor prefix can be empty.
  • The prefix of the string should be either a decimal or an integer and the suffix of the string should be an integer.

 

Now to check if a number is an integer or not:

  • The integer may start with a sign ‘+’ or ‘-’, excluding the sign there should be at least one digit.
  • Check if all the remaining characters are digits only, if they are then the number is an integer.
     

Now to check if a number is a decimal or not:

  • A decimal may start with the sign ‘+’ or ‘-’.
  • A decimal must have ‘.’ in it. If it doesn’t then the number isn’t a decimal.
  • If it does have ..’ it means we can divide the string into two parts, the string on the left of ‘.’ will be our prefix string and the string on the right of ‘.’ will be our suffix string. Both suffix and prefix cannot be empty at the same time.
  • If both prefix and suffix are integers then the number is a decimal else it is not.
     

Algorithm:

  • Create a variable ‘pos’, which stores the position of ‘e’ or ‘E’ in the original string. Also, create a boolean variable ‘valid’ which is true if the number is a valid number else it stores false.
  • If ‘e’ or ‘E’ does not exist in the string then set ‘valid’= isDecimal(s) or isInteger(s), i.e. ‘valid’ will be true if the string ‘s’ is decimal or an integer.
    isInteger(s) returns true if the string ‘s’ is an integer, similarly isDeciaml(s) returns true if ‘s’ is a decimal.
  • If ‘e’ or ‘E’ exists, then divide string ‘s’ in two parts ‘prefix’ and ‘suffix’. Left part of the string from ‘e’ and right part of the string from ‘e’
  • If either of ‘prefix’ and ‘suffix’ is empty, set ‘valid’ = false.
  • Else set boolean variable

valid = ( isDecimal(prefix) or isInteger(prefix) ) and isInteger(suffix)        

  • If valid is true, then return “Valid” else return “Invalid”.
     

Implementing isInteger(s):

  • The integer may start with a sign ‘+’ or ‘-’, excluding the sign there should be at least one digit.
  • Check if all the remaining characters are digits only, if they are then the number is an integer.
     

Implementing isDecimal(s):
 

  • A decimal may start with the sign ‘+’ or ‘-’.
  • Create a variable ‘pos’, which stores the position of ‘.’ in the string.
  • If ‘.’ does not exist in the string then return “false”, which means that the string ’s’ is not a decimal.
  • If ‘.’ exists, then divide string ‘s’ into two parts ‘prefix’ and ‘suffix’. Left part of the string from ‘.’ and right part of the string from ‘.’
  • If ‘prefix’ and ‘suffix’ both are empty then return ‘false’;
  • Else if both ‘prefix’ and ‘suffix’ only have integers return ‘true’, else return ‘false’.
Time Complexity

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)
 

Space Complexity

O(N),  where N is the length of the string


 

We are creating prefix and suffix strings which can be of length O(N)
 

Code Solution
(100% EXP penalty)
Valid Number
Full screen
Console