Given a string containing only digits, restore it by returning all possible valid IP address combinations.
Example:
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, 0, 0, s, ""); return res; } private void helper(List<String> res, int count, int index, String s, String str) { if (count > 4) { return; } if (count == 4 && index == s.length()) { res.add(str); } for (int i = 1; i < 4; i++) { if (index + i > s.length()) { break; } String cur = s.substring(index, index + i); if (cur.charAt(0) == '0' && cur.length() > 1 || cur.length() == 3 && Integer.parseInt(cur) > 255) { continue; } String signal = count == 3 ? "": "."; helper(res, count + 1, index + i, s, str + cur + signal); } } }