1
class Solution {
2
public:
3
bool checkforIPv6(string IP) {
4
int n = IP.size();
5
vector<string> store;
6
string s = "";
7
for (int i = 0; i < n; i++) {
8
if (IP[i] == ':') {
9
store.push_back(s);
10
s = "";
11
} else {
12
s += IP[i];
13
}
14
}
15
store.push_back(s);
16
if (store.size() != 8) {
17
return false;
18
}
19
for (int i = 0; i < store.size(); i++) {
20
string s = store[i];
21
if (s.size() > 4 or s.size() == 0) {
22
return false;
23
}
24
for (int j = 0; j < s.size(); j++) {
25
if (s[j] >= 'a' and s[j] <= 'f') {
26
continue;
27
} else if (s[j] >= 'A' and s[j] <= 'F') {
28
continue;
29
} else if (s[j] >= '0' and s[j] <= '9') {
30
continue;
31
} else {
32
return false;
33
}
34
}
35
}
36
return true;
37
}
38

39
bool checkforIPv4(string IP) {
40
int n = IP.size();
41
vector<string> store;
42
string s = "";
43
for (int i = 0; i < n; i++) {
44
if (IP[i] == '.') {
45
store.push_back(s);
46
s = "";
47
} else {
48
s += IP[i];
49
}
50
}
51
store.push_back(s);
52
if (store.size() != 4) {
53
return false;
54
}
55
for (int i = 0; i < store.size(); i++) {
56
string s = store[i];
57
if (s.size() > 3 or s.size() == 0) {
58
return false;
59
}
60
int num = 0;
61
for (int j = 0; j < s.size(); j++) {
62
if (s.size() >= 2 and s[0] == '0' and s[1] == '0') {
63
return false;
64
}
65
if (s.size() >= 2 and s[0] == '0' and s[1] != '0') {
66
return false;
67
}
68
if (s[j] >= '0' and s[j] <= '9') {
69
// Do nothing.
70
} else {
71
return false;
72
}
73
num = num * 10 + (s[j] - '0');
74
}
75
if (num > 255 or num < 0) {
76
return false;
77
}
78
}
79
return true;
80
}
81
string validIPAddress(string queryIP) {
82
bool IPv6 = checkforIPv6(queryIP);
83
bool IPv4 = checkforIPv4(queryIP);
84
if (IPv6) {
85
return "IPv6";
86
}
87
if (IPv4) {
88
return "IPv4";
89
}
90
return "Neither";
91
}
92
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0