搜索此博客

2013年2月7日星期四

Restore IP Address

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)
Solution: this question is not easy. The most straightforward way is to do is recursively. 
Note, as "." is a special character in regular expression, I use "," in the intermediate process to be a separator.

01 public ArrayList<String> restoreIpAddresses(String s) {
02    ArrayList<String> results=new ArrayList<String>();
03     if(s.length()<4)
04         return results;
05     ArrayList<String> tempResults=new ArrayList<String>();
06     convert(results,tempResults,s,0);
07     return results;
08 }
09 
10 public void convert(ArrayList<String>results,ArrayList<String> tempResults,String s,int start)
11 {
12   //base case: reach the last two digit
13     if(start==s.length()-2)
14     {
15         String sb1=new String();
16         sb1+=s.substring(start);
17         tempResults.add(sb1);
18         String sb2=new String();
19       // because "." is used in the regular expression, we use "," instead as the separater
20         sb2+=s.substring(start,start+1)+","+s.substring(start+1,start+2);      
21         tempResults.add(sb2);
22     }
23     else
24     {
25      //call itself recursively to get the previous value
26         convert(results,tempResults,s,start+1);
27         for(int i=tempResults.size()-1;i>=0;i--)
28         {
29           //two possible strings
30           //sb is to append the sequence right after the current character
31           //sa is to append the separater and the later sequence after the current character
32             String sb=tempResults.get(i);
33             String sa=new String(sb);            
34             sb=s.charAt(start)+sb;              
35             String[]sb_array=sb.split(",");
36             int value=Integer.parseInt(sb_array[0]);                              
37             if(value<=255)
38                 tempResults.set(i,sb);
39             else
40                 tempResults.remove(i);
41           //finish parsing and output the result
42             if(sb.length()==s.length()+3 && sb.charAt(0)==s.charAt(0))
43             {
44                 String[] ch_b=sb.split(",");
45                 if(ch_b[0].length()==1 || ch_b[0].charAt(0)-'0'>0)
46                 {
47                     int val=Integer.parseInt(ch_b[0]);
48                     if(val<=255)
49                     {
50                         sb=sb.replace(",", ".");
51                         results.add(sb);
52                     }
53                 }                        
54             }
55         
56           //handle sa similarly
57             sa=s.charAt(start)+","+sa;
58             String[]sa_array=sa.split(",");
59             if(sa_array.length<=4)
60             {
61               
62                 if(sa_array[1].length()==1 || (sa_array[1].length()>1 && sa_array[1].charAt(0)-'0'>0))
63                 {
64                     tempResults.add(sa);
65                     if(sa.length()==s.length()+3 && sa.charAt(0)==s.charAt(0))
66                     {
67                         if(sa_array[0].length()==1 ||sa_array[0].charAt(0)-'0'>0)
68                         {
69                              if(Integer.parseInt(sa_array[0])<=255)
70                             {
71                                 sa=sa.replace(",", ".");
72                                     results.add(sa);
73                             }
74                         }
75                     }
76                 }
77             }              
78         }
79     }
80 }

没有评论:

发表评论