Description
Validate if a given string can be interpreted as a decimal number.
Some examples:
"0"
=> true
" 0.1 "
=> true
"abc"
=> false
"1 a"
=> false
"2e10"
=> true
" -90e3 "
=> true
" 1e"
=> false
"e3"
=> false
" 6e-1"
=> true
" 99e2.5 "
=> false
"53.5e93"
=> true
" --6 "
=> false
"-+3"
=> false
"95a54e53"
=> false
Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one. However, here is a list of characters that can be in a valid decimal number:
Positive/negative sign - "+"/"-"
Of course, the context of these characters also matters in the input.
Constraints
Approach
Links
Examples
Solutions
Solution 1 Solution 2
Copy /**
* Time complexity :
* Space complexity :
*/
class Solution {
public boolean isNumber(String s) {
s = s.trim();
int n = s.length();
if(n == 0 || isAlphabetic(s.charAt(0)) ||
(n > 0 && isAlphabetic(s.charAt(n-1)))) return false;
Set<Character> sign = Set.of('+', '-');
Set<Character> numbers = Set.of('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
Set<Character> dec = Set.of('.');
Set<Character> exp = Set.of('e');
if(n == 1 && !numbers.contains(s.charAt(0))) return false;
boolean isDigit = false;
boolean isExp = false;
boolean isDec = false;
boolean isSigned = false;
Set<Character> allowedChars = new HashSet();
allowedChars.addAll(sign);
allowedChars.addAll(dec);
allowedChars.addAll(numbers);
for(int i = 0; i < n; i++) {
char ch = s.charAt(i);
if(allowedChars.contains(ch)) {
allowedChars.clear();
if(ch == '+' || ch == '-') {
isSigned = true;
if(isExp && (i+1 >= n || !numbers.contains(s.charAt(i+1)))) return false;
if(!isExp && !isDec) allowedChars.addAll(dec);
allowedChars.addAll(numbers);
} else if(ch == '.') {
if(isSigned && (i+1 >= n || !numbers.contains(s.charAt(i+1)))) return false;
else isSigned = true;
isDec = true;
allowedChars.addAll(exp);
allowedChars.addAll(numbers);
} else if(ch == 'e') {
if(!isDigit) return false;
isExp = true;
isSigned = false;
allowedChars.addAll(sign);
allowedChars.addAll(numbers);
} else {
isSigned = false;
isDigit = true;
if(!isExp) allowedChars.addAll(exp);
if(!isExp && !isDec) allowedChars.addAll(dec);
allowedChars.addAll(numbers);
}
} else {
return false;
}
}
return true;
}
private boolean isAlphabetic(char ch) {
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
}
}
Copy /**
* Time complexity :
* Space complexity :
*/
class Solution {
public boolean isNumber(String s) {
s = s.trim();
int n = s.length(), index = 0;
if(n == 0 || (n == 1 && !isDigit(s.charAt(index)))) return false;
if(index < n && isSign(s.charAt(index))) {
index++;
}
boolean isNum = false;
while(index < n && isDigit(s.charAt(index))) {
index++;
isNum = true;
}
if(index < n && s.charAt(index) == '.') {
index++;
while(index < n && isDigit(s.charAt(index))) {
index++;
isNum = true;
}
}
if(isNum && index < n && s.charAt(index) == 'e') {
index++;
if(index < n && isSign(s.charAt(index))) {
index++;
}
isNum = false;
while(index < n && isDigit(s.charAt(index))) {
index++;
isNum = true;
}
}
return isNum && index == n;
}
private boolean isAlphabetic(char ch) {
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
}
private boolean isDigit(char ch) {
return ch >= '0' && ch <= '9';
}
private boolean isSign(char ch) {
return ch == '+' || ch == '-';
}
}
Follow up