93. Restore IP Addresses

93. Restore IP Addresses

Description

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.

  • 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 "192.168@1.1" are invalid IP addresses.

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.

Example 1:

1
2
Input: s = "25525511135"
Output: ["255.255.11.135","255.255.111.35"]

Example 2:

1
2
Input: s = "0000"
Output: ["0.0.0.0"]

Example 3:

1
2
Input: s = "101023"
Output: ["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"]

Constraints:

  • 1 <= s.length <= 20
  • s consists of digits only.

Hints/Notes

Solution

Language: C++

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
class Solution {
public:
vector<string> res;

bool valid(const string& s, int start, int length) {
return length == 1 || (s[start] != '0' && (length < 3 || s.substr(start, length) <= "255"));
}

void dfs(const string& s, int idx, vector<int>& dots) {
const int remainingLength = s.size() - idx;
const int remainingNum = 4 - dots.size();

if (remainingLength > remainingNum * 3 || remainingLength < remainingNum) {
return;
}
if (dots.size() == 3) {
if (valid(s, idx, remainingLength)) {
string cur;
int start = 0;
for (int dot : dots) {
cur.append(s.substr(start, dot));
cur.push_back('.');
start += dot;
}
cur.append(s.substr(start));
res.push_back(cur);
}
return;
}
for (int len = 1; len <= min(3, remainingLength); len++) {
dots.push_back(len);
if (valid(s, idx, len)) {
dfs(s, idx + len, dots);
}
dots.pop_back();
}
}

vector<string> restoreIpAddresses(string s) {
vector<int> dots;
dfs(s, 0, dots);
return res;
}
};