Skip to content

Latest commit

 

History

History

816

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

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.

 

Related Topics:
String

Solution 1.

Complexity Analysis

We need to loop O(N) time to find the split points.

For each split point, we call getNumbers to generate a list of possible numbers for the first and second parts.

Each getNumbers takes O(N) time and O(N) space, and could generate a list of O(N) elements.

Lastly, we concatenate the numbers which will take O(N^2) time.

So, overall this solution takes O(N^3) time and O(N) space.

// OJ: https://leetcode.com/problems/ambiguous-coordinates/
// Author: github.com/lzl124631x
// Time: O(N^3)
// Space: O(N)
class Solution {
    vector<string> getNumbers(string s) {
        if (s[0] == '0') {
            if (s.size() == 1) return { s };
            if (s.back() == '0') return {};
            s.insert(begin(s) + 1, '.');
            return { s };
        }
        if (s.back() == '0') return { s };
        vector<string> ans;
        ans.push_back(s);
        for (int i = 1; i <= s.size() - 1; ++i) {
            auto tmp = s;
            tmp.insert(begin(tmp) + i, '.');
            ans.push_back(tmp);
        }
        return ans;
    }
public:
    vector<string> ambiguousCoordinates(string s) {
        vector<string> ans;
        for (int i = 2, N = s.size(); i < N - 1; ++i) {
            auto first = getNumbers(s.substr(1, i - 1));
            if (first.empty()) continue;
            auto second = getNumbers(s.substr(i, N - 1 - i));
            if (second.empty()) continue;
            for (auto &a : first) {
                for (auto &b : second) {
                    ans.push_back("(" + a + ", " + b + ")");
                }
            }
        }
        return ans;
    }
};