-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipAddressList.cpp
More file actions
110 lines (83 loc) · 2 KB
/
Copy pathipAddressList.cpp
File metadata and controls
110 lines (83 loc) · 2 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
bool isValidPart(string str){
//2 conditions
// integer value < 255
// there should be no leading 0
// "028" -> integer me convert -> 28 -> conver to string ->. "28"
int intPart = stoi(str);
if (intPart > 255)
return false;
string strPart = to_string(intPart);
if (strPart.length() == str.length())
return true;
return false;
}
/*
["192", "168", "255" ["20"]
192.168.255.20.
192.168.255.20
*/
string join(vector<string> str){
string s;
for(int i = 0 ; i < str.size() ; i++){
s += str[i];
if (i < str.size() - 1) {
s += ".";
}
}
return s;
}
//Time Complexity
// maximum 12 digits
// steps are fixed now
// O(1) ->
vector<string> getValidIpAddress(string str){
vector<string> ipAddressFound;
if (str.length() > 12 || str.length() < 4){
return ipAddressFound;
}
//result array
/*
192
min(3,4)
i 1 -> 4 not included
1921680
i = 1
substr(0, 1) = 1
1926
1
*/
for (int i = 1 ; i < min((int)str.length(), 4 ); i++){
vector<string> parts = { "", "", "", ""};
parts[0] = str.substr(0, i);
if(!isValidPart(parts[0])){
continue;
}
//now find the second part
for (int j = i + 1 ; j < (i + min((int)str.length() -i , 4)) ; j++){
parts[1] = str.substr(i, j-i);
if(!isValidPart(parts[1])){
continue;
}
//now find 3rd and 4th parts
for (int k = j + 1 ; k < (j + min((int)str.length() - j, 4)); k++){
parts[2] = str.substr(j, k-j);
parts[3] = str.substr(k);
if(isValidPart(parts[2]) && isValidPart(parts[3])){
ipAddressFound.push_back(join(parts));
}
}
}
}
return ipAddressFound;
}
int main() {
vector<string> result = getValidIpAddress("1921680");
for(int i = 0 ; i < result.size() ;i++){
cout<<result[i]<<endl;
}
}