Skip to content
Problem 816 · LeetCode 816(opens in a new tab)

Ambiguous Coordinates

MediumBacktracking

Someone wrote down a point as (x, y), where x and y are numbers that may have a decimal point. Then the comma, the space and any decimal points were erased, leaving a string s such as "(1056)".

Return every point the original could have been, written as "(x, y)" with one space after the comma, in any order. The original numbers were written in the shortest normal way: no extra leading zeros (01, 00), no trailing zeros after a decimal point (1.50, 2.0), and a decimal point always has a digit before it (0.5, never .5).

Example 1

Input:  s = "(1056)"
Output: ["(1, 0.56)", "(10, 56)", "(10, 5.6)",
         "(105, 6)", "(1.05, 6)", "(10.5, 6)"]
Why:    three ways to split, then every legal dot placement
2 more examples and the constraints

Example 2

Input:  s = "(0000)"
Output: []
Why:    any part with two or more digits is all zeros

Example 3

Input:  s = "(0100)"
Output: ["(0, 100)"]
Why:    01, 010 and 00 cannot be written legally

Constraints

  • 4 ≤ length of s ≤ 12
  • s starts with ( and ends with )
  • Every other character is a digit

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Build all, then checkO(n⁴)
  2. Generate only valid formsO(n³)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for 10 digitsVerdict
Build all, then checkO(n⁴)
Correct; wastes work
Generate only valid formsO(n³)
Best; nothing thrown away

n = number of digits inside the brackets (at most 10).

Edge cases to test

  • All zeros. "(0000)" has no answer; "(00)" has exactly one, "(0, 0)".
  • A lone 0 is fine. A one-digit part is always legal, even 0.
  • Leading zero. "05" can only be 0.5.
  • Trailing zero. "50" can only be 50, never 5.0.
  • Both ends zero. "010" has no legal form, so its split adds nothing.

Common mistakes

  • Allowing 0.0 or 1.50 because the dot looks fine.
  • Rejecting a lone 0 as a leading zero.
  • Forgetting the space after the comma in the output format.

How to talk through it

  1. 1Clarify. Is 0 itself allowed? Must there be one space after the comma? Is any order fine?
  2. 2Enumerate the splits. n − 1 places to cut the digits into x and y.
  3. 3State the zero rules. Leading zero forces 0.; trailing zero forbids a dot; both means none.
  4. 4Pair the forms. Every left form with every right form, formatted as "(x, y)".
  5. 5Test out loud. Run "(00)", "(0000)" and a split with a part like "05".
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?