Skip to content

Commit 451b2e6

Browse files
authoredJun 6, 2024
feat: add solutions to lc problem: No.468 (#3058)
No.0468.Validate IP Address
1 parent b9a754c commit 451b2e6

File tree

8 files changed

+1077
-228
lines changed

8 files changed

+1077
-228
lines changed
 

‎solution/0400-0499/0468.Validate IP Address/README.md

+370-74
Large diffs are not rendered by default.

‎solution/0400-0499/0468.Validate IP Address/README_EN.md

+370-74
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
class Solution {
2+
public:
3+
string validIPAddress(string queryIP) {
4+
if (isIPv4(queryIP)) {
5+
return "IPv4";
6+
}
7+
if (isIPv6(queryIP)) {
8+
return "IPv6";
9+
}
10+
return "Neither";
11+
}
12+
13+
private:
14+
bool isIPv4(const string& s) {
15+
if (s.empty() || s.back() == '.') {
16+
return false;
17+
}
18+
vector<string> ss = split(s, '.');
19+
if (ss.size() != 4) {
20+
return false;
21+
}
22+
for (const string& t : ss) {
23+
if (t.empty() || (t.size() > 1 && t[0] == '0')) {
24+
return false;
25+
}
26+
int x = convert(t);
27+
if (x < 0 || x > 255) {
28+
return false;
29+
}
30+
}
31+
return true;
32+
}
33+
34+
bool isIPv6(const string& s) {
35+
if (s.empty() || s.back() == ':') {
36+
return false;
37+
}
38+
vector<string> ss = split(s, ':');
39+
if (ss.size() != 8) {
40+
return false;
41+
}
42+
for (const string& t : ss) {
43+
if (t.size() < 1 || t.size() > 4) {
44+
return false;
45+
}
46+
for (char c : t) {
47+
if (!isxdigit(c)) {
48+
return false;
49+
}
50+
}
51+
}
52+
return true;
53+
}
54+
55+
int convert(const string& s) {
56+
int x = 0;
57+
for (char c : s) {
58+
if (!isdigit(c)) {
59+
return -1;
60+
}
61+
x = x * 10 + (c - '0');
62+
if (x > 255) {
63+
return x;
64+
}
65+
}
66+
return x;
67+
}
68+
69+
vector<string> split(const string& s, char delimiter) {
70+
vector<string> tokens;
71+
string token;
72+
istringstream iss(s);
73+
while (getline(iss, token, delimiter)) {
74+
tokens.push_back(token);
75+
}
76+
return tokens;
77+
}
78+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
func validIPAddress(queryIP string) string {
2+
if isIPv4(queryIP) {
3+
return "IPv4"
4+
}
5+
if isIPv6(queryIP) {
6+
return "IPv6"
7+
}
8+
return "Neither"
9+
}
10+
11+
func isIPv4(s string) bool {
12+
if strings.HasSuffix(s, ".") {
13+
return false
14+
}
15+
ss := strings.Split(s, ".")
16+
if len(ss) != 4 {
17+
return false
18+
}
19+
for _, t := range ss {
20+
if len(t) == 0 || (len(t) > 1 && t[0] == '0') {
21+
return false
22+
}
23+
x := convert(t)
24+
if x < 0 || x > 255 {
25+
return false
26+
}
27+
}
28+
return true
29+
}
30+
31+
func isIPv6(s string) bool {
32+
if strings.HasSuffix(s, ":") {
33+
return false
34+
}
35+
ss := strings.Split(s, ":")
36+
if len(ss) != 8 {
37+
return false
38+
}
39+
for _, t := range ss {
40+
if len(t) < 1 || len(t) > 4 {
41+
return false
42+
}
43+
for _, c := range t {
44+
if !unicode.IsDigit(c) && !strings.ContainsRune("0123456789abcdefABCDEF", c) {
45+
return false
46+
}
47+
}
48+
}
49+
return true
50+
}
51+
52+
func convert(s string) int {
53+
x := 0
54+
for _, c := range s {
55+
if !unicode.IsDigit(c) {
56+
return -1
57+
}
58+
x = x*10 + int(c-'0')
59+
if x > 255 {
60+
return x
61+
}
62+
}
63+
return x
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
class Solution {
2+
public String validIPAddress(String queryIP) {
3+
if (isIPv4(queryIP)) {
4+
return "IPv4";
5+
}
6+
if (isIPv6(queryIP)) {
7+
return "IPv6";
8+
}
9+
return "Neither";
10+
}
11+
12+
private boolean isIPv4(String s) {
13+
if (s.endsWith(".")) {
14+
return false;
15+
}
16+
String[] ss = s.split("\\.");
17+
if (ss.length != 4) {
18+
return false;
19+
}
20+
for (String t : ss) {
21+
if (t.length() == 0 || t.length() > 1 && t.charAt(0) == '0') {
22+
return false;
23+
}
24+
int x = convert(t);
25+
if (x < 0 || x > 255) {
26+
return false;
27+
}
28+
}
29+
return true;
30+
}
31+
32+
private boolean isIPv6(String s) {
33+
if (s.endsWith(":")) {
34+
return false;
35+
}
36+
String[] ss = s.split(":");
37+
if (ss.length != 8) {
38+
return false;
39+
}
40+
for (String t : ss) {
41+
if (t.length() < 1 || t.length() > 4) {
42+
return false;
43+
}
44+
for (char c : t.toCharArray()) {
45+
if (!Character.isDigit(c)
46+
&& !"0123456789abcdefABCDEF".contains(String.valueOf(c))) {
47+
return false;
48+
}
49+
}
50+
}
51+
return true;
52+
}
53+
54+
private int convert(String s) {
55+
int x = 0;
56+
for (char c : s.toCharArray()) {
57+
if (!Character.isDigit(c)) {
58+
return -1;
59+
}
60+
x = x * 10 + (c - '0');
61+
if (x > 255) {
62+
return x;
63+
}
64+
}
65+
return x;
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
11
class Solution:
2-
def validIPAddress(self, IP: str) -> str:
3-
if "." in IP:
4-
segments = IP.split(".")
5-
if len(segments) != 4:
6-
return "Neither"
7-
for segment in segments:
8-
if (
9-
not segment.isdigit()
10-
or not 0 <= int(segment) <= 255
11-
or (segment[0] == "0" and len(segment) > 1)
12-
):
13-
return "Neither"
2+
def validIPAddress(self, queryIP: str) -> str:
3+
def is_ipv4(s: str) -> bool:
4+
ss = s.split(".")
5+
if len(ss) != 4:
6+
return False
7+
for t in ss:
8+
if len(t) > 1 and t[0] == "0":
9+
return False
10+
if not t.isdigit() or not 0 <= int(t) <= 255:
11+
return False
12+
return True
13+
14+
def is_ipv6(s: str) -> bool:
15+
ss = s.split(":")
16+
if len(ss) != 8:
17+
return False
18+
for t in ss:
19+
if not 1 <= len(t) <= 4:
20+
return False
21+
if not all(c in "0123456789abcdefABCDEF" for c in t):
22+
return False
23+
return True
24+
25+
if is_ipv4(queryIP):
1426
return "IPv4"
15-
elif ":" in IP:
16-
segments = IP.split(":")
17-
if len(segments) != 8:
18-
return "Neither"
19-
for segment in segments:
20-
if (
21-
not segment
22-
or len(segment) > 4
23-
or not all(c in string.hexdigits for c in segment)
24-
):
25-
return "Neither"
27+
if is_ipv6(queryIP):
2628
return "IPv6"
2729
return "Neither"
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,68 @@
11
impl Solution {
2-
fn is_IPv4(s: &String) -> bool {
3-
let ss = s.split('.').collect::<Vec<&str>>();
2+
pub fn valid_ip_address(query_ip: String) -> String {
3+
if Self::is_ipv4(&query_ip) {
4+
return "IPv4".to_string();
5+
}
6+
if Self::is_ipv6(&query_ip) {
7+
return "IPv6".to_string();
8+
}
9+
"Neither".to_string()
10+
}
11+
12+
fn is_ipv4(s: &str) -> bool {
13+
if s.ends_with('.') {
14+
return false;
15+
}
16+
let ss: Vec<&str> = s.split('.').collect();
417
if ss.len() != 4 {
518
return false;
619
}
7-
for s in ss {
8-
match s.parse::<i32>() {
9-
Err(_) => {
10-
return false;
20+
for t in ss {
21+
if t.is_empty() || (t.len() > 1 && t.starts_with('0')) {
22+
return false;
23+
}
24+
match Self::convert(t) {
25+
Some(x) if x <= 255 => {
26+
continue;
1127
}
12-
Ok(num) => {
13-
if num < 0 || num > 255 || num.to_string() != s.to_string() {
14-
return false;
15-
}
28+
_ => {
29+
return false;
1630
}
1731
}
1832
}
1933
true
2034
}
2135

22-
fn is_IPv6(s: &String) -> bool {
23-
let ss = s.split(':').collect::<Vec<&str>>();
36+
fn is_ipv6(s: &str) -> bool {
37+
if s.ends_with(':') {
38+
return false;
39+
}
40+
let ss: Vec<&str> = s.split(':').collect();
2441
if ss.len() != 8 {
2542
return false;
2643
}
27-
for s in ss {
28-
if s.len() == 0 || s.len() > 4 {
44+
for t in ss {
45+
if t.len() < 1 || t.len() > 4 {
2946
return false;
3047
}
31-
for &c in s.as_bytes() {
32-
if
33-
(c >= b'0' && c <= b'9') ||
34-
(c >= b'a' && c <= b'f') ||
35-
(c >= b'A' && c <= b'F')
36-
{
37-
continue;
38-
}
48+
if !t.chars().all(|c| c.is_digit(16)) {
3949
return false;
4050
}
4151
}
4252
true
4353
}
4454

45-
pub fn valid_ip_address(query_ip: String) -> String {
46-
if Self::is_IPv4(&query_ip) {
47-
return String::from("IPv4");
48-
}
49-
if Self::is_IPv6(&query_ip) {
50-
return String::from("IPv6");
55+
fn convert(s: &str) -> Option<i32> {
56+
let mut x = 0;
57+
for c in s.chars() {
58+
if !c.is_digit(10) {
59+
return None;
60+
}
61+
x = x * 10 + (c.to_digit(10).unwrap() as i32);
62+
if x > 255 {
63+
return Some(x);
64+
}
5165
}
52-
String::from("Neither")
66+
Some(x)
5367
}
5468
}

0 commit comments

Comments
 (0)