Search⌘ K
AI Features

Is String a Valid Number?

Understand how to validate whether a given string represents a valid number by applying a state machine method. Explore the process of analyzing each character to transition through states, handling signs, decimals, and invalid inputs. This lesson helps you implement an efficient O(n) time and O(1) space complexity solution for string numeric validation.

Statement

Given an input string, determine if a given string makes a valid number or not.

Notes:

  • For simplicity, assume that white spaces are not present in the input.
  • In case the input string is empty, the function should return TRUE.

Example

Input string

Validity

4.325

Valid number

1.1.1

Not a valid number

222

Valid number

22.

Not a valid number

0.1

Valid number

22.22.

Not a valid number

1.

Not a valid number

Example 1

Sample input

4.325

Expected output

true

Example 2

Sample input

1.1.1

Expected output

false

Try it yourself