Skip to content

Commit fd53b27

Browse files
committed
leetcode
1 parent 6cc6a2c commit fd53b27

File tree

4 files changed

+224
-0
lines changed

4 files changed

+224
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
190190
- [**918.** Maximum Sum Circular Sub-Array](MaximumSumCircularSubarray/maximum_sum_circular_subarray.dart)
191191
- [**974.** SubArray Sums Divisible by K](SubarraySumsDivisibleByK/subarray_sums_divisible_by_k.dart)
192192
- [**491.** Non-decreasing Subsequences](Non-DecreasingSubsequences/non_decreasing_subsequences.dart)
193+
- [**93.** Restore IP Addresses](RestoreIPAddresses/restore_ip_addresses.dart)
193194

194195
## Reach me via
195196

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
func restoreOctets(s string, octetsNumber int8) []string {
4+
if len(s) == 0 {
5+
return nil
6+
}
7+
if octetsNumber == 1 {
8+
if (s[0] == '0' && len(s) != 1) || len(s) > 3 || (len(s) == 3 && (s[0] > '2' || (s[0] == '2' && (s[1]-'0')*10+s[2]-'0' > 55))) {
9+
return nil
10+
}
11+
return []string{s}
12+
}
13+
var result []string
14+
if s[0] == '0' {
15+
for _, s2 := range restoreOctets(s[1:], octetsNumber-1) {
16+
result = append(result, "0."+s2)
17+
}
18+
} else {
19+
num := 0
20+
for i := 0; i < len(s); i++ {
21+
num = num*10 + int(s[i]-'0')
22+
if num > 255 {
23+
break
24+
}
25+
for _, s2 := range restoreOctets(s[i+1:], octetsNumber-1) {
26+
result = append(result, s[:i+1]+"."+s2)
27+
}
28+
}
29+
}
30+
return result
31+
}
32+
33+
func restoreIpAddresses(s string) []string {
34+
return restoreOctets(s, 4)
35+
}
+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# 🔥 2 Solutions 🔥 || Simple Fast and Easy || with Explanation
2+
3+
## Solution -1
4+
5+
```dart
6+
class Solution {
7+
List<String> ans = [];
8+
String str = "";
9+
List<String> restoreIpAddresses(String s) {
10+
str = s;
11+
soft("", 0, 0);
12+
return ans;
13+
}
14+
15+
void soft(String path, int index, int dots) {
16+
if (dots > 4) return;
17+
if (dots == 4 && index >= str.length) {
18+
ans.add(path.substring(0, path.length - 1));
19+
return;
20+
}
21+
for (int length = 1;
22+
length <= 3 && index + length <= str.length;
23+
length++) {
24+
String nums = str.substring(index, index + length);
25+
if (nums[0] == '0' && length != 1)
26+
break;
27+
else if (int.parse(nums) <= 255) {
28+
soft(path + str.substring(index, index + length) + ".",
29+
index + length, dots + 1);
30+
}
31+
}
32+
}
33+
}
34+
```
35+
36+
## Solution - 2
37+
38+
```dart
39+
class Solution {
40+
List<String> restoreIpAddresses(String s) {
41+
List<String> ips = [];
42+
int n = s.length;
43+
// iterate `s` - place 3 dots to have 4 segments
44+
// [seg1].[seg2].[seg3].[seg4]
45+
// 1st dot - we just need to run it 3 times at most
46+
// e.g. for 255, we can place the first dot at `2.55`, `25.5` or `255.`
47+
for (int i = 1; i < 4 && i < n; i++) {
48+
// we place the 2nd dot in a similar way
49+
for (int j = i + 1; j < i + 4 && j < n; j++) {
50+
// we place the 3rd dot in a similar way
51+
for (int k = j + 1; k < j + 4 && k < n; k++) {
52+
// now we can separate into 4 segments
53+
String seg1 = s.substring(0, i),
54+
seg2 = s.substring(i, j),
55+
seg3 = s.substring(j, k),
56+
seg4 = s.substring(k);
57+
// for each segment, check if it is valid
58+
if (ok(seg1) && ok(seg2) && ok(seg3) && ok(seg4)) {
59+
// if so, we build the ip address and push to answer
60+
ips.add(seg1 + "." + seg2 + "." + seg3 + "." + seg4);
61+
}
62+
}
63+
}
64+
}
65+
return ips;
66+
}
67+
68+
bool ok(String s) {
69+
// string length > 3 is not a valid IP address segment
70+
if (s.length > 3 ||
71+
// empty segment is not valid
72+
s.length == 0 ||
73+
// if the first character is 0, we cannot have something like 0x, 0xx
74+
(s[0] == '0' && s.length > 1) ||
75+
// segment is out of range
76+
int.parse(s) > 255) {
77+
return false;
78+
}
79+
return true;
80+
}
81+
}
82+
```

0 commit comments

Comments
 (0)