Problem Description
Given a coordinate string s that has had commas, decimal points, and spaces removed (for example, "(1, 3)" becomes "(13)"), return a list of all possible original coordinates. The original coordinates did not have extra leading or trailing zeros and any decimal point always had at least one digit before it. The resulting coordinate strings should be formatted with exactly one space after the comma.
Key Insights
- Remove the outer parentheses to work with the pure digits.
- For each valid split of the remaining string into two parts, generate valid representations by inserting a decimal point in all possible positions, while enforcing no extraneous zeros.
- Validate integer-only representations (ensuring no invalid leading zeros) and those with decimals (ensuring no trailing zeros in the fractional part).
- Use nested loops: one for splitting the string into the two coordinate parts, and a helper function to determine all valid representations for each part.
- Combine each valid representation from the left part with each from the right part to form the final coordinate string.
Space and Time Complexity
Time Complexity: O(n^2) in the worst-case scenario, due to iterating through each possible split and potential decimal insertions. Space Complexity: O(n) for temporary storage and output list.
Solution
First, remove the outer parentheses from s. Then, for every possible split of the string into two parts, generate valid numeric representations for each side using a helper function. This helper function generates the integer version (if valid) and attempts to insert a decimal point at various positions where the resulting parts are valid (i.e., checking for invalid leading zeros on the integer part and invalid trailing zeros on the fractional part). Finally, combine valid pairs into formatted coordinate strings.