Skip to content

Commit dd7df2e

Browse files
committed
[Array] Add a solution to Validate IP Address
1 parent 0d7b4c4 commit dd7df2e

File tree

2 files changed

+96
-2
lines changed

2 files changed

+96
-2
lines changed

Array/ValidateIPAddress.swift

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/validate-ip-address/
3+
* Primary idea: Determine whether they are iPv4 or iPv6, then handle them separately
4+
*
5+
* Time Complexity: O(n), Space Complexity: O(1)
6+
*
7+
*/
8+
9+
class ValidateIPAddress {
10+
11+
let IPv4 = "IPv4"
12+
let IPv4Separator = Character(".")
13+
let IPv6 = "IPv6"
14+
let IPv6Separator = Character(":")
15+
let InvalidIP = "Neither"
16+
17+
func validIPAddress(_ IP: String) -> String {
18+
// detect IPv4 or IPv6
19+
guard let isIPv4 = isPotentialIPv4(IP) else {
20+
return InvalidIP
21+
}
22+
23+
return isIPv4 ? isValidIPv4(IP) : isValidIPv6(IP)
24+
}
25+
26+
private func isPotentialIPv4(_ IP: String) -> Bool? {
27+
let isIPv4 = IP.contains(IPv4Separator), isIPv6 = IP.contains(IPv6Separator)
28+
29+
if isIPv4 || isIPv6 {
30+
return isIPv4
31+
} else {
32+
return nil
33+
}
34+
}
35+
36+
private func isValidIPv4(_ IP: String) -> String {
37+
if IP.contains("\(IPv4Separator)\(IPv4Separator)") || IP.first == IPv4Separator || IP.last == IPv4Separator {
38+
return InvalidIP
39+
}
40+
41+
let numbers = IP.split(separator: IPv4Separator)
42+
43+
guard numbers.count == 4 else {
44+
return InvalidIP
45+
}
46+
47+
for number in numbers {
48+
guard let num = Int(number) else {
49+
return InvalidIP
50+
}
51+
52+
guard let first = number.first, first.isNumber else {
53+
return InvalidIP
54+
}
55+
56+
if first == "0", number.count > 1 {
57+
return InvalidIP
58+
}
59+
if num < 0 || num > 255 {
60+
return InvalidIP
61+
}
62+
}
63+
64+
return IPv4
65+
}
66+
67+
private func isValidIPv6(_ IP: String) -> String {
68+
if IP.contains("\(IPv6Separator)\(IPv6Separator)") || IP.first == IPv6Separator || IP.last == IPv6Separator {
69+
return InvalidIP
70+
}
71+
72+
let numbers = IP.split(separator: IPv6Separator)
73+
74+
guard numbers.count == 8 else {
75+
return InvalidIP
76+
}
77+
78+
for number in numbers {
79+
let number = number.lowercased()
80+
81+
if number.count > 4 {
82+
return InvalidIP
83+
}
84+
85+
for char in number {
86+
if !char.isHexDigit {
87+
return InvalidIP
88+
}
89+
}
90+
}
91+
92+
return IPv6
93+
}
94+
}

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
![Leetcode](./logo.png?style=centerme)
55

66
## Progress
7-
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 322 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
7+
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 323 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
88

99
## Contributors
1010

@@ -93,7 +93,7 @@
9393
[Gas Station](https://leetcode.com/problems/gas-station/)| [Swift](./Array/GasStation.swift)| Medium| O(n)| O(1)|
9494
[Game of Life](https://leetcode.com/problems/game-of-life/)| [Swift](./Array/GameLife.swift)| Medium| O(n)| O(1)|
9595
[Task Scheduler](https://leetcode.com/problems/task-scheduler/)| [Swift](./Array/TaskScheduler.swift)| Medium| O(nlogn)| O(n)|
96-
[]
96+
[Validate IP Address](https://leetcode.com/problems/validate-ip-address/)| [Swift](./Array/ValidateIPAddress.swift)| Medium| O(n)| O(1)|
9797
[Sliding Window Maximum ](https://leetcode.com/problems/sliding-window-maximum/)| [Swift](./Array/SlidingWindowMaximum.swift)| Hard| O(n)| O(n)|
9898
[Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/)| [Swift](./Array/LongestConsecutiveSequence.swift)| Hard| O(n)| O(n)|
9999
[Create Maximum Number](https://leetcode.com/problems/create-maximum-number/)| [Swift](./Array/CreateMaximumNumber.swift)| Hard| O(n^2)| O(n)|

0 commit comments

Comments
 (0)