|
| 1 | +/* |
| 2 | +
|
| 3 | +
|
| 4 | +-* 93. Restore IP Addresses *- |
| 5 | +
|
| 6 | +
|
| 7 | +A valid IP address consists of exactly four integers separated by single dots. Each integer is between 0 and 255 (inclusive) and cannot have leading zeros. |
| 8 | +
|
| 9 | +For example, "0.1.2.201" and "192.168.1.1" are valid IP addresses, but "0.011.255.245", "192.168.1.312" and "[email protected]" are invalid IP addresses. |
| 10 | +Given a string s containing only digits, return all possible valid IP addresses that can be formed by inserting dots into s. You are not allowed to reorder or remove any digits in s. You may return the valid IP addresses in any order. |
| 11 | +
|
| 12 | + |
| 13 | +
|
| 14 | +Example 1: |
| 15 | +
|
| 16 | +Input: s = "25525511135" |
| 17 | +Output: ["255.255.11.135","255.255.111.35"] |
| 18 | +Example 2: |
| 19 | +
|
| 20 | +Input: s = "0000" |
| 21 | +Output: ["0.0.0.0"] |
| 22 | +Example 3: |
| 23 | +
|
| 24 | +Input: s = "101023" |
| 25 | +Output: ["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"] |
| 26 | + |
| 27 | +
|
| 28 | +Constraints: |
| 29 | +
|
| 30 | +1 <= s.length <= 20 |
| 31 | +s consists of digits only. |
| 32 | +
|
| 33 | +
|
| 34 | +*/ |
| 35 | +class A { |
| 36 | + List<String> ans = []; |
| 37 | + String str = ""; |
| 38 | + List<String> restoreIpAddresses(String s) { |
| 39 | + str = s; |
| 40 | + magical("", 0, 0); |
| 41 | + return ans; |
| 42 | + } |
| 43 | + |
| 44 | + void magical(String path, int index, int dots) { |
| 45 | + if (dots > 4) return; |
| 46 | + if (dots == 4 && index >= str.length) { |
| 47 | + ans.add(path.substring(0, path.length - 1)); |
| 48 | + return; |
| 49 | + } |
| 50 | + for (int length = 1; |
| 51 | + length <= 3 && index + length <= str.length; |
| 52 | + length++) { |
| 53 | + String nums = str.substring(index, index + length); |
| 54 | + if (nums[0] == '0' && length != 1) |
| 55 | + break; |
| 56 | + else if (int.parse(nums) <= 255) { |
| 57 | + magical(path + str.substring(index, index + length) + ".", |
| 58 | + index + length, dots + 1); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +class B { |
| 65 | + List<String> restoreIpAddresses(String s) { |
| 66 | + List<String> ips = []; |
| 67 | + int n = s.length; |
| 68 | + // iterate `s` - place 3 dots to have 4 segments |
| 69 | + // [seg1].[seg2].[seg3].[seg4] |
| 70 | + // 1st dot - we just need to run it 3 times at most |
| 71 | + // e.g. for 255, we can place the first dot at `2.55`, `25.5` or `255.` |
| 72 | + for (int i = 1; i < 4 && i < n; i++) { |
| 73 | + // we place the 2nd dot in a similar way |
| 74 | + for (int j = i + 1; j < i + 4 && j < n; j++) { |
| 75 | + // we place the 3rd dot in a similar way |
| 76 | + for (int k = j + 1; k < j + 4 && k < n; k++) { |
| 77 | + // now we can separate into 4 segments |
| 78 | + String seg1 = s.substring(0, i), |
| 79 | + seg2 = s.substring(i, j), |
| 80 | + seg3 = s.substring(j, k), |
| 81 | + seg4 = s.substring(k); |
| 82 | + // for each segment, check if it is valid |
| 83 | + if (ok(seg1) && ok(seg2) && ok(seg3) && ok(seg4)) { |
| 84 | + // if so, we build the ip address and push to answer |
| 85 | + ips.add(seg1 + "." + seg2 + "." + seg3 + "." + seg4); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + return ips; |
| 91 | + } |
| 92 | + |
| 93 | + bool ok(String s) { |
| 94 | + // string length > 3 is not a valid IP address segment |
| 95 | + if (s.length > 3 || |
| 96 | + // empty segment is not valid |
| 97 | + s.length == 0 || |
| 98 | + // if the first character is 0, we cannot have something like 0x, 0xx |
| 99 | + (s[0] == '0' && s.length > 1) || |
| 100 | + // segment is out of range |
| 101 | + int.parse(s) > 255) { |
| 102 | + return false; |
| 103 | + } |
| 104 | + return true; |
| 105 | + } |
| 106 | +} |
0 commit comments