Skip to content

Practice Valid Number

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#65
LeetCode Problem

Valid Number

Problem Statement

Return whether s is a valid numeric string. It may begin with + or -. Its main part must be either one or more digits, digits followed by a decimal point with optional following digits, or a decimal point followed by one or more digits. An optional exponent starts with e or E and then contains an optional sign followed by one or more digits. The exponent cannot contain a decimal point. No other characters or extra signs are allowed. Thus 4., -.9 and 3e+7 are valid, while 1e and 99e2.5 are not.

Source: Valid Number on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
s = "0"
Output
true

# Example 2

Input
s = "e"
Output
false

# Example 3

Input
s = "."
Output
false

Constraints

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

Related LeetCode Problems

Practice more problems similar to Valid Number.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.