-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package leetcode; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* @see <a href="https://leetcode.com/problems/restore-ip-addresses/">Restore IP Addresses</a> | ||
*/ | ||
public class RestoreIPAddresses { | ||
|
||
public static List<String> restoreIpAddresses(String input) { | ||
|
||
if (input.length() < 4 || input.length() > 12) { | ||
return new ArrayList<>(); | ||
} | ||
List<String> results = new ArrayList<>(); | ||
List<String> octets = new ArrayList<>(); | ||
validIPsHelper(results, octets, input, 0, 4); | ||
return results; | ||
} | ||
|
||
private static void validIPsHelper(List<String> results, List<String> octets, String input, | ||
int index, | ||
int octetsToAdd) { | ||
if (octetsToAdd < 1) { | ||
if (index == input.length()) { | ||
results.add(String.join(".", octets)); | ||
} | ||
return; | ||
} | ||
|
||
int curr = index; | ||
int maxIndex = curr + 3; | ||
while (curr < input.length() && curr < maxIndex) { | ||
String subString = input.substring(index, curr + 1); | ||
int num = Integer.parseInt(subString); | ||
if (subString.equals(Integer.toString(num)) && isValidOctet(num)) { | ||
octets.add(subString); | ||
validIPsHelper(results, octets, input, curr + 1, octetsToAdd - 1); | ||
octets.remove(octets.size() - 1); | ||
} else { | ||
break; | ||
} | ||
curr++; | ||
} | ||
} | ||
|
||
private static boolean isValidOctet(int num) { | ||
return num >= 0 && num <= 255; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package leetcode; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class RestoreIPAddressesTest { | ||
|
||
@Test | ||
public void testRestoreIpAddresses1() { | ||
List<String> expected = Arrays.asList("255.255.11.135", "255.255.111.35"); | ||
List<String> result = RestoreIPAddresses.restoreIpAddresses("25525511135"); | ||
Collections.sort(expected); | ||
Collections.sort(result); | ||
assertEquals(expected, result); | ||
} | ||
|
||
@Test | ||
public void testRestoreIpAddresses2() { | ||
List<String> expected = Arrays.asList("0.0.0.0"); | ||
List<String> result = RestoreIPAddresses.restoreIpAddresses("0000"); | ||
assertEquals(expected, result); | ||
} | ||
|
||
@Test | ||
public void testRestoreIpAddresses3() { | ||
List<String> expected = Arrays.asList("1.0.10.23", "1.0.102.3", "10.1.0.23", "10.10.2.3", | ||
"101.0.2.3"); | ||
List<String> result = RestoreIPAddresses.restoreIpAddresses("101023"); | ||
Collections.sort(expected); | ||
Collections.sort(result); | ||
assertEquals(expected, result); | ||
} | ||
|
||
@Test | ||
public void testRestoreIpAddresses4() { | ||
List<String> expected = Arrays.asList("1.92.168.11", "19.2.168.11", "19.21.68.11", | ||
"19.216.8.11", "19.216.81.1", "192.1.68.11", "192.16.8.11", "192.16.81.1", "192.168.1.1"); | ||
|
||
List<String> result = RestoreIPAddresses.restoreIpAddresses("19216811"); | ||
Collections.sort(expected); | ||
Collections.sort(result); | ||
assertEquals(expected, result); | ||
} | ||
} |