Valid Number – Python 3 (Week 10 – 14)

class Solution:
    def isNumber(self, s: str) -> bool:
        if len(s) == 0:
            return False
        
        s = s.strip() + " "
        i = 0;
        
        if s[i] == '+' or s[i] == '-':
            i += 1
        
        nDigit, nPoint = 0, 0
        while s[i].isdigit() or s[i] == '.':
            if s[i].isdigit():
                nDigit += 1
            elif s[i] == '.':
                nPoint += 1
            i += 1
        if nDigit <= 0 or nPoint > 1:
            return False
            
        if s[i] == 'e':
            i += 1
            if s[i] == '+' or s[i] == '-':
                i += 1
            if i == len(s) - 1:
                return False;
            while i < len(s) - 1:
                if not s[i].isdigit():
                    return False
                i += 1
         
        return i == len(s) - 1       
            
        
        
        
        
        

Leave a comment

Design a site like this with WordPress.com
Get started