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;
}
}