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.
A regular expression can be used to validate if a string is a valid number by matching patterns that define valid numbers. This pattern matches integers with optional signs, decimals, and scientific notation.
C language does not feature built-in regular expression checks. Therefore, this solution leverages the usage of the POSIX regex library to define patterns and match the string against it.
Time Complexity: O(n) due to scanning each character in the string.
Space Complexity: O(1) since we're using a fixed-size pattern.
This approach involves parsing the string based on its characters and their positions. By iterating through the string, the method confirms whether it forms a valid number pattern based on segment rules.
This solution iteratively traverses the characters, checking the rules of valid numbers such as digits, signs, dots, and exponents. It carefully handles different cases to validate the string.
Time Complexity: O(n) where n is the length of the string.
Space Complexity: O(1) using variables to track state.
First, we check if the string starts with a positive or negative sign. If it does, we move the pointer i one step forward. If the pointer i has reached the end of the string at this point, it means the string only contains a positive or negative sign, so we return false.
If the character pointed to by the current pointer i is a decimal point, and there is no number after the decimal point, or if there is an e or E after the decimal point, we return false.
Next, we use two variables dot and e to record the number of decimal points and e or E respectively.
We use pointer j to point to the current character:
e or E has appeared before, return false. Otherwise, we increment dot by one;e or E, and e or E has appeared before, or if the current character is at the beginning or end of the string, return false. Otherwise, we increment e by one; then check if the next character is a positive or negative sign, if it is, move the pointer j one step forward. If the pointer j has reached the end of the string at this point, return false;false.After traversing the string, return true.
The time complexity is O(n), and the space complexity is O(1). Here, n is the length of the string.
| Approach | Complexity |
|---|---|
| Using Regular Expressions | Time Complexity: O(n) due to scanning each character in the string. |
| Iterative Parsing Approach | Time Complexity: O(n) where n is the length of the string. |
| Case Discussion | — |
| 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 |
Valid Number | Live Coding with Explanation | Leetcode - 65 • Algorithms Made Easy • 12,402 views views
Watch 9 more video solutions →Practice Valid Number with our built-in code editor and test cases.
Practice on FleetCode