import java.util.*;
public class Solution {
public ArrayList<String> restoreIpAddresses(String s) {
ArrayList<String> res=new ArrayList<String>();
String temp="";
dfs(s,0,temp,res);//temp 已经处理完的,s剩下未处理的
return res;
}
public static void dfs(String s,int n,String temp,ArrayList<String> res){
if(n==3&&isValid(s)){
res.add(temp+s);
return ;
}
for(int i=0;i<4&&i<s.length();i++){
String sub=s.substring(0,i);
if(isValid(sub)){
dfs(s.substring(i),n+1,temp+sub+".",res);
}
}
}
public static boolean isValid(String s){
if(s.length()>3||s.length()==0||(s.length()>1&&s.charAt(0)=='0')
||(s.length()>0&&Integer.parseInt(s)>255)){
return false;
}
return true;
}
}