Restore IP Addresses

Given a String contains only digits, restore it by returning all possible IP address combinations.

e.g. "25525511135" return ["255.255.11.135", "255.255.111.35"].

Solution:

1. We should know what is a valid IP address. A valid IP address is formed of 4 parts. Each part can not be more than 255 or small than 0. Also, we can not have leading 0s if the number is not 0. We form a IP address in four steps, each step of the first three steps, we add a IP section number and '.'. For the final step, we only add the section number.

2. Write a function to determine if a IP section number is valid. Write a function to determine on each step if the remaining length of number is valid to continue forming a valid IP address.

3. Then wirte a function to recursively add IPs to the result list. If step is 4 and the remaining number is valid, we add it to the StringBuilder and add the string value of it to the result list. And delete the part we just added. Otherwise for length of the section number from 1 to 3, we get each section number from the start point and determine if it is valid and if we can continue the next step using the function we wrote in 2. If all valid, we add it to the StringBuilder and recursively invoke this function and delete the part we added from the StringBuilder.

4. In the main method, we invoke the function we wrote in 3. with parameters Given String s, start = 0, step = 1, remaining = s.length, an empty StringBuilder, and an empty result list.

Code:

public class Solution {
    public List<String> restoreIpAddresses(String s) {
        List<String> res = new ArrayList<>();
        if(s.length()==0 || s.length()>12)
            return res;
        StringBuilder temp = new StringBuilder();
        addIPs(s, res, temp, 1, 0, s.length());
        return res;
    }
    
    private void addIPs(String s, List<String> result, StringBuilder temp, int step, int start, int remaining){
        if(step == 4){
            String t = s.substring(start);
            if(!isValidIPSection(t))
                return;
            temp.append(t);
            result.add(temp.toString());
            temp.delete(start+3, temp.length());
            return;
        }
        for(int i = 1; i < 4; i++){
            if(!isValidToRemain(step, remaining-i) || !isValidIPSection(s.substring(start, start+i)))
                continue;
            temp.append(s.substring(start, start+i));
            temp.append('.');
            addIPs(s, result, temp, step+1, start+i, remaining-i);
            temp.delete(temp.length()-1-i, temp.length());
        }
    }
    
    private boolean isValidIPSection(String s){
        if(s.length() > 4 || s.length() == 0)
            return false;
        if(s.length()!=1 && s.charAt(0) == '0')
            return false;
        int n = Integer.parseInt(s);
        if(n > 255 || n < 0)
            return false;
        return true;
    }
    private boolean isValidToRemain(int step, int remaining){
        switch(step){
            case 1 : return remaining<=9&&remaining>=3;
            case 2 : return remaining<=6&&remaining>=2;
            case 3 : return remaining<=3&&remaining>=1;
            default: return false;
        }
    }
}
View Code

 

posted @ 2017-06-24 02:38  小风约定  阅读(71)  评论(0)    收藏  举报