/65. Valid Number

65. Valid Number

Hard
Strings22.7% acceptance

Given a string input_str, determine if it represents a valid number according to the following rules: 1) An integer number may have an optional + or - sign, followed by one or more digits, and may be followed by an optional exponent. 2) A decimal number may have an optional + or - sign, followed by one of: a) digits followed by a dot, b) digits followed by a dot and more digits, c) a dot followed by digits. Both integer and decimal numbers may be followed by an optional exponent. An exponent is e or E followed by an integer number (with optional sign). Return True if input_str is a valid number, otherwise return False.

Example 1

Input: 12.34e+56

Output: True

Explanation: Valid decimal with exponent.

Example 2

Input: -.5E-2

Output: True

Explanation: Valid decimal with exponent and sign.

Example 3

Input: 7e

Output: False

Explanation: Exponent must be followed by integer.

Example 4

Input: ..2

Output: False

Explanation: Multiple dots are not allowed.

Example 5

Input: 3.14.15

Output: False

Explanation: Multiple dots are not allowed.

Constraints

  • 1 <= len(input_str) <= 20
  • input_str consists only of English letters (upper/lowercase), digits (0-9), +, -, or .
Python (current runtime)

Case 1

Input: '123'

Expected: True

Case 2

Input: '4.5E3'

Expected: True

Case 3

Input: '+'

Expected: False

Case 4

Input: '1e-1'

Expected: True

Case 5

Input: '1.2.3'

Expected: False

Case 6

Input: 'e10'

Expected: False

Case 7

Input: '0.0'

Expected: True

Case 8

Input: '--1'

Expected: False