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 INPUT s = "(123)" String without formatting: ( 1 2 3 ) Digits to split: "123" Valid Number Rules: - No leading zeros: "01" invalid - No trailing zeros after "." - "0" alone is valid - "0.5" is valid - "1.0" is invalid (trailing 0) ALGORITHM STEPS 1 Split into X,Y parts "123" -- (1,23) (12,3) 2 Generate decimals "12" -- "12", "1.2" 3 Validate numbers Check leading/trailing zeros 4 Combine valid pairs Format as "(x, y)" Split Positions for "123": 1 | 23 X=1, Y=23 12 | 3 X=12, Y=3 Then add decimal variants FINAL RESULT All Valid Coordinates: "(1, 23)" OK "(12, 3)" OK "(1.2, 3)" OK "(1, 2.3)" OK Output Array: ["(1, 23)", "(12, 3)", "(1.2, 3)", "(1, 2.3)"] Key Insight: For each way to split the string into two parts (X and Y), generate all possible decimal placements. A number is valid if: (1) it has no leading zeros unless it's "0" or "0.xxx", and (2) decimals have no trailing zeros. Time: O(n^3) for all splits and decimal combinations. Space: O(n^3) for results. TutorialsPoint - Ambiguous Coordinates | Optimal Solution
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