forked from AlgoStudyGroup/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidate-IP-Address.kt
76 lines (71 loc) · 2.07 KB
/
Validate-IP-Address.kt
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
class ValidateIPAddressKotlin468 {
fun validIPAddress(IP: String): String {
if (IP.isEmpty() || IP.length < 6) {
return "Neither"
}
val str5 = IP.substring(0, 6)
return when {
str5.contains(".") -> {
val strArray4 = IP.split(".")
if (strArray4.size == 4) {
strArray4.forEach {
if (!isIpV4(it)) {
return "Neither"
}
}
"IPv4"
} else {
"Neither"
}
}
str5.contains(":") -> {
val strArray8 = IP.split(":")
if (strArray8.size == 8) {
strArray8.forEach {
if (!isIpV6(it)) {
return "Neither"
}
}
"IPv6"
} else {
"Neither"
}
}
else -> "Neither"
}
}
private val validIpV4String = "0123456789"
private fun isIpV4(string: String): Boolean {
if (string.length in 1..3) {
string.forEach {
if (!validIpV4String.contains(it, ignoreCase = true)) {
return false
}
}
if (string.toInt() !in 0..255) {
return false
}
if (string[0] == '0' && string.length > 1) {
return false
}
return true
}
return false
}
private val validIpV6String = "0123456789abcdef"
private fun isIpV6(string: String): Boolean {
if (string.length in 1..4) {
string.forEach {
if (!validIpV6String.contains(it, ignoreCase = true)) {
return false
}
}
return true
}
return false
}
}
fun main() {
val str = "2001:0db8:85a3:0:0:8A2E:0370:7334:"
println(str.split(":").size)
}