StaffString

Valid Number

Given a string s, determine if it represents a valid number.

A valid number can be described by the following rules:

  • It may be an integer or a decimal number.
  • It may have an optional sign, either + or -, at the beginning.
  • It may contain an exponent part, indicated by e or E, followed by an integer.

An integer is defined as an optional sign followed by one or more digits.

A decimal number is defined as an optional sign followed by one of the following:

  • One or more digits followed by a dot ..
  • One or more digits followed by a dot . followed by one or more digits.
  • A dot . followed by one or more digits.

The exponent part, if present, must contain e or E followed by an integer.

Return true if s is a valid number, otherwise return false.

Example 1
Inputs = "0"
Outputtrue
0 is an integer, so it is a valid number.
Example 2
Inputs = "e"
Outputfalse
e does not contain a valid integer or decimal number before the exponent marker.

Constraints

  • 1 <= s.length <= 20
  • s consists of only English letters (both uppercase and lowercase), digits (0-9), plus '+', minus '-', or dot '.'.

Asked at 7 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate