| 8 | |
| 9 | |
| 10 | class Solution { |
| 11 | public: |
| 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 | |
| 44 | int main() { |
nothing calls this directly
no outgoing calls
no test coverage detected