Ambiguous Coordinates - Problem

Imagine you're a forensic data analyst tasked with reconstructing corrupted GPS coordinates! You have a string that represents coordinates where all formatting has been stripped away - no commas, decimal points, or spaces remain.

Your mission: determine all possible valid coordinate pairs that could have produced this string.

The Challenge: Given a string like "(205)", figure out if it could represent coordinates like "(2, 0.5)", "(20, 5)", or "(2.0, 5)".

Important Rules:

  • ๐Ÿšซ No leading zeros in integers (avoid "001", "00.5")
  • ๐Ÿšซ No trailing zeros in decimals (avoid "1.0", "2.50")
  • ๐Ÿšซ No decimals starting with a point (avoid ".5")
  • โœ… Each coordinate must have exactly one space after the comma

Goal: Return a list of all possible valid coordinate representations that could have produced the input string.

Input & Output

example_1.py โ€” Basic Split
$ Input: s = "(123)"
โ€บ Output: ["(1, 23)", "(12, 3)", "(1.2, 3)", "(1, 2.3)"]
๐Ÿ’ก Note: We can split '123' as '1|23' or '12|3'. For '1|23': (1, 23), (1, 2.3). For '12|3': (12, 3), (1.2, 3). All combinations are valid.
example_2.py โ€” Leading Zero Edge Case
$ Input: s = "(0123)"
โ€บ Output: ["(0, 123)", "(0, 12.3)", "(0, 1.23)", "(0.1, 23)", "(0.1, 2.3)", "(0.12, 3)"]
๐Ÿ’ก Note: The first digit '0' must be standalone (no leading zeros for multi-digit). So splits like '01|23' are invalid, but '0|123' works. Decimal '0.1|23' is valid.
example_3.py โ€” Trailing Zero Edge Case
$ Input: s = "(100)"
โ€บ Output: ["(10, 0)", "(1, 00)"]
๐Ÿ’ก Note: Wait, this is wrong! Let me recalculate: '10|0' gives (10, 0) which is valid. '1|00' would be (1, 00) but '00' has leading zeros so it's invalid. Correct answer: ["(10, 0)"]

Visualization

Tap to expand
Ambiguous Coordinates: Step-by-Step ProcessInput String(205)Step 1: Try All Split PositionsSplit 1"2" | "05"Split 2"20" | "5"Step 2: Generate All Valid NumbersSplit 1 AnalysisLeft "2": ["2"] โœ“Right "05": ["05", "0.5"]"05" โŒ (leading zero)"0.5" โœ“Split 2 AnalysisLeft "20": ["20", "2.0"]"20" โœ“"2.0" โŒ (trailing zero)Right "5": ["5"] โœ“Step 3: Combine Valid PairsFrom Split 1(2, 0.5) โœ“From Split 2(20, 5) โœ“Final Result[(2, 0.5), (20, 5)]
Understanding the Visualization
1
Split Candidates
Try splitting the inner digits at each position
2
Number Generation
For each part, generate all valid integer and decimal forms
3
Validation
Check each number against no-leading-zeros and no-trailing-zeros rules
4
Combination
Combine valid left and right parts into coordinate format
Key Takeaway
๐ŸŽฏ Key Insight: Systematically enumerate all splits, then generate valid decimal representations while enforcing the no-leading-zeros and no-trailing-zeros constraints.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยณ)

For each of n possible splits, we try O(n) decimal positions for each part, and O(n) validation time

n
2n
โš  Quadratic Growth
Space Complexity
O(nยฒ)

Storing all possible valid coordinate combinations

n
2n
โš  Quadratic Space

Constraints

  • 4 โ‰ค s.length โ‰ค 12
  • s[0] == '(' and s[s.length - 1] == ')'
  • The rest of s are digits
  • No extraneous leading zeros in original coordinates
  • No trailing zeros in decimal representations
Asked in
Google 35 Facebook 28 Amazon 22 Microsoft 15
23.4K Views
Medium Frequency
~25 min Avg. Time
892 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