Skip to content

Commit a3553a9

Browse files
authored
Create 816. Ambiguous Coordinates
1 parent c138793 commit a3553a9

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

816. Ambiguous Coordinates

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Solution {
2+
List<String> result = new ArrayList<>();
3+
public List<String> ambiguousCoordinates(String s) {
4+
s=s.substring(1,s.length()-1);
5+
//Breaking String in x,y form (0123) -> (0,123)
6+
for(int i = 1; i < s.length(); i++) {
7+
helper(s.substring(0,i), s.substring(i));
8+
}
9+
return result;
10+
}
11+
12+
public void helper(String x, String y){
13+
List<String> dotx = putDot(x);
14+
List<String> doty = putDot(y);
15+
16+
for(String dx : dotx){
17+
if(isValid(dx)) {
18+
for(String dy : doty){
19+
if(isValid(dy)) {
20+
result.add("("+dx+", "+dy+")"); //(1, 23)
21+
}
22+
}
23+
}
24+
}
25+
}
26+
27+
public List<String> putDot(String s){
28+
List<String> res = new ArrayList<>();
29+
res.add(s);
30+
for(int i = 1; i < s.length(); i++) {
31+
res.add(s.substring(0,i)+"."+s.substring(i));
32+
}
33+
return res;
34+
}
35+
36+
public boolean isValid(String s) {
37+
if(s.contains(".")) {
38+
String[] part = s.split("\\.");
39+
if(!part[0].equals("0") && part[0].startsWith("0")) return false;
40+
else return !part[1].endsWith("0");
41+
}
42+
else {
43+
if(s.equals("0")) return true;
44+
else return !s.startsWith("0");
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)