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