Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

How to generate all possible IP addresses based on a given set of digits, in Java?


/*
	This implementation demonstrates how 
	to generate all valid IP addresses
	based on a string that is made up of 
	digits only.

	Time complexity: O(1) 
	Space complexity: O(1)
*/
import java.util.List;
import java.util.ArrayList;

public class ValidIPAddresses {
	public static void main(String[] args) {
		String s = "25525511135";
		// Below prints: [255.255.11.135, 255.255.111.35]
		System.out.println(generateIpAddresses(s));
	}

	private static List<String> generateIpAddresses(String s) {
		List<String> allPossibleIPs = new ArrayList<>();
		int length = s.length();
		for (int idx1 = 1; idx1 < Math.min(length, 4); idx1++) {
			String[] currentIPParts = new String[] { "", "", "", "" };
			currentIPParts[0] = s.substring(0, idx1);
			if (!isValid(currentIPParts[0])) {
				continue;
			}

			for (int idx2 = idx1 + 1; idx2 < idx1 + Math.min(length - idx1, 4); idx2++) {
				currentIPParts[1] = s.substring(idx1, idx2);
				if (!isValid(currentIPParts[1])) {
					continue;
				}

				for (int idx3 = idx2 + 1; idx3 < idx2 + Math.min(length - idx2, 4); idx3++) {
					currentIPParts[2] = s.substring(idx2, idx3);
					currentIPParts[3] = s.substring(idx3);
					if (isValid(currentIPParts[2]) && isValid(currentIPParts[3])) {
						allPossibleIPs.add(String.join(".", currentIPParts));
					}

				}
			}

		}

		return allPossibleIPs;
	}

	private static boolean isValid(String part) {
		if (part.length() > 3) {
			return false;
		}
		int partAsInt = Integer.parseInt(part);
		// Any part greater than 255 is not valid
		if (partAsInt > 255) {
			return false;
		}
		// Account for leading 0s
		return part.length() == Integer.toString(partAsInt).length();
	}

}
Comment

PREVIOUS NEXT
Code Example
Java :: implementing iterator for linked list java 
Java :: Hourglass Java 
Java :: square root of a number in java without sqrt 
Java :: print an array java 
Java :: java date format with timezone 
Java :: get random number from enum in java 
Java :: java catch multiple exceptions 
Java :: java create file in folder 
Java :: next greater permutation leetcode 
Java :: No Java executable found in current PATH: /bin:/usr/bin:/sbin:/usr/sbin 
Java :: spring boot actuator 
Java :: spring boot save file to static folder 
Java :: sort descending java stream 
Java :: java if or 
Java :: print list of map java 
Java :: uppercase java 
Java :: set image programmatically android 
Java :: react The href attribute is required for an anchor to be keyboard 
Java :: java string array initialization 
Java :: how to get last element in java 
Java :: java localtime 
Java :: java sum of array elements 
Java :: how to use sql file in java 
Java :: random number generator java with range 
Java :: java long to hours minutes and seconds 
Java :: final in java 
Java :: linear search java 
Java :: code to include layout from java in android 
Java :: java sql query with parameters 
Java :: @crossorigin spring allow all 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =