Check Whether a String Is a Valid Integer, Decimal or Scientific-Notation Number

Quick Overview

Decide whether a string is a valid integer, decimal or scientific-notation number under exact rules for signs, dots and exponents. It tests building a small finite state machine or a careful single scan, and handling edge cases such as a lone dot or an exponent with no digits.

Check Whether a String Is a Valid Integer, Decimal or Scientific-Notation Number

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

Given a string `s`, determine whether it represents a valid number. A valid number can be an integer, a decimal, or either of those in scientific notation. Return `True` if `s` is a valid number and `False` otherwise. ### Function Signature ```python def is_number(s: str) -> bool: ``` ### Rules - A valid number is an integer or a decimal, optionally followed by an exponent. - An integer is an optional sign (`+` or `-`) followed by one or more digits. - A decimal is an optional sign followed by one of these: one or more digits and a dot (`4.`), one or more digits, a dot and one or more digits (`4.5`), or a dot and one or more digits (`.5`). - An exponent is `e` or `E` followed by an integer. The exponent may carry its own sign but never a dot. - Nothing else may appear in the string. Leading zeros are allowed. - Valid strings include `0`, `0089`, `-0.1`, `4.`, `.5`, `2E10`, `3e+7` and `-1.5e-3`. - Invalid strings include `.`, `+`, `+.`, `1e`, `e3`, `.e1`, `1e2.5`, `--6`, `1a` and `inf`. ### Constraints - `1 <= len(s) <= 20` - `s` consists only of English letters (uppercase and lowercase), digits `0-9`, plus `+`, minus `-` and dot `.`. ### Examples ```text Input: s = "123" Output: True ``` The string is an integer: no sign, followed by three digits. ```text Input: s = "e3" Output: False ``` An exponent has to follow an integer or a decimal, and here nothing comes before `e`. ```text Input: s = "-.5e+10" Output: True ``` `-.5` is a decimal (a sign, a dot and one digit), and `e+10` is a valid exponent because `+10` is an integer.

Overview: Decide whether a string is a valid integer, decimal or scientific-notation number under exact rules for signs, dots and exponents. It tests building a small finite state machine or a careful single scan, and handling edge cases such as a lone dot or an exponent with no digits.

Nov 24, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Given a string s, determine whether it represents a valid number. A valid number can be an integer, a decimal, or either of those in scientific notation. Return True if s is a valid number and False otherwise.

Function Signature

def is_number(s: str) -> bool:

Rules

  • A valid number is an integer or a decimal, optionally followed by an exponent.
  • An integer is an optional sign ( + or - ) followed by one or more digits.
  • A decimal is an optional sign followed by one of these: one or more digits and a dot ( 4. ), one or more digits, a dot and one or more digits ( 4.5 ), or a dot and one or more digits ( .5 ).
  • An exponent is e or E followed by an integer. The exponent may carry its own sign but never a dot.
  • Nothing else may appear in the string. Leading zeros are allowed.
  • Valid strings include 0 , 0089 , -0.1 , 4. , .5 , 2E10 , 3e+7 and -1.5e-3 .
  • Invalid strings include . , + , +. , 1e , e3 , .e1 , 1e2.5 , --6 , 1a and inf .

Constraints

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

Examples

Input: s = "123"
Output: True

The string is an integer: no sign, followed by three digits.

Input: s = "e3"
Output: False

An exponent has to follow an integer or a decimal, and here nothing comes before e.

Input: s = "-.5e+10"
Output: True

-.5 is a decimal (a sign, a dot and one digit), and e+10 is a valid exponent because +10 is an integer.