Formatted question description: https://leetcode.ca/all/816.html

# 816. Ambiguous Coordinates

## Level

Medium

## Description

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`

. 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 less digits. Also, a decimal point within a number never occurs without at least one digit occuring before it, so we never started with numbers like “.1”.

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

**Example 1:**

**Input:** “(123)”

**Output:** [“(1, 23)”, “(12, 3)”, “(1.2, 3)”, “(1, 2.3)”]

**Example 2:**

**Input:** “(00011)”

**Output:** [“(0.001, 1)”, “(0, 0.011)”]

**Explanation:**

0.0, 00, 0001 or 00.01 are not allowed.

**Example 3:**

**Input:** “(0123)”

**Output:** [“(0, 123)”, “(0, 12.3)”, “(0, 1.23)”, “(0.1, 23)”, “(0.1, 2.3)”, “(0.12, 3)”]

**Example 4:**

**Input:** “(100)”

**Output:** [(10, 0)]

**Explanation:**

1.0 is not allowed.

**Note:**

`4 <= S.length <= 12.`

`S[0]`

= “(“,`S[S.length - 1] = ")"`

, and the other elements in`S`

are digits.

## Solution

For the given string `S`

, consider all the digits and consider all the indices where a comma can be inserted. If there are `n`

digits in `S`

, then there are `n - 1`

insertion points in `S`

. Inserting a comma at an insertion point splits the digits into two parts. For each part, consider all possible numbers.

For the given part, the possible numbers are as follows.

- If the part has length 1, or the first (or leftmost/highest) digit is not zero, then the part can be an integer without adding a decimal point.
- If the part has length greater than 1 and all the digits are zeros, then no possible numbers can be obtained from the part.
- If the last (or rightmost/lowest) digit is zero, then no decimal point can be added, and the only possible number is the part without adding a decimal point.
- If the last digit is not zero, then check whether there are leading zeros. If there are no leading zeros, then a decimal point can be inserted between any two digits. If there are leading zeros (at least one leading zero), then a decimal point can only be inserted after the first digit.

For each insertion point where a comma is inserted such that the string is split into two parts, apply the rules for both parts to obtain all possible coordinates. Finally, return the list that stores all possible coordinates.

```
class Solution {
public List<String> ambiguousCoordinates(String S) {
List<String> coordinatesList = new ArrayList<String>();
int length = S.length();
int left = 1, right = length - 1;
for (int i = left + 1; i < right; i++) {
String xCoordinateStr = S.substring(left, i);
String yCoordinateStr = S.substring(i, right);
List<String> xCoordinatesList = getPossibleCoordinates(xCoordinateStr);
List<String> yCoordinatesList = getPossibleCoordinates(yCoordinateStr);
for (String xCoordinate : xCoordinatesList) {
for (String yCoordinate : yCoordinatesList) {
String coordinate = "(" + xCoordinate + ", " + yCoordinate + ")";
coordinatesList.add(coordinate);
}
}
}
return coordinatesList;
}
public List<String> getPossibleCoordinates(String coordinateStr) {
List<String> coordinatesList = new ArrayList<String>();
if (coordinateStr.length() == 1 || coordinateStr.charAt(0) != '0')
coordinatesList.add(coordinateStr);
int leadingZeros = 0;
int length = coordinateStr.length();
for (int i = 0; i < length; i++) {
if (coordinateStr.charAt(i) == '0')
leadingZeros++;
else
break;
}
if (leadingZeros < length && coordinateStr.charAt(length - 1) != '0') {
int maxInsertionIndex = leadingZeros > 0 ? 1 : length - 1;
for (int i = 1; i <= maxInsertionIndex; i++) {
StringBuffer sb = new StringBuffer(coordinateStr);
sb.insert(i, '.');
coordinatesList.add(sb.toString());
}
}
return coordinatesList;
}
}
```