FastPrepRestricted Decimal Number Validator

Restricted Decimal Number Validator

LinkedIn logoLinkedIn● EasyNEW GRADPHONE SCREEN
Learn

Problem statement

You are given a string toTest. Return true exactly when it represents a number in the restricted decimal grammar below.

  • An optional minus sign - may appear only as the first character.
  • At least one ASCII digit must follow the optional sign.
  • An optional decimal point . may appear at most once, and it must have at least one digit on each side.
  • No other characters, whitespace, exponent notation, or leading plus sign are accepted.

Leading zeros are allowed.

Function

isNumber(toTest: String) → boolean

Examples

Example 1

toTest = "234"return = true

Every character is an ASCII digit.

Example 2

toTest = "-455"return = true

A single leading minus sign is allowed.

Example 3

toTest = "1e2"return = false

Exponent notation is outside the accepted decimal grammar.

Example 4

toTest = "12^2"return = false

The caret is not a digit or the optional decimal point.

Example 5

toTest = "23.23"return = true

There are digits on both sides of the only decimal point.

Example 6

toTest = "2.2.4"return = false

A valid representation may contain at most one decimal point.

Constraints

  • 0 ≤ toTest.length ≤ 100000
  • toTest consists of printable ASCII characters.

More LinkedIn problems

See LinkedIn hiring insights
public boolean isNumber(String toTest) {
    // write your code here
}
toTest"234"
expectedtrue
Checking account…