Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BAEL-8920] Add code for restoring IP address #18109

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.baeldung.restoreipaddress;

import java.util.ArrayList;
import java.util.List;

public class Backtracking {
public List<String> restoreIPAddresses(String s) {
List<String> result = new ArrayList<>();
backtrack(result, s, new ArrayList<>(), 0);
return result;
}

private void backtrack(List<String> result, String s, List<String> current, int index) {
if (current.size() == 4) {
if (index == s.length()) {
result.add(String.join(".", current));
}
return;
}

for (int len = 1; len <= 3; len++) {
if (index + len > s.length()) break;

String part = s.substring(index, index + len);
if (isValid(part)) {
current.add(part);
backtrack(result, s, current, index + len);
current.remove(current.size() - 1);
}
}
}

private boolean isValid(String part) {
if (part.length() > 1 && part.startsWith("0")) return false;
int num = Integer.parseInt(part);
return num >= 0 && num <= 255;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.baeldung.restoreipaddress;

import java.util.ArrayList;
import java.util.List;

public class DynamicProgramming {
public List<String> restoreIPAddresses(String s) {
int n = s.length();
if (n < 4 || n > 12) return new ArrayList<>();

List<String>[][] dp = new ArrayList[n + 1][5];
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= 4; j++) {
dp[i][j] = new ArrayList<>();
}
}

dp[0][0].add("");

for (int i = 1; i <= n; i++) {
for (int k = 1; k <= 4; k++) {
for (int j = 1; j <= 3 && i - j >= 0; j++) {
String segment = s.substring(i - j, i);
if (isValid(segment)) {
for (String prefix : dp[i - j][k - 1]) {
dp[i][k].add(prefix.isEmpty() ? segment : prefix + "." + segment);
}
}
}
}
}

return dp[n][4];
}

private boolean isValid(String part) {
if (part.length() > 1 && part.startsWith("0")) return false;
int num = Integer.parseInt(part);
return num >= 0 && num <= 255;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.baeldung.restoreipaddress;

import java.util.ArrayList;
import java.util.List;

public class Iterative {
public List<String> restoreIPAddresses(String s) {
List<String> result = new ArrayList<>();

if (s.length() < 4 || s.length() > 12) {
return result;
}

for (int i = 1; i < 4; i++) {
for (int j = i + 1; j < Math.min(i + 4, s.length()); j++) {
for (int k = j + 1; k < Math.min(j + 4, s.length()); k++) {
String part1 = s.substring(0, i);
String part2 = s.substring(i, j);
String part3 = s.substring(j, k);
String part4 = s.substring(k);

if (isValid(part1) && isValid(part2) && isValid(part3) && isValid(part4)) {
result.add(part1 + "." + part2 + "." + part3 + "." + part4);
}
}
}
}
return result;
}

private boolean isValid(String part) {
if (part.length() > 1 && part.charAt(0) == '0') {
return false;
}
int num = Integer.parseInt(part);
return num >= 0 && num <= 255;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.baeldung.restoreipaddress;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;

import org.junit.jupiter.api.Test;

public class BacktrackingUnitTest {
Backtracking backtracking = new Backtracking();

@Test
public void givenValidInput_whenUsingBacktracking_thenRestoreIPAddress() {
String input = "25525511135";
List<String> result = backtracking.restoreIPAddresses(input);
assertTrue(result.contains("255.255.11.135"));
assertTrue(result.contains("255.255.111.35"));
assertEquals(2, result.size());
}

@Test
public void givenMinimumLength_whenUsingBacktracking_thenRestoreIPAddress() {
String input = "4567";
List<String> result = backtracking.restoreIPAddresses(input);
assertTrue(result.contains("4.5.6.7"));
assertEquals(1, result.size());
}

@Test
public void givenEmptyString_whenUsingBacktracking_thenReturnEmptyList() {
String input = "";
List<String> result = backtracking.restoreIPAddresses(input);
assertTrue(result.isEmpty());
}

@Test
public void givenLongString_whenUsingBacktracking_thenReturnEmptyList() {
String input = "123456789012345";
List<String> result = backtracking.restoreIPAddresses(input);
assertTrue(result.isEmpty());
}

@Test
public void givenShortString_whenUsingBacktracking_thenReturnEmptyList() {
String input = "456";
List<String> result = backtracking.restoreIPAddresses(input);
assertTrue(result.isEmpty());
}

@Test
public void givenInvalidParts_whenUsingBacktracking_thenReturnEmptyList() {
String input = "265265278294";
List<String> result = backtracking.restoreIPAddresses(input);
assertTrue(result.isEmpty());
}

@Test
public void givenLeadingZeros_whenUsingBacktracking_thenRestoreIPAddress() {
String input = "010010";
List<String> result = backtracking.restoreIPAddresses(input);
assertTrue(result.contains("0.10.0.10"));
assertTrue(result.contains("0.100.1.0"));
assertEquals(2, result.size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.baeldung.restoreipaddress;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;

import org.junit.jupiter.api.Test;

public class DynamicProgrammingUnitTest {
DynamicProgramming dynamicProgramming = new DynamicProgramming();

@Test
public void givenValidInput_whenUsingIterative_thenRestoreIPAddress() {
String input = "25525511135";
List<String> result = dynamicProgramming.restoreIPAddresses(input);
assertTrue(result.contains("255.255.11.135"));
assertTrue(result.contains("255.255.111.35"));
assertEquals(2, result.size());
}

@Test
public void givenMinimumLength_whenUsingIterative_thenRestoreIPAddress() {
String input = "4567";
List<String> result = dynamicProgramming.restoreIPAddresses(input);
assertTrue(result.contains("4.5.6.7"));
assertEquals(1, result.size());
}

@Test
public void givenEmptyString_whenUsingIterative_thenReturnEmptyList() {
String input = "";
List<String> result = dynamicProgramming.restoreIPAddresses(input);
assertTrue(result.isEmpty());
}

@Test
public void givenLongString_whenUsingIterative_thenReturnEmptyList() {
String input = "123456789012345";
List<String> result = dynamicProgramming.restoreIPAddresses(input);
assertTrue(result.isEmpty());
}

@Test
public void givenShortString_whenUsingIterative_thenReturnEmptyList() {
String input = "456";
List<String> result = dynamicProgramming.restoreIPAddresses(input);
assertTrue(result.isEmpty());
}

@Test
public void givenInvalidParts_whenUsingIterative_thenReturnEmptyList() {
String input = "265265278294";
List<String> result = dynamicProgramming.restoreIPAddresses(input);
assertTrue(result.isEmpty());
}

@Test
public void givenLeadingZeros_whenUsingIterative_thenRestoreIPAddress() {
String input = "010010";
List<String> result = dynamicProgramming.restoreIPAddresses(input);
assertTrue(result.contains("0.10.0.10"));
assertTrue(result.contains("0.100.1.0"));
assertEquals(2, result.size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.baeldung.restoreipaddress;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.util.List;

public class IterativeUnitTest {
Iterative iterative = new Iterative();

@Test
public void givenValidInput_whenUsingIterative_thenRestoreIPAddress() {
String input = "25525511135";
List<String> result = iterative.restoreIPAddresses(input);
assertTrue(result.contains("255.255.11.135"));
assertTrue(result.contains("255.255.111.35"));
assertEquals(2, result.size());
}

@Test
public void givenMinimumLength_whenUsingIterative_thenRestoreIPAddress() {
String input = "4567";
List<String> result = iterative.restoreIPAddresses(input);
assertTrue(result.contains("4.5.6.7"));
assertEquals(1, result.size());
}

@Test
public void givenEmptyString_whenUsingIterative_thenReturnEmptyList() {
String input = "";
List<String> result = iterative.restoreIPAddresses(input);
assertTrue(result.isEmpty());
}

@Test
public void givenLongString_whenUsingIterative_thenReturnEmptyList() {
String input = "123456789012345";
List<String> result = iterative.restoreIPAddresses(input);
assertTrue(result.isEmpty());
}

@Test
public void givenShortString_whenUsingIterative_thenReturnEmptyList() {
String input = "456";
List<String> result = iterative.restoreIPAddresses(input);
assertTrue(result.isEmpty());
}

@Test
public void givenInvalidParts_whenUsingIterative_thenReturnEmptyList() {
String input = "265265278294";
List<String> result = iterative.restoreIPAddresses(input);
assertTrue(result.isEmpty());
}

@Test
public void givenLeadingZeros_whenUsingIterative_thenRestoreIPAddress() {
String input = "010010";
List<String> result = iterative.restoreIPAddresses(input);
assertTrue(result.contains("0.10.0.10"));
assertTrue(result.contains("0.100.1.0"));
assertEquals(2, result.size());
}
}