-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidIP.java
48 lines (40 loc) · 950 Bytes
/
validIP.java
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
48
package strings;
public class validIP {
public static void main(String[] args) {
// TODO Auto-generated method stub
// backtrack("25525511135", "", 0);
// backtrack("25505011535", "", 0);
backtrack("19002426", "", 0);
}
public static void backtrack(String s, String ans, int count) {
if (count == 4 && s.length() == 0) {
System.out.println(ans);
return;
}
if (count == 4) {
return;
}
for (int i = 2; i >= 0; i--) {
if (i < s.length()) {
String A = s.substring(0, i + 1);
int k = 0, len = 0;
while (k < A.length()) {
if (A.charAt(k) == '0') {
len++;
}
k++;
}
if ((len == A.length() || A.charAt(0) != '0') && Integer.parseInt(A) >= 0
&& Integer.parseInt(A) <= 255) {
String ros = s.substring(i + 1);
count++;
if (count == 4)
backtrack(ros, ans + A, count);
else
backtrack(ros, ans + A + ".", count);
count--;
}
}
}
}
}