Skip to content

Commit

Permalink
[Array] Add a solution to Validate IP Address
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed Mar 1, 2020
1 parent 0d7b4c4 commit dd7df2e
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 2 deletions.
94 changes: 94 additions & 0 deletions Array/ValidateIPAddress.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* Question Link: https://leetcode.com/problems/validate-ip-address/
* Primary idea: Determine whether they are iPv4 or iPv6, then handle them separately
*
* Time Complexity: O(n), Space Complexity: O(1)
*
*/

class ValidateIPAddress {

let IPv4 = "IPv4"
let IPv4Separator = Character(".")
let IPv6 = "IPv6"
let IPv6Separator = Character(":")
let InvalidIP = "Neither"

func validIPAddress(_ IP: String) -> String {
// detect IPv4 or IPv6
guard let isIPv4 = isPotentialIPv4(IP) else {
return InvalidIP
}

return isIPv4 ? isValidIPv4(IP) : isValidIPv6(IP)
}

private func isPotentialIPv4(_ IP: String) -> Bool? {
let isIPv4 = IP.contains(IPv4Separator), isIPv6 = IP.contains(IPv6Separator)

if isIPv4 || isIPv6 {
return isIPv4
} else {
return nil
}
}

private func isValidIPv4(_ IP: String) -> String {
if IP.contains("\(IPv4Separator)\(IPv4Separator)") || IP.first == IPv4Separator || IP.last == IPv4Separator {
return InvalidIP
}

let numbers = IP.split(separator: IPv4Separator)

guard numbers.count == 4 else {
return InvalidIP
}

for number in numbers {
guard let num = Int(number) else {
return InvalidIP
}

guard let first = number.first, first.isNumber else {
return InvalidIP
}

if first == "0", number.count > 1 {
return InvalidIP
}
if num < 0 || num > 255 {
return InvalidIP
}
}

return IPv4
}

private func isValidIPv6(_ IP: String) -> String {
if IP.contains("\(IPv6Separator)\(IPv6Separator)") || IP.first == IPv6Separator || IP.last == IPv6Separator {
return InvalidIP
}

let numbers = IP.split(separator: IPv6Separator)

guard numbers.count == 8 else {
return InvalidIP
}

for number in numbers {
let number = number.lowercased()

if number.count > 4 {
return InvalidIP
}

for char in number {
if !char.isHexDigit {
return InvalidIP
}
}
}

return IPv6
}
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
![Leetcode](./logo.png?style=centerme)

## Progress
[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.
[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.

## Contributors

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

0 comments on commit dd7df2e

Please sign in to comment.