816. Ambiguous Coordinates LeetCode Solution
In this guide, you will get 816. Ambiguous Coordinates LeetCode Solution with the best time and space complexity. The solution to Ambiguous Coordinates problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Ambiguous Coordinates solution in C++
- Ambiguous Coordinates solution in Java
- Ambiguous Coordinates solution in Python
- Additional Resources

Problem Statement of Ambiguous Coordinates
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.)
Example 1:
Input: s = “(123)”
Output: [“(1, 2.3)”,”(1, 23)”,”(1.2, 3)”,”(12, 3)”]
Example 2:
Input: s = “(0123)”
Output: [“(0, 1.23)”,”(0, 12.3)”,”(0, 123)”,”(0.1, 2.3)”,”(0.1, 23)”,”(0.12, 3)”]
Explanation: 0.0, 00, 0001 or 00.01 are not allowed.
Example 3:
Input: s = “(00011)”
Output: [“(0, 0.011)”,”(0.001, 1)”]
Constraints:
4 <= s.length <= 12
s[0] == '(' and s[s.length – 1] == ')'.
The rest of s are digits.
Complexity Analysis
- Time Complexity: O(n^3)
- Space Complexity: O(n^3)
816. Ambiguous Coordinates LeetCode Solution in C++
class Solution {
public:
vector<string> ambiguousCoordinates(string s) {
vector<string> ans;
s = s.substr(1, s.length() - 2);
for (int i = 1; i < s.length(); ++i)
for (const string& x : splits(s.substr(0, i)))
for (const string& y : splits(s.substr(i)))
ans.push_back('(' + x + ", " + y + ')');
return ans;
}
private:
vector<string> splits(const string& s) {
if (s.empty() || s.length() > 1 && s.front() == '0' && s.back() == '0')
return {};
if (s.back() == '0')
return {s};
if (s.front() == '0')
return {"0." + s.substr(1)};
vector<string> candidates{s};
for (int i = 1; i < s.length(); ++i)
candidates.push_back(s.substr(0, i) + '.' + s.substr(i));
return candidates;
}
};
/* code provided by PROGIEZ */
816. Ambiguous Coordinates LeetCode Solution in Java
class Solution {
public List<String> ambiguousCoordinates(String s) {
List<String> ans = new ArrayList<>();
s = s.substring(1, s.length() - 1);
for (int i = 1; i < s.length(); ++i)
for (final String x : splits(s.substring(0, i)))
for (final String y : splits(s.substring(i)))
ans.add("(" + x + ", " + y + ")");
return ans;
}
private List<String> splits(final String s) {
if (s.isEmpty() || s.length() > 1 && s.charAt(0) == '0' && s.charAt(s.length() - 1) == '0')
return new ArrayList<>();
if (s.charAt(s.length() - 1) == '0')
return List.of(s);
if (s.charAt(0) == '0')
return List.of("0." + s.substring(1));
List<String> res = new ArrayList<>(List.of(s));
for (int i = 1; i < s.length(); ++i)
res.add(s.substring(0, i) + "." + s.substring(i));
return res;
}
}
// code provided by PROGIEZ
816. Ambiguous Coordinates LeetCode Solution in Python
class Solution:
def ambiguousCoordinates(self, s: str) -> list[str]:
def splits(s: str) -> list[str]:
if not s or len(s) > 1 and s[0] == s[-1] == '0':
return []
if s[-1] == '0':
return [s]
if s[0] == '0':
return [s[0] + '.' + s[1:]]
return [s] + [s[:i] + '.' + s[i:] for i in range(1, len(s))]
ans = []
s = s[1:-1]
for i in range(1, len(s)):
for x in splits(s[:i]):
for y in splits(s[i:]):
ans.append('(%s, %s)' % (x, y))
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.