065-valid-number
Question
https://leetcode.com/problems/valid-number/description/
Validate if a given string is numeric.
Example:
"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.
Thought Process
- Trim
- We first trim the leading spaces and trailing spaces
- For each character we visit we decide its validity
- The boolean variable leftSeen and rightSeen track the validity of the number as whole, even when the number is an integer
- For the integer, we can just assign these two variables to true after getting a number
- For the double, we need to have number on at least one side
- For the scientific notation, we need to make sure we get a number before the notation
- The pointSeen and eSeen boolean variable help to track the difference in type of number we are dealing with
- Time complexity O(n)
- Space complexity O(1)
Solution
class Solution {
public boolean isNumber(String s) {
if (s == null) return false;
s = s.trim();
boolean numSeen = false;
boolean pointSeen = false, eSeen = false;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (Character.isDigit(c)) {
numSeen = true;
} else if (c == '.') {
if (pointSeen || eSeen) return false;
pointSeen = true;
} else if (c == 'e') {
if (!numSeen || eSeen) return false;
eSeen = true;
numSeen = false;
} else if (c == '+' || c == '-') {
if (i != 0 && s.charAt(i - 1) != 'e') return false;
} else {
return false;
}
}
return numSeen;
}
}