Valid Number algo (using c++ regex) timing out.


  • 0
    S

    Hello, my first post. My algorithm to verify if a string input is a valid number is timing out while submitting my solution. It times out at different test cases. I'm using very simple regex matching logic. Not sure if the timeout is set to be too narrow. Dont think the algo is slow (or as slow/efficient as the c++ regex implementation). Core logic is below. Other than that, there is some white-space stripping logic.

            std::vector<std::regex> regExs;
            
            // Handle case where: +1.1e-2, etc
            regExs.emplace_back("(\\+|-)?[[:digit:]]+(\\.([[:digit:]]+))?((e|E)(\\+|-)?[[:digit:]]+)?"); 
            
            // Handle case where +3.e1
            regExs.emplace_back("(\\+|-)?[[:digit:]]+(\\.([[:digit:]]*))?((e|E)(\\+|-)?[[:digit:]]+)?");
                
            // Handle case where +.1e1 or .1 or .1e-1
            regExs.emplace_back("(\\+|-)?\\.[[:digit:]]+((e|E)(\\+|-)?[[:digit:]]+)?");
            
            for(auto regEx : regExs) {
                
                if(std::regex_match(s, regEx))
                    return true;
            }
    
            return false;
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.