Thursday, May 13, 2021

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.  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: 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.

 

Note:

  • 4 <= s.length <= 12.
  • s[0] = "(", s[s.length - 1] = ")", and the other elements in s are digits.


Approach:

For this problem, we have two basic challenges. The first challenge is preventing invalid coordinates. For that, we can define a helper function (isValid) which will take a string (str) and a decimal location (dec). A value of 0 for dec will mean that there is no decimal.

The resulting number will be invalid if:

  • The string ends with a "0", unless there is no decimal.
  • The string starts with a "0", unless the entire string is just "0" or the decimal is right after the initial "0".

After defining our 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, 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.

Once we finish iterating through all comma positions, we can return ans.


C++ Code:

class Solution {
public:
    vector<string> ambiguousCoordinates(string S) {
        vector<string> ans;
        for (int i = 2; i < S.size() - 1; i++) {
            string xStr = S.substr(1,i-1), yStr = S.substr(i, S.size() - 1 - i);
            vector<string> xPoss;
            for (int j = 0; j < xStr.size(); j++)
                if (isValid(xStr, j))
                    xPoss.push_back(xStr.substr(0,j) + (j ? "." : "") + 
                                                        xStr.substr(j));
            for (int j = 0; j < yStr.size(); j++)
                if (isValid(yStr, j)) {
                    string y = yStr.substr(0,j) + (j ? "." : "") + 
                                                              yStr.substr(j);
                    for (auto x : xPoss)
                        ans.push_back("(" + x + ", " + y + ")");
                }
        }
        return ans;
    }
    
    bool isValid(string str, int dec) {
        if (dec && str[str.size()-1] == '0') return false;
        if (str.size() != 1 && dec != 1 && str[0] == '0') return false;
        return true;
    }
};



💻🐱‍💻If there are any suggestions / questions / mistakes in my post, please do comment below 👇



No comments:

Post a Comment

Please do not enter any spam link in the comment box