-
Notifications
You must be signed in to change notification settings - Fork 115
/
0468-ValidateIPAddress.cs
51 lines (46 loc) · 1.62 KB
/
0468-ValidateIPAddress.cs
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
//-----------------------------------------------------------------------------
// Runtime: 84ms
// Memory Usage: 22.6 MB
// Link: https://leetcode.com/submissions/detail/354663426/
//-----------------------------------------------------------------------------
using System;
using System.Linq;
namespace LeetCode
{
public class _0468_ValidateIPAddress
{
public string ValidIPAddress(string IP)
{
if (IP.Count(ch => ch == '.') == 3)
return ValidateIPv4(IP) ? "IPv4" : "Neither";
if (IP.Count(ch => ch == ':') == 7)
return ValidateIPv6(IP) ? "IPv6" : "Neither";
return "Neither";
}
private bool ValidateIPv4(string IP)
{
var nums = IP.Split(new char[] { '.' }, StringSplitOptions.None);
foreach (var x in nums)
{
if (x.Length == 0 || x.Length > 3) return false;
if (x[0] == '0' && x.Length != 1) return false;
foreach (var ch in x)
if (!char.IsDigit(ch)) return false;
if (int.Parse(x) > 255) return false;
}
return true;
}
private bool ValidateIPv6(string IP)
{
string hexdigits = "0123456789abcdefABCDEF";
var nums = IP.Split(new char[] { ':' }, StringSplitOptions.None);
foreach (var x in nums)
{
if (x.Length == 0 || x.Length > 4) return false;
foreach (var ch in x)
if (hexdigits.IndexOf(ch) == -1) return false;
}
return true;
}
}
}