Watch 10 video solutions for Valid Number, a hard level problem involving String. This walkthrough by Algorithms Made Easy has 12,402 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given a string s, return whether s is a valid number.
For example, all the following are valid numbers: "2", "0089", "-0.1", "+3.14", "4.", "-.9", "2e10", "-90E3", "3e+7", "+6e-1", "53.5e93", "-123.456e789", while the following are not valid numbers: "abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53".
Formally, a valid number is defined using one of the following definitions:
An integer number is defined with an optional sign '-' or '+' followed by digits.
A decimal number is defined with an optional sign '-' or '+' followed by one of the following definitions:
'.'.'.' followed by digits.'.' followed by digits.An exponent is defined with an exponent notation 'e' or 'E' followed by an integer number.
The digits are defined as one or more digits.
Example 1:
Input: s = "0"
Output: true
Example 2:
Input: s = "e"
Output: false
Example 3:
Input: s = "."
Output: false
Constraints:
1 <= s.length <= 20s consists of only English letters (both uppercase and lowercase), digits (0-9), plus '+', minus '-', or dot '.'.Problem Overview: Given a string, determine whether it represents a valid numeric value. The string may contain digits, optional sign characters (+ or -), decimal points, and scientific notation using e or E. The challenge is validating the structure of the number while handling edge cases like multiple signs, misplaced decimals, or incomplete exponent parts.
Approach 1: Using Regular Expressions (O(n) time, O(1) space)
This approach relies on a carefully constructed regular expression that describes all valid numeric formats. The pattern checks optional leading signs, digits before or after a decimal point, and an optional exponent part like e+10. Once the pattern is defined, you simply run a regex match against the input string. Regex engines scan the string in linear time, so the complexity is O(n), and the memory overhead is constant aside from the compiled pattern. This approach is concise and easy to implement, especially in languages with strong regex support such as Python, Java, and JavaScript. The tradeoff is readability—debugging a complex regex can be difficult during interviews.
Approach 2: Iterative Parsing Approach (O(n) time, O(1) space)
This method scans the string once while tracking key conditions using boolean flags. As you iterate through characters, maintain flags such as seenDigit, seenDot, and seenExponent. Digits are always allowed, but a decimal point can appear only once and only before an exponent. The exponent character (e or E) must appear after at least one digit and resets the requirement that digits must follow. Sign characters are allowed only at the start of the string or immediately after an exponent. By validating each character under these rules, you effectively implement a lightweight parser. This approach is deterministic, interview-friendly, and commonly expected when solving string parsing problems.
Edge cases handled by this scan include inputs like ".1", "3.", "-90E3", and invalid cases such as "1e", "e3", or multiple decimal points. The algorithm only performs a single pass through the string, making it optimal for large inputs.
Recommended for interviews: The iterative parsing approach is typically preferred. It demonstrates your ability to reason about state transitions while processing a string. Regex works well in production code and quick scripts, but interviewers often want to see explicit logic instead of relying on regular expressions. Mentioning both approaches shows breadth: regex for brevity and manual parsing for algorithmic clarity.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Regular Expression Matching | O(n) | O(1) | When the language provides strong regex support and you want a concise validation rule |
| Iterative Parsing with Flags | O(n) | O(1) | Interview scenarios or when explicit validation logic is required |