Created
May 14, 2021 06:04
-
-
Save vrat28/bda23e3f7bb39911eb192075ff2b93c1 to your computer and use it in GitHub Desktop.
Ambiguous Coordinates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { //aw | |
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment