-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRestoreIPAddresses.java
More file actions
62 lines (50 loc) · 2.06 KB
/
RestoreIPAddresses.java
File metadata and controls
62 lines (50 loc) · 2.06 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Given a string containing only digits, restore it by returning all possible valid IP address combinations.
// See: https://leetcode.com/problems/restore-ip-addresses/
// TODO: The current solution works but is relatively slow may be because a lot of
// string concatenation and substring actions.
// Try to find a faster alternative.
package leetcode.backtracking;
import java.util.LinkedList;
import java.util.List;
public class RestoreIPAddresses {
public List<String> restoreIpAddresses(String s) {
if (s.length() > 12) {
return new LinkedList<>();
}
List<String> res = new LinkedList<String>();
backtrack(s, "", res, 3);
return res;
}
private void backtrack(String s, String ip, List<String> res, int dot) {
if (isValid(s) && dot == 0) {
String currRes = ip + s;
res.add(currRes);
return;
}
if (s.length() >= 1 && isValid("" + s.charAt(0))) {
backtrack(s.substring(1), ip + s.charAt(0) + ".", res, dot - 1);
}
if (s.length() >= 2 && isValid("" + s.charAt(0) + s.charAt(1))) {
backtrack(s.substring(2), ip + s.charAt(0) + s.charAt(1) + ".", res, dot - 1);
}
if (s.length() >= 3 && isValid("" + s.charAt(0) + s.charAt(1) + s.charAt(2))) {
backtrack(s.substring(3), ip + s.charAt(0) + s.charAt(1) + s.charAt(2) + ".", res, dot - 1);
}
}
private boolean isValid(String ipPart) {
if (ipPart.equals("0")) {
return true;
}
if (ipPart.isEmpty() || ipPart.length() > 3 || ipPart.charAt(0) == '0' || Integer.parseInt(ipPart) > 255) {
return false;
}
return true;
}
public static void main(String[] args) {
RestoreIPAddresses sln = new RestoreIPAddresses();
System.out.println(sln.restoreIpAddresses("25525511135"));
System.out.println(sln.restoreIpAddresses("0000"));
System.out.println(sln.restoreIpAddresses("1721701"));
System.out.println(sln.restoreIpAddresses("1111"));
}
}