Skip to content

Commit

Permalink
Add Leetcode: Restore IP Addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
aykrieger committed Jul 7, 2022
1 parent 5c3b08c commit 3b0b9c7
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,14 @@ HackerRank assigns their problems a difficulty rating (Easy, Medium, Hard, Exper

- Employee Names (Easy) [Problem](https://www.hackerrank.com/challenges/name-of-employees/problem) | [Solution](https://github.com/aykrieger/coding-exercises/blob/master/sql/employee-names.sql)

## LeetCode (2 Problems)
## LeetCode (3 Problems)

- K-diff Pairs in an Array (Medium) [Problem](https://leetcode.com/problems/k-diff-pairs-in-an-array/) | [Solution](https://github.com/aykrieger/coding-exercises/blob/master/src/main/java/leetcode/KDiffPairsInAnArray.java)

- Longest Substring Without Repeating Characters (Medium) [Problem](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [Solution](https://github.com/aykrieger/coding-exercises/blob/master/src/main/java/leetcode/LongestSubstringWithoutRepeatingCharacters.java)

- Restore IP Addresses (Medium) [Problem](https://leetcode.com/problems/restore-ip-addresses/) | [Solution](https://github.com/aykrieger/coding-exercises/blob/master/src/main/java/leetcode/RestoreIPAddresses.java)

## aykrieger (1 Problem)

- First Non-Duplicate [Solution](https://github.com/aykrieger/coding-exercises/blob/master/src/main/java/aykrieger/FirstNonDuplicate.java)
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/leetcode/RestoreIPAddresses.java
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;
}
}
48 changes: 48 additions & 0 deletions src/test/java/leetcode/RestoreIPAddressesTest.java
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);
}
}

0 comments on commit 3b0b9c7

Please sign in to comment.