Skip to content

Commit 9a562df

Browse files
add 468
1 parent af26eac commit 9a562df

File tree

3 files changed

+170
-9
lines changed

3 files changed

+170
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ Your ideas/fixes/algorithms are more than welcome!
115115
|473|[Matchsticks to Square](https://leetcode.com/problems/matchsticks-to-square/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_473.java) | O(n!) |O(n) | Medium| Backtracking, DFS
116116
|472|[Concatenated Words](https://leetcode.com/problems/concatenated-words/)|[Solution](../master/src/main/java/com/fishercoder/solutions/ConcatenatedWords.java) | O(n^2) |O(n) | Hard| Trie, DP, DFS
117117
|469|[Convex Polygon](https://leetcode.com/problems/convex-polygon/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_469.java) | O(n) |O(1) | Medium| Math
118-
|468|[Validate IP Address](https://leetcode.com/problems/validate-ip-address/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_468.java) | O(n^2) |O(n) | Hard| Trie, DP, DFS
118+
|468|[Validate IP Address](https://leetcode.com/problems/validate-ip-address/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_468.java) | O(n) |O(1) | Medium | String
119119
|467|[Unique Substrings in Wraparound String](https://leetcode.com/problems/unique-substrings-in-wraparound-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_467.java) | O(n) |O(1) | Medium| DP
120120
|466|[Count The Repetitions](https://leetcode.com/problems/count-the-repetitions/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_466.java)| O(max(m,n))|O(1) | Hard| DP
121121
|464|[Can I Win](https://leetcode.com/problems/can-i-win/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_464.java)| O(2^n)|O(n) | Medium| DP

src/main/java/com/fishercoder/solutions/_468.java

Lines changed: 84 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
11
package com.fishercoder.solutions;
22

3+
import java.util.Arrays;
4+
import java.util.HashSet;
5+
import java.util.Set;
6+
37
/**
8+
* 468. Validate IP Address
49
* Write a function to check whether an input string is a valid IPv4 address or IPv6 address or neither.
5-
6-
IPv4 addresses are canonically represented in dot-decimal notation, which consists of four decimal numbers, each ranging from 0 to 255, separated by dots ("."), e.g.,172.16.254.1;
7-
10+
IPv4 addresses are canonically represented in dot-decimal notation, which consists of four decimal numbers,
11+
each ranging from 0 to 255, separated by dots ("."), e.g.,172.16.254.1;
812
Besides, leading zeros in the IPv4 is invalid. For example, the address 172.16.254.01 is invalid.
913
10-
IPv6 addresses are represented as eight groups of four hexadecimal digits, each group representing 16 bits. The groups are separated by colons (":"). For example, the address 2001:0db8:85a3:0000:0000:8a2e:0370:7334 is a valid one. Also, we could omit some leading zeros among four hexadecimal digits and some low-case characters in the address to upper-case ones, so 2001:db8:85a3:0:0:8A2E:0370:7334 is also a valid IPv6 address(Omit leading zeros and using upper cases).
14+
IPv6 addresses are represented as eight groups of four hexadecimal digits,
15+
each group representing 16 bits.
16+
The groups are separated by colons (":").
17+
For example, the address 2001:0db8:85a3:0000:0000:8a2e:0370:7334 is a valid one.
18+
Also, we could omit some leading zeros among four hexadecimal digits and
19+
some low-case characters in the address to upper-case ones,
20+
so 2001:db8:85a3:0:0:8A2E:0370:7334 is also a valid IPv6 address(Omit leading zeros and using upper cases).
1121
12-
However, we don't replace a consecutive group of zero value with a single empty group using two consecutive colons (::) to pursue simplicity. For example, 2001:0db8:85a3::8A2E:0370:7334 is an invalid IPv6 address.
22+
However, we don't replace a consecutive group of zero value with a single empty
23+
group using two consecutive colons (::) to pursue simplicity.
24+
For example, 2001:0db8:85a3::8A2E:0370:7334 is an invalid IPv6 address.
1325
14-
Besides, extra leading zeros in the IPv6 is also invalid. For example, the address 02001:0db8:85a3:0000:0000:8a2e:0370:7334 is invalid.
26+
Besides, extra leading zeros in the IPv6 is also invalid.
27+
For example, the address 02001:0db8:85a3:0000:0000:8a2e:0370:7334 is invalid.
1528
1629
Note: You may assume there is no extra space or special characters in the input string.
1730
@@ -42,9 +55,72 @@
4255
*/
4356
public class _468 {
4457

58+
static final String NEITHER = "Neither";
4559
public String validIPAddress(String IP) {
46-
String[] bytes = IP.split(".");
47-
return "";
60+
if (IP.contains(".")) {
61+
return isValidIPv4(IP);
62+
} else if (IP.contains(":")) {
63+
return isValidIPv6(IP);
64+
} else return NEITHER;
65+
}
66+
67+
private String isValidIPv6(String IP) {
68+
if (getDelimiterCount(IP, ':') != 7) return NEITHER;
69+
String[] bytes = IP.split("\\:");
70+
if (bytes.length != 8) return NEITHER;
71+
for (int i = 0; i < 8; i++) {
72+
if (hasInvalidIPV6Char(bytes[i])) return NEITHER;
73+
try {
74+
if (bytes[i].length() > 4) return NEITHER;
75+
int intNum = Integer.parseInt(bytes[i], 16);
76+
if (intNum < 0) return NEITHER;
77+
if (i == 0 && intNum != 0 && bytes[i].charAt(0) == '0') return NEITHER;
78+
} catch (Exception e) {
79+
return NEITHER;
80+
}
81+
82+
}
83+
return "IPv6";
84+
}
85+
86+
private String isValidIPv4(String IP) {
87+
if (getDelimiterCount(IP, '.') != 3) return NEITHER;
88+
String[] bytes = IP.split("\\.");
89+
if (bytes.length != 4) return NEITHER;
90+
for (String num : bytes) {
91+
try {
92+
int intNum = Integer.parseInt(num);
93+
if (intNum > 255 || intNum < 0) return NEITHER;
94+
if (intNum != 0 ) {
95+
for (int i = 0; i < num.length(); i++) {
96+
if (num.charAt(i) == '0') return NEITHER;
97+
else break;
98+
}
99+
} else if (intNum == 0) {
100+
if (num.length() != 1) return NEITHER;
101+
}
102+
} catch (Exception e) {
103+
return NEITHER;
104+
}
105+
}
106+
return "IPv4";
107+
}
108+
109+
private boolean hasInvalidIPV6Char(String str) {
110+
Set<Character> set = new HashSet<>(Arrays.asList('0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
111+
'a', 'b', 'c', 'd', 'e', 'f', 'A', 'B', 'C', 'D', 'E', 'F'));
112+
for (char c : str.toCharArray()) {
113+
if (!set.contains(c)) return true;
114+
}
115+
return false;
116+
}
117+
118+
private int getDelimiterCount(String ip, char delimiter) {
119+
int count = 0;
120+
for (char c : ip.toCharArray()) {
121+
if (c == delimiter) count++;
122+
}
123+
return count;
48124
}
49125

50126
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._468;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
/**
10+
* Created by stevesun on 6/10/17.
11+
*/
12+
public class _468Test {
13+
private static _468 test;
14+
15+
@BeforeClass
16+
public static void setup(){
17+
test = new _468();
18+
}
19+
20+
@Test
21+
public void test1(){
22+
assertEquals("IPv4", test.validIPAddress("172.16.254.1"));
23+
}
24+
25+
@Test
26+
public void test2(){
27+
assertEquals("IPv6", test.validIPAddress("2001:0db8:85a3:0:0:8A2E:0370:7334"));
28+
}
29+
30+
@Test
31+
public void test3(){
32+
assertEquals("Neither", test.validIPAddress("2001:0db8:85a3::8A2E:0370:7334"));
33+
}
34+
35+
@Test
36+
public void test4(){
37+
assertEquals("Neither", test.validIPAddress("02001:0db8:85a3:0000:0000:8a2e:0370:7334"));
38+
}
39+
40+
@Test
41+
public void test5(){
42+
assertEquals("Neither", test.validIPAddress("256.256.256.256"));
43+
}
44+
45+
@Test
46+
public void test6(){
47+
assertEquals("Neither", test.validIPAddress("2001:0db8:85a3:0:0:8A2E:0370:7334:"));
48+
}
49+
50+
@Test
51+
public void test7(){
52+
assertEquals("Neither", test.validIPAddress("01.01.01.01"));
53+
}
54+
55+
@Test
56+
public void test8(){
57+
assertEquals("Neither", test.validIPAddress("00.0.0.0"));
58+
}
59+
60+
@Test
61+
public void test9(){
62+
assertEquals("Neither", test.validIPAddress("000.0.0.0"));
63+
}
64+
65+
@Test
66+
public void test10(){
67+
assertEquals("Neither", test.validIPAddress("0000.0.0.0"));
68+
}
69+
70+
@Test
71+
public void test11(){
72+
assertEquals("IPv4", test.validIPAddress("0.0.0.0"));
73+
}
74+
75+
@Test
76+
public void test12(){
77+
assertEquals("Neither", test.validIPAddress("1081:db8:85a3:01:-0:8A2E:0370:7334"));
78+
}
79+
80+
@Test
81+
public void test13(){
82+
assertEquals("Neither", test.validIPAddress("1081:db8:85a3:01:z:8A2E:0370:7334"));
83+
}
84+
85+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy