65. Valid Number

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:

  • Numbers 0-9

  • Exponent - "e"

  • Positive/negative sign - "+"/"-"

  • Decimal point - "."

Of course, the context of these characters also matters in the input.

Constraints

Approach

Examples

"2e10" => true

Solutions

/**
 * 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');
    }
}

Follow up

  • Check whether given floating point number is even or odd - GFG

Last updated

Was this helpful?