Problem · String
Validate IP Address
Learn this problemProblem statement
Given a string queryIP, return "IPv4" if it is a valid IPv4 address, "IPv6" if it is a valid IPv6 address, or "Neither" otherwise.
IPv4 rules
- The address contains exactly four decimal segments separated by
.. - Each segment is an integer from
0through255. - A segment has no leading zero unless it is exactly
0. - Every character in a segment is a decimal digit.
IPv6 rules
- The address contains exactly eight groups separated by
:. - Each group contains one to four hexadecimal characters.
- Hexadecimal letters may be lowercase or uppercase.
- Leading zeroes are allowed, but an empty group is not.
Function
validIPAddress(queryIP: String) → StringExamples
Example 1
queryIP = "172.16.254.1"return = "IPv4"The address has four decimal segments, each is in range, and none has an invalid leading zero.
Example 2
queryIP = "2001:0db8:85a3:0:0:8A2E:0370:7334"return = "IPv6"The address has eight non-empty hexadecimal groups, and every group contains at most four characters.
Example 3
queryIP = "256.256.256.256"return = "Neither"Every segment is greater than 255, so this is not a valid IPv4 address.
Constraints
queryIPis a non-empty string.queryIPcontains only English letters, decimal digits,., and:.
More Postman problems
- Configuration SystemOA · Seen Sep 2020
- Large ResponsesOA · Seen Sep 2020
- Minimum Swaps to Sort an ArrayOA · Seen Aug 2020
- Without WhitespacesOA · Seen Sep 2019
- Maximum Laptop Rating in a Price RangeOA · Seen Aug 2019
- Encode and Decode a String StreamONSITE INTERVIEW
- Group Duplicate Files by ContentONSITE INTERVIEW