Valid Number Algorithm

The Valid Number Algorithm is a computational method used to determine whether a given input string represents a valid number, taking into account various notations, formats, and conventions. This algorithm deals with different representations of numbers, such as integers, floating-point numbers, scientific notations, and numbers with positive or negative signs. It is highly useful in parsing and validating user inputs in various applications, such as programming languages, calculators, data processing tools, and more. By leveraging techniques like finite automata, regular expressions, or simple state machines, the algorithm ensures that the input string adheres to the rules and formats that define a valid number. The core principle of the Valid Number Algorithm involves breaking down the input string into smaller components and validating them individually. For instance, the algorithm checks for the presence of a valid sign (+ or -) at the beginning of the string, followed by a valid sequence of digits, and so on. In the case of floating-point numbers, the algorithm ensures that the decimal point is appropriately placed and followed by a valid sequence of digits. Similarly, for numbers in scientific notation, the algorithm verifies the presence of a valid exponent (e or E) and its associated components. By systematically examining each component and its position within the string, the Valid Number Algorithm efficiently evaluates the validity of the input number, returning a boolean output to indicate whether the number is valid or not.
// NFA
class Solution {
public:
    bool accept(const char* s, int& i, string expected) {
        for (int j = 0; j < expected.size(); j++) {
            if (s[i] == expected[j]) {
                i += 1;
                return true;
            }
        }
        return false;
    }
    
    bool acceptRun(const char* s, int& i, string expected) {
        bool found = false;
        int count = 0;
        while (s[i] != '\0') {
            found = false;
            for (int j = 0; j < expected.size(); j++) {
                if (s[i] == expected[j]) {
                    i++;
                    count++;
                    found = true;
                }
            }
            if (!found) {
                break;
            }
        }
        if (count > 0) {
            return true;
        }
        return false;
    }

    bool isNumber(const char *s) {
        string digits("0123456789");
        
        int i = 0;
        
        acceptRun(s, i, " ");
        
        bool beforedot = false;
        bool afterdot = false;
        
        accept(s, i, "+-");
        beforedot = acceptRun(s, i, digits);
        
        if (accept(s, i, ".")) {
            if (acceptRun(s, i, digits)) {
                afterdot = true;
            }
        }
        
        if (!beforedot && !afterdot) {
            return false;
        }
        
        if (accept(s, i, "eE")) {
            accept(s, i, "+-");
            if (!acceptRun(s, i, digits)) {
                return false;
            }
        }
   
        while (s[i] != '\0') {
            if (s[i] != ' ') {
                return false;
            }
            i++;
        }
        return true;
    }
};

LANGUAGE:

DARK MODE: