FunctionRacerProblemsRace

Well-Formed Sum

EasyStringsPython, JavaScript, TypeScript, Java, C++

Return True if expr is a well-formed sum, otherwise False. A well-formed sum is one or more whole numbers separated by the operators + and -, following all of these rules:

So 12+5-3, 0 and 7-0+100 are well formed, while +12, 12++5, 012, 12- and 1 + 2 are not.

Example 1

expr "12+5-3"
returns true

Three numbers without leading zeros, separated by single operators.

Example 2

expr "+12"
returns false

A leading sign is not allowed.

Example 3

expr "12++5"
returns false

Two operators are next to each other, which leaves an empty number between them.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Strings problems