Given a string containing only digits, restore it by returning all possible valid IP address combinations.
Input: "25525511135"
Output: ["255.255.11.135", "255.255.111.35"]
class Solution {
public List<String> restoreIpAddresses(String s) {
List<String> res = new ArrayList<>();
helper(res,s,0,0,"");
return res;
}
public void helper(List<String> res, String ip, int index, int count,String store) {
if(count > 4) return;
if(count == 4 && index == ip.length()) res.add(store) ;
for(int i = 1 ; i < 4; i++){
if(index + i > ip.length()) break;
String s = ip.substring(index,index+i);
if(s.startsWith("0") && s.length() > 1 || i ==3 && Integer.parseInt(s) >=256) continue;
helper(res,ip,index+i,count+1,store+s+(count == 3 ? "":"."));
}
}
}