https://leetcode.com/articles/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: "(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.
- Time Complexity: , where is the length
S
. We evaluate the sum . - Space Complexity: , to store the answer.
public List<String> ambiguousCoordinates(String S) {
List<String> ans = new ArrayList();
for (int i = 2; i < S.length()-1; ++i)
for (String left: make(S, 1, i))
for (String right: make(S, i, S.length()-1))
ans.add("(" + left + ", " + right + ")");
return ans;
}
public List<String> make(String S, int i, int j) {
// Make on S.substring(i, j)
List<String> ans = new ArrayList();
for (int d = 1; d <= j-i; ++d) {
String left = S.substring(i, i+d);
String right = S.substring(i+d, j);
if ((!left.startsWith("0") || left.equals("0"))
&& !right.endsWith("0")) //
ans.add(left + (d < j-i ? "." : "") + right);
}
return ans;
}
// the way isValid is too complex........
public List<String> ambiguousCoordinates(String S) {
List<String> result = new ArrayList<>();
if (S.length() < 4) {
return result;
}
S = S.substring(1, S.length() - 1);
// split two parts
// get possible coordinates for each part
for (int i = 1; i < S.length(); i++) { // not S.length()-1
List<String> lefts = getCoordinates(S.substring(0, i));
if (lefts.isEmpty())
continue;
List<String> rights = getCoordinates(S.substring(i));
if (rights.isEmpty())
continue;
for (String left : lefts) {
StringBuilder sb = new StringBuilder("(").append(left).append(", ");
int keepLen = sb.length();
for (String right : rights) {
sb.append(right).append(")");
result.add(sb.toString());
sb.setLength(keepLen);
}
}
}
return result;
}
public List<String> getCoordinates(String word) {
if (cache.containsKey(word)) {
return cache.get(word);
}
// no duplicate
List<String> result = new ArrayList<>();
// 12 => 1.2, 12
// put . at pos i in word
for (int i = 1; i <= word.length(); i++) {
if (isValid(word, i)) {
// put example in the code to help write and check the code
if (i == word.length()) {
result.add(word);
} else {
result.add(word.substring(0, i) + "." + word.substring(i));
}
}
}
cache.put(word, result);
return result;
}
private boolean isValid(String word, int docPos) {
// precondition: docPost < word.length
boolean startsWith0 = word.charAt(0) == '0';
if (startsWith0) {
if (docPos != 1) {
return false;
}
}
// skip integral part
// fraction part
boolean hasInvalidEnding0 = false;
int i = docPos; // not docPos-1
while (i < word.length()) {
hasInvalidEnding0 = word.charAt(i) == '0';
i++;
}
return !hasInvalidEnding0; // !hasInvalidEnding0
}