Problem
Section titled “Problem”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 followed by an optional exponent.
- A decimal number followed by an optional exponent.
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:
- Digits followed by a dot
'.'. - Digits followed by a dot
'.'followed by digits. - A dot
'.'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: trueExample 2:
Input: s = "e"Output: falseExample 3:
Input: s = "."Output: falseConstraints:
-
s.length sconsists of only English letters (both uppercase and lowercase), digits (0-9), plus'+', minus'-', or dot'.'.
Solution approach
Section titled “Solution approach”Intuition
Section titled “Intuition”The grammar is a regular language: it can be recognised by a small finite automaton that reads the string once. Drawn out, the automaton looks like this (green states are accepting states):
Any character with no outgoing arrow from the current state makes the string invalid. Rather than storing the table, we can track the same information with three flags, because the automaton only ever needs to know:
seenDigit— has the current part (mantissa, or exponent after ane) got at least one digit?seenDot— has a.appeared? Only one is allowed, and only in the mantissa.seenExp— has ane/Eappeared? Only one is allowed, and only after a mantissa with digits.
A sign is legal only at the very start or immediately after e/E. Every rule of the grammar maps onto one of these checks, and the string is valid when the scan finishes with seenDigit true (so neither the mantissa nor the exponent is empty).
Algorithm
Section titled “Algorithm”- Set
seenDigit = seenDot = seenExp = false. - For each index
iwith characterc = s[i]:- Digit: set
seenDigit = true. +or-: valid only ifi == 0ors[i - 1]ise/E; otherwise returnfalse.eorE: ifseenExpis already set or the mantissa has no digit (!seenDigit), returnfalse. SetseenExp = trueand resetseenDigit = false, because the exponent needs digits of its own..: ifseenDotorseenExpis set, returnfalse; otherwise setseenDot = true.- Anything else (letters other than
e/E): returnfalse.
- Digit: set
- Return
seenDigit.
Walkthrough
Section titled “Walkthrough”s | Decisive step | Result |
|---|---|---|
"-123.456e789" | sign at i = 0, one dot before e, digits on both sides of e | true |
"4." | one dot, mantissa has digits, no exponent | true |
"1e" | seenDigit reset at e and never set again | false |
"99e2.5" | . after seenExp | false |
"-+3" | + at i = 1, previous char is not e | false |
"." | ends with seenDigit = false | false |
Complexity
Section titled “Complexity”- Time: — one constant-time check per character.
- Space: — three booleans.
Edge cases
Section titled “Edge cases”- A lone
"."or".e1": the mantissa has no digit, so.is accepted bute(or the end of the string) fails onseenDigit. "e3":earrives withseenDigit = falseand is rejected.- Trailing dot before exponent (
"1.e5"): allowed by rule 1 of decimal numbers; the dot comes beforeseenExp, so it passes.
Think before you peek
Work out an approach on your own first — the explanation will still be here when you want it.
class Solution { public: bool isNumber(string s) { bool seenDigit = false, seenDot = false, seenExp = false; for (size_t i = 0; i < s.size(); i++) { char c = s[i]; if (c >= '0' && c <= '9') { seenDigit = true; } else if (c == '+' || c == '-') { if (i > 0 && s[i - 1] != 'e' && s[i - 1] != 'E') return false; } else if (c == 'e' || c == 'E') { if (seenExp || !seenDigit) return false; seenExp = true; seenDigit = false; // the exponent needs its own digits } else if (c == '.') { if (seenDot || seenExp) return false; seenDot = true; } else { return false; } } return seenDigit; }};class Solution { public boolean isNumber(String s) { boolean seenDigit = false, seenDot = false, seenExp = false; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c >= '0' && c <= '9') { seenDigit = true; } else if (c == '+' || c == '-') { if (i > 0 && s.charAt(i - 1) != 'e' && s.charAt(i - 1) != 'E') return false; } else if (c == 'e' || c == 'E') { if (seenExp || !seenDigit) return false; seenExp = true; seenDigit = false; // the exponent needs its own digits } else if (c == '.') { if (seenDot || seenExp) return false; seenDot = true; } else { return false; } } return seenDigit; }}class Solution: def isNumber(self, s: str) -> bool: seen_digit = seen_dot = seen_exp = False for i, c in enumerate(s): if '0' <= c <= '9': seen_digit = True elif c in '+-': if i > 0 and s[i - 1] not in 'eE': return False elif c in 'eE': if seen_exp or not seen_digit: return False seen_exp = True seen_digit = False # the exponent needs its own digits elif c == '.': if seen_dot or seen_exp: return False seen_dot = True else: return False return seen_digitGive it a try first
Attempt the problem on LeetCode — the code will still be here.
