In this Leetcode Valid Number problem solution, we have given a string s and we need to return true if string s is a valid number. a valid number can be a number that has a decimal or integer number and optional an 'e' or 'E' that is followed by an integer.

Leetcode Valid Number problem solution


Problem solution in Python.

class Solution(object):
    def isNumber(self, s,t=1,tt=1):
        if t==1: s,i=s.strip(' '),s.strip(' ').find('e')
        if t==1 and i!=-1: return self.isNumber(s[:i],0,1) and self.isNumber(s[i+1:],-1,0)
        if s.count('.')>tt: return False
        return len(s)-len(s.lstrip('+-'))<2 and s.lstrip('+-').replace('.','').isdigit()



Problem solution in Java.

class Solution {
    public boolean isNumber(String s) {
        if (s == null)return false;
        s = s.trim();
        if (s.length() == 0) return false;
        boolean point = false, sign = false, exponent = false, number = false;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (!Character.isDigit(c)) {
                if (c != 'e' && c != '+' && c != '-' && c != '.') return false;
                if (c == '.') {
                    if (point) {
                        return false;
                    } else if (exponent) {
                        return false;
                    } else {
                        point = true;
                    }
                }
                if (c == 'e') {
                    if (exponent) return false;
                    if (!number) return false;
                    exponent = true;
                    number = false;
                }
                if (c == '+' || c == '-') {
                    if (i == 0) {
                        sign = true;
                    } else {
                        if (s.charAt(i - 1) != 'e') return false;
                    }
                }
            } else {
                number = true;
            }
        }
        return number;
    }
}


Problem solution in C++.

bool isNumber(string s) {
        int len = s.length();
        bool sign = false;
        bool num = false;
        bool point = false;
        bool e = false;

        int i = 0;
        while (i < len && s[i] == ' ') i++;
        int j = len-1;
        while(j >=0 && s[j] == ' ') j--;
        
        for(;i <= j; ++i)
        {
            if(s[i] >= '0' && s[i] <= '9')
            {
                num = true;
            }
            else if(s[i] == '+' || s[i] == '-')
            {
                if(num || point)
                    return false;
                if(!sign)
                    sign = true;
                else
                    return false;
            }
            else if(s[i] == '.')
            {
                if(e)
                    return false;
                if(!point)
                    point = true;
                else
                    return false;
            }
            else if(s[i] == 'e')
            {
                if(!num)
                    return false;
                if(!e)
                {
                    e = true;
                    num = false;
                    sign = false;
                    point = false;
                }else
                    return false;
            }
            else
                return false;
        }
        
        return num;
    }


Problem solution in C.

#define EXPONENT 1
#define POINTED 2
#define SIGNED 4
#define NUMBERED 8

bool isNumber(char * s){
    int flags = 0;
    char* ss;
    unsigned long len = strlen(s);
    if(*s == 'e' || s[len - 1] == 'e')
        return false;
beg_trim:
    if(*s == ' '){
        ++s;
        --len;
        if(len == 0)
            return false;
        goto beg_trim;
    }
    
after_trim:
    ss = s;
    if(s[len - 1] == ' '){
        s[len-1] = 0;
        --len;
         if(len == 0)
            return false;
        goto after_trim;
    }
   if(len == 0)
        return false;
    while(*s){
        if(*s == '+' || *s == '-'){
            if(flags & SIGNED)
                return false;
            flags |= SIGNED;
            ++s;
            continue;
        } else if(*s == 'e'){
            if(flags & EXPONENT)
                return false;
            if(!(flags & NUMBERED))
                return false;
            flags |= EXPONENT | POINTED;
            flags &= ~SIGNED;
            flags &= ~NUMBERED;
            ++s; 
            continue;
        } else if(*s == '.'){
            if(flags & POINTED)
                return false;
            flags |= SIGNED;
            flags |= POINTED;
            ++s;
            continue;
        } else if(*s >= '0' && *s <= '9'){
            flags |= SIGNED;
            flags |= NUMBERED;
            ++s;
            continue;
        }
        return false;
    }
    if(len == 1 && ((*s == '+' || *s =='-') || flags & EXPONENT || flags & POINTED))
        return false;
    if( !(flags & NUMBERED) && (flags & EXPONENT))
        return false;
    if(flags * POINTED && !(flags & NUMBERED))
        return false;
    return true;
}