Restore IP Addresses Java

Given a string containing only digits, restore it by returning all possible valid IP address combinations.

For example:
Given "25525511135",

return ["255.255.11.135", "255.255.111.35"]. (Order does not matter)

Two Approach: the problem can solve by recursion and non-recursion method

Solution_1: Recursion Method

This is kind of NP-problem, but actually it is not, since we only need to
    conduct String into 4 segments to check wether is valid IP Address,
     and the length will be range of between 4 and 12
    Idea: same approach as N-Queens => loop-recursion method
    Restore IP Addresses:
    i: indicate range of each segment 1<=i<=3
    index: current index of String, used by substring method
    curr: store Valid IP address and concatenate together
    res: result Valid IP address list

public class Solution {
   public ArrayList<String> restoreIpAddresses(String s) {
        ArrayList<String> res=new ArrayList<String>();
        helper(s,0,1,"",res);
        return res;
    }
    private void helper(String s, int index, int segment, String curr, ArrayList<String> res){
        int len=s.length();
        if(segment==4){ //the case of 4th segment
            String str=s.substring(index);
            if(isValid(str)){
                res.add(curr+"."+str);
            }
            return;
        }
        for(int i=1;i<=3 && i+index<=len;i++){
                String str=s.substring(index,index+i);
            if(isValid(str)) {
                if (segment == 1) { //the case of 1st segment
                    helper(s, index + i, segment + 1, str, res);
                } else {
                    helper(s, index + i, segment + 1, curr + "." + str, res);
                }
            }
        }
    }
    private boolean isValid(String str){
        int len=str.length();
        if(len==0 || len>3){
            return false;
        }
        if(str.charAt(0)=='0' && len>1){
            return false;
        }
        int num=Integer.parseInt(str);
        if(num>=0 && num<=255){
            return true;
        }
        return false;
    }
}


Solution_2:Non-recursion method

    Solved by  the Nested loop with depth 3
    that considering up to 3 possibilities of making up the current part
    and we split each part iteratively by explicitly determining
    the positions of the dots.
    Check the coding in detail below: 

public class Solution {
   public ArrayList<String> restoreIpAddresses(String s) {
    ArrayList<String> result = new ArrayList<String>();
        int len=s.length();
        if(len<4 || len>12) return result;
        //1st segment
        for(int i=1;i<=3;i++){  //at most 3 digits at each segment
            if((len-i)>9) continue; //the remaining part should less than 9
            if(s.charAt(0)=='0' && i>1) break; //0x is not allow
            String firstSeg=s.substring(0,i);
            if(Integer.parseInt(firstSeg)<=255) { //>maximum ip number is 255
                2nd segment
                for(int j=i+1;j<=i+3 && j<len;j++){
                    if((len-j)>6) continue; the remaining part should less than 6
                    if(s.charAt(i)=='0' && j>i+1) break;
                    String secondSeg=s.substring(i,j);
                    if(Integer.parseInt(secondSeg)<=255){
                        //3rd segment
                        for(int m=j+1;m<=j+3 && m<len;m++){
                            if((len-m)>3) continue;
                            if(s.charAt(j)=='0' && m>j+1) break;
                            String thirdSeg=s.substring(j,m);
                            if(Integer.parseInt(thirdSeg)<=255){
                                // If the fourth part is a single digit, or it does not begin with a '0' and <=255
                                if(m==len-1 || s.charAt(m)!='0' && (Integer.parseInt(s.substring(m))<=255)){
                                        result.add(firstSeg+"."+secondSeg+"."+thirdSeg+"."+s.substring(m));
                                }
                            }
                        }
                    }
                }
            }
        }
        return result;
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值