MCPcopy Create free account
hub / github.com/Ainevsia/Leetcode-Rust / Solution

Class Solution

93. Restore IP Addresses/Solution.cpp:10–41  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8
9
10class Solution {
11public:
12 bool isPart(string str) {
13 if (str.size() > 3 or str.size() == 0) return false;
14 int i = stoi(str);
15 return i <= 255 and 0 <= i and (i == 0 ? str.size() == 1 : str[0] != '0');
16 }
17
18 vector<string> ipFromNPart(int n, string str) {
19 vector<string> res ;
20 if (n == 1) {
21 if (this->isPart(str)) res.push_back(str);
22 return res;
23 }
24 for (int i=1; i<=3 and i < str.size() ; i++) {
25 if (this->isPart(str.substr(0,i))) {
26 auto possible_postfix = this->ipFromNPart(n-1,str.substr(i));
27 for (auto word: possible_postfix) {
28 auto append = str.substr(0,i) + "." + word;
29 res.push_back(append);
30 }
31 }
32 }
33 return res;
34 }
35
36 // Input: "25525511135"
37 // Output: ["255.255.11.135", "255.255.111.35"]
38 vector<string> restoreIpAddresses(string s) {
39 return this->ipFromNPart(4, s);
40 }
41};
42
43
44int main() {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected