-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0468.cpp
More file actions
49 lines (40 loc) · 1.25 KB
/
Copy path0468.cpp
File metadata and controls
49 lines (40 loc) · 1.25 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
class Solution {
static string valid4(const string &s) {
stringstream ss(s);
string crnt;
int cnt = 0;
while (getline(ss, crnt, '.')) {
if (size(crnt) == 0 || size(crnt) > 3) break;
if (any_of(begin(crnt), end(crnt), [](char c) { return isalpha(c); })) break;
if (size(crnt) > 1 && crnt[0] == '0') break;
if (stoi(crnt) >= 256) break;
cnt++;
}
return cnt == 4 ? "IPv4" : "Neither";
}
static string valid6(const string &s) {
stringstream ss(s);
string crnt;
int cnt = 0;
while (getline(ss, crnt, ':')) {
if (size(crnt) == 0 || size(crnt) > 4) break;
cnt++;
}
return cnt == 8 ? "IPv6" : "Neither";
}
public:
string validIPAddress(const string &queryIP) const {
int dot = 0, col = 0;
for (const char c : queryIP) {
if (c == '.')
dot++;
else if (c == ':')
col++;
else if (!isxdigit(c))
return "Neither";
}
if (dot && col) return "Neither";
if (dot ? dot != 3 : col != 7) return "Neither";
return dot ? valid4(queryIP) : valid6(queryIP);
}
};