Ambiguous Coordinates - Problem

We had some 2-dimensional coordinates, like (1, 3) or (2, 0.5). Then, we removed all commas, decimal points, and spaces and ended up with the string s.

For example, (1, 3) becomes s = "(13)" and (2, 0.5) becomes s = "(205)".

Return a list of strings representing all possibilities for what our original coordinates could have been.

Our original representation never had extraneous zeroes, so we never started with numbers like "00", "0.0", "0.00", "1.0", "001", "00.01", or any other number that can be represented with fewer digits. Also, a decimal point within a number never occurs without at least one digit occurring before it, so we never started with numbers like ".1".

The final answer list can be returned in any order. All coordinates in the final answer have exactly one space between them (occurring after the comma).

Input & Output

Example 1 — Basic Case
$ Input: s = "(123)"
Output: ["(1, 23)", "(12, 3)", "(1.2, 3)", "(1, 2.3)"]
💡 Note: Split "123" at positions 1 and 2. For split "1|23": (1, 23) and (1, 2.3). For split "12|3": (12, 3) and (1.2, 3). All combinations are valid.
Example 2 — Leading Zero Constraint
$ Input: s = "(00011)"
Output: ["(0.001, 1)", "(0, 0.011)"]
💡 Note: Split positions create parts with leading zeros. "0001" cannot be "0001" or "000.1" (invalid), but "0.001" is valid. "1" is valid, "0011" becomes "0.011".
Example 3 — Trailing Zero Constraint
$ Input: s = "(0123)"
Output: ["(0, 123)", "(0, 12.3)", "(0, 1.23)", "(0.1, 23)", "(0.1, 2.3)", "(0.12, 3)"]
💡 Note: "0" by itself is valid. "123" can be "123", "12.3", "1.23". "01" becomes "0.1" (since "01" has leading zero). "23" can be "23" or "2.3".

Constraints

  • 4 ≤ s.length ≤ 12
  • s[0] == '(' and s[s.length - 1] == ')'
  • The rest of s are digits

Visualization

Tap to expand
Ambiguous Coordinates: Reconstruct Original Format(123)Input String→ Split & Add Decimals →"1" | "23""12" | "3"All Split Positions1, 23, 2.312, 1.2, 3Valid Numbers→ Combine →(1, 23)(1, 2.3)(12, 3)(1.2, 3)Validation Rules:• No leading zeros (except "0")• No trailing zeros after decimalResult: [(1, 23), (1, 2.3), (12, 3), (1.2, 3)]
Understanding the Visualization
1
Input
String with removed commas, spaces, and decimals: (123)
2
Process
Try all ways to split and add decimal points with validation
3
Output
All valid coordinate representations
Key Takeaway
🎯 Key Insight: Systematically try all split positions and decimal placements while validating number format rules
Asked in
Google 25 Amazon 18 Microsoft 15
32.0K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen