-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestore_ip_addresses.rs
63 lines (56 loc) · 1.7 KB
/
restore_ip_addresses.rs
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
63
///
/// Problem: Restore IP Addresses
///
/// Given a string `s` containing **only digits**, return **all possible valid IP addresses** that can be formed by inserting dots (`.`).
///
/// A valid IP address consists of **four octets** (0-255), and no segment can have **leading zeros** unless it's "0".
///
/// **Example 1:**
/// ```plaintext
/// Input: s = "25525511135"
/// Output: ["255.255.11.135", "255.255.111.35"]
/// ```
///
/// **Example 2:**
/// ```plaintext
/// Input: s = "0000"
/// Output: ["0.0.0.0"]
/// ```
///
/// **Constraints:**
/// - `1 <= s.length <= 20`
/// - `s` consists of digits only.
///
/// # Solution:
/// - **Time Complexity:** `O(1)`
/// - **Space Complexity:** `O(1)`
impl Solution {
pub fn restore_ip_addresses(s: String) -> Vec<String> {
let mut result = Vec::new();
let mut segments = Vec::new();
Self::backtrack(&s, 0, &mut segments, &mut result);
result
}
fn backtrack(s: &str, start: usize, segments: &mut Vec<String>, result: &mut Vec<String>) {
if segments.len() == 4 && start == s.len() {
result.push(segments.join("."));
return;
}
if segments.len() >= 4 {
return;
}
for length in 1..=3 {
if start + length > s.len() {
break;
}
let segment = &s[start..start + length];
if (segment.starts_with('0') && segment.len() > 1) ||
segment.parse::<u8>().map_or(true, |num| num > 255) {
continue;
}
segments.push(segment.to_string());
Self::backtrack(s, start + length, segments, result);
segments.pop();
}
}
}