/65. Valid Number

65. Valid Number

Hard
Strings22.7% acceptance

Given a string input_str, determine if input_str represents a valid number according to the following rules:

- A valid number can be an integer or a decimal, optionally followed by an exponent.

- An integer consists of an optional + or - sign followed by one or more digits.

- A decimal consists of an optional + or - sign, and one of the following:

1. Digits followed by a dot (.).

2. Digits followed by a dot (.) and more digits.

3. A dot (.) followed by digits.

- An exponent is denoted by e or E, followed by an integer (as defined above).

Return True if input_str is a valid number, otherwise return False.

Example 1

Input: 7.5e-2

Output: True

Explanation: Valid decimal with exponent.

Example 2

Input: -.7

Output: True

Explanation: Valid decimal without exponent.

Example 3

Input: 3e+4

Output: True

Explanation: Valid integer with exponent.

Example 4

Input: 4.2.3

Output: False

Explanation: Multiple dots are not allowed.

Example 5

Input: e9

Output: False

Explanation: Exponent must follow a valid number.

Constraints

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

Case 1

Input: '12E5'

Expected: True

Case 2

Input: '--2.5'

Expected: False

Case 3

Input: '0.0e0'

Expected: True

Case 4

Input: '+'

Expected: False

Case 5

Input: '1.2e'

Expected: False

Case 6

Input: '3.14e+0'

Expected: True

Case 7

Input: '1e-1.5'

Expected: False