Valid Number (FSM)

medium · state-machine, string

Valid Number (FSM)

Given a string s, return true if it represents a valid decimal number. The string is already trimmed (no leading/trailing spaces).

A valid number can be:

  • an integer: "2", "-3"
  • a decimal: "3.14", ".1", "4."
  • with exponent: "2e10", "-90E3", "3e+7"

Invalid examples: "e3", "1e", "99e2.5", "--6", "-+3"

Function signature

func IsValidNumber(s string) bool

Constraints

  • 0 <= len(s) <= 200

Notes

  • Build a small finite‑state machine for digits, decimal point, and exponent.
Run tests to see results
No issues detected