Valid Number
Given a string s, return true if it is a valid number. A valid number is an optional sign followed by a decimal (a run of digits containing at most one dot and at least one digit) or an integer, optionally followed by an exponent: 'e' or 'E', an optional sign, and one or more digits. No other characters are allowed.
Open official problem prompt ↗Decide whether a string conforms exactly to the grammar of a signed decimal or integer with an optional scientific exponent.
Like a passport control that checks each field in a fixed order - nationality prefix, main number, optional visa stamp - and rejects the traveler if any field is malformed or if there are extra pages after the last valid one.
- Input
- s = "3.14"
- Output
- true
- Why
- '3.14' is a decimal with one dot and digits present, and it has no exponent, so it is valid.
1 <= s.length <= 20s consists of only English letters (both cases), digits, '+', '-', '.', 'e', and 'E'