# LeetCode – 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"].

Java Solution

This is a typical search problem and it can be solved by using DFS.

```public List<String> restoreIpAddresses(String s) { ArrayList<ArrayList<String>> result = new ArrayList<ArrayList<String>>(); ArrayList<String> t = new ArrayList<String>(); dfs(result, s, 0, t);   ArrayList<String> finalResult = new ArrayList<String>();   for(ArrayList<String> l: result){ StringBuilder sb = new StringBuilder(); for(String str: l){ sb.append(str+"."); } sb.setLength(sb.length() - 1); finalResult.add(sb.toString()); }   return finalResult; }   public void dfs(ArrayList<ArrayList<String>> result, String s, int start, ArrayList<String> t){ //if already get 4 numbers, but s is not consumed, return if(t.size()>=4 && start!=s.length()) return;   //make sure t's size + remaining string's length >=4 if((t.size()+s.length()-start+1)<4) return;   //t's size is 4 and no remaining part that is not consumed. if(t.size()==4 && start==s.length()){ ArrayList<String> temp = new ArrayList<String>(t); result.add(temp); return; }   for(int i=1; i<=3; i++){ //make sure the index is within the boundary if(start+i>s.length()) break;   String sub = s.substring(start, start+i); //handle case like 001. i.e., if length > 1 and first char is 0, ignore the case. if(i>1 && s.charAt(start)=='0'){ break; }   //make sure each number <= 255 if(Integer.valueOf(sub)>255) break;   t.add(sub); dfs(result, s, start+i, t); t.remove(t.size()-1); } }```
Category >> Algorithms >> Interview
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
```<pre><code>
String foo = "bar";
</code></pre>
```
• Anish

Can we have a solution in non recursive manner ? I could not understand the recursive solution.

• Titan

Why so many cases to consider, check my code :

``` public ArrayList createValidIp(String prefix, int dots, String rem){ ArrayList l = new ArrayList(); if(rem.length() == 0 && dots == 4) { l.add(prefix.substring(0, prefix.length()-1)); return l; } if(rem.length() == 0 || dots == 4) return l;```

``` // Single digit : String num = String.valueOf(rem.charAt(0)); l = merge(l, createValidIp(prefix+num+".", dots+1, rem.substring(1))); // Double digit : if(rem.length() >= 2 && num.charAt(0) != '0'){ num = num + String.valueOf(rem.charAt(1)); l = merge(l, createValidIp(prefix+num+".", dots+1, rem.substring(2))); } // Triple digit : if(rem.length() >=3 && num.charAt(0) != '0'){ num = num + String.valueOf(rem.charAt(2)); if(Integer.parseInt(num) <=255) l = merge(l, createValidIp(prefix+num+".", dots+1, rem.substring(3))); } return l; } public ArrayList merge(ArrayList l1, ArrayList l2){ ArrayList l = new ArrayList(); for(String s : l1) l.add(s); for(String s : l2) l.add(s); return l; } ```

``` public ArrayList restoreIpAddresses(String a) { return createValidIp("", 0, a); } ```