This is part of a series of Leetcode solution explanations (index). If you liked this solution or found it useful, please like this post and/or upvote my solution post on Leetcode's forums.
Leetcode Problem #816 (Medium): Ambiguous Coordinates
Description:
(Jump to: Solution Idea || Code: JavaScript | Python | Java | C++)
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 strings
. 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.)
Examples:
Example 1: Input: s = "(123)" Output: ["(1, 23)", "(12, 3)", "(1.2, 3)", "(1, 2.3)"]
Example 2: Input: s = "(00011)" Output: ["(0.001, 1)", "(0, 0.011)"] Explanation: 0.0, 00, 0001 or 00.01 are not allowed.
Example 3: Input: s = "(0123)" Output: ["(0, 123)", "(0, 12.3)", "(0, 1.23)", "(0.1, 23)", "(0.1, 2.3)", "(0.12, 3)"]
Example 4: Input: s = "(100)" Output: [(10, 0)] Explanation: 1.0 is not allowed.
Constraints:
4 <= s.length <= 12
s[0] = "("
,s[s.length - 1] = ")"
, and the other elements ins
are digits.
Idea:
(Jump to: Problem Description || Code: JavaScript | Python | Java | C++)
For this problem, we have two basic challenges. The first challenge is preventing invalid coordinates. For that, we can define a helper function (parse) which will take a string (str) and only pass on valid options for another helper (process) to handle.
We can break down the options into three categories:
- No decimal: Any option except one with more than 1 digit and a leading "0".
- Decimal after first digit: Any option with more than 1 digit and no trailing "0".
- Decimals throughout: Any option that doesn't start and end with a "0"
After defining our first helper function, the next thing we should do is iterate through possible comma locations in our input string (S) and separate the coordinate pair strings (xStr, yStr).
Then we'll run into the second challenge, which is to avoid repeating the same processing. If we were to employ a simple nested loop or recursive function to solve this problem, it would end up redoing the same processes many times, since both coordinates can have a decimal.
What we actually want is the product of two loops. The basic solution would be to create two arrays and iterate through their combinations, but there's really no need to actually build the second array, since we can just as easily process the combinations while we iterate through the second coordinate naturally.
So we should first build and validate all decimal options for the xStr of a given comma position and store the valid possibilities in an array (xPoss). Once this is complete we should find each valid decimal option for yStr, combine it with each value in xPoss, and add the results to our answer array (ans) before moving onto the next comma position.
To aid in this, we can define process, which will either store the valid decimal options from xStr into xPoss or combine valid decimal options from yStr with the contents of xPoss and store the results in ans, depending on which coordinate string we're currently on (xy).
Once we finish iterating through all comma positions, we can return ans.
Javascript Code:
(Jump to: Problem Description || Solution Idea)
var ambiguousCoordinates = function(S) {
let ans = [], xPoss
const process = (str, xy) => {
if (xy)
for (let x of xPoss)
ans.push(`(${x}, ${str})`)
else xPoss.push(str)
}
const parse = (str, xy) => {
if (str.length === 1 || str[0] !== "0")
process(str, xy)
if (str.length > 1 && str[str.length-1] !== "0")
process(str.slice(0,1) + "." + str.slice(1), xy)
if (str.length > 2 && str[0] !== "0" && str[str.length-1] !== "0")
for (let i = 2; i < str.length; i++)
process(str.slice(0,i) + "." + str.slice(i), xy)
}
for (let i = 2; i < S.length - 1; i++) {
let strs = [S.slice(1,i), S.slice(i, S.length - 1)]
xPoss = []
for (let j = 0; j < 2; j++)
if (xPoss.length || !j) parse(strs[j], j)
}
return ans
};
Python Code:
(Jump to: Problem Description || Solution Idea)
class Solution:
def ambiguousCoordinates(self, S: str) -> List[str]:
ans, xPoss = [], []
def process(st: str, xy: int):
if xy:
for x in xPoss:
ans.append("(" + x + ", " + st + ")")
else: xPoss.append(st)
def parse(st: str, xy: int):
if len(st) == 1 or st[0] != "0":
process(st, xy)
if len(st) > 1 and st[-1] != "0":
process(st[:1] + "." + st[1:], xy)
if len(st) > 2 and st[0] != "0" and st[-1] != "0":
for i in range(2, len(st)):
process(st[:i] + "." + st[i:], xy)
for i in range(2, len(S)-1):
strs, xPoss = [S[1:i], S[i:-1]], []
for j in range(2):
if xPoss or not j: parse(strs[j], j)
return ans
Java Code:
(Jump to: Problem Description || Solution Idea)
class Solution {
private List<String> xPoss, ans;
public List<String> ambiguousCoordinates(String S) {
ans = new ArrayList<>();
for (int i = 2; i < S.length() - 1; i++) {
String[] strs = {S.substring(1,i), S.substring(i, S.length() - 1)};
xPoss = new ArrayList<>();
for (int j = 0; j < 2; j++)
if (xPoss.size() > 0 || j == 0) parse(strs[j], j);
}
return ans;
}
private void parse(String str, int xy) {
if (str.length() == 1 || str.charAt(0) != '0')
process(str, xy);
if (str.length() > 1 && str.charAt(str.length()-1) != '0')
process(str.substring(0,1) + "." + str.substring(1), xy);
if (str.length() > 2 && str.charAt(0) != '0' && str.charAt(str.length()-1) != '0')
for (int i = 2; i < str.length(); i++)
process(str.substring(0,i) + "." + str.substring(i), xy);
}
private void process(String str, int xy) {
if (xy > 0)
for (String x : xPoss)
ans.add("(" + x + ", " + str + ")");
else xPoss.add(str);
}
}
C++ Code:
(Jump to: Problem Description || Solution Idea)
class Solution {
public:
vector<string> ambiguousCoordinates(string S) {
for (int i = 2; i < S.size() - 1; i++) {
string strs[2] = {S.substr(1,i-1), S.substr(i,S.size()-i-1)};
xPoss.clear();
for (int j = 0; j < 2; j++)
if (xPoss.size() > 0 || j == 0) parse(strs[j], j);
}
return ans;
}
private:
vector<string> ans, xPoss;
void parse(string str, int xy) {
if (str.size() == 1 || str.front() != '0')
process(str, xy);
if (str.size() > 1 && str.back() != '0')
process(str.substr(0,1) + "." + str.substr(1), xy);
if (str.size() > 2 && str.front() != '0' && str.back() != '0')
for (int i = 2; i < str.size(); i++)
process(str.substr(0,i) + "." + str.substr(i), xy);
}
void process(string str, int xy) {
if (xy)
for (auto x : xPoss)
ans.push_back("(" + x + ", " + str + ")");
else xPoss.push_back(str);
}
};
Discussion (0)