65 Valid Number
Last updated
Last updated
Validate if a given string is numeric.
Some examples:
"0"
=>true
" 0.1 "
=>true
"abc"
=>false
"1 a"
=>false
"2e10"
=>true
Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.
The Idea: I definitely view this problem as a valid interview question, but it's validity only comes through the interaction and conversation exchange between interviewer and interviewee. In an OJ setting, the problem is that the 'requirements' that you have to gather are in some cases, subjective, and you end up learning through trail and error in the end. My solution would be to have OJ specify a relatively exhaustive list of examples that the user can this use to formulate a good idea on what these requirements are.
An organized approach (1) to this problem would be to create a state transition diagram that captures all the valid states that can occur from one to another. A valid number is then just one that follows a traversal through this graph and terminates on a valid state.
Complexity: O(n) time where n is the length of the string, and O(1) space