Question:
Given a string containing only digits, restore it by returning all possible valid IP address combinations.
For example:
Given "25525511135 ",
Return ["commandid 11.135", "commandid 111.35"]. (Order does not matter)
The Code is as follows:
Bool iscorrect (string s)
{
If (s. length () = 2)
{
If (s [0] = '0') return false;
}
If (s. length () = 3)
{
Int tmp = (s [0]-'0') x 100 + (s [1]-'0') * 10 + (s [2]-'0 ');
If (tmp <100 | tmp> 255) return false;
}
Return true;
}
Bool istrue (string a, string B, string c, string d)
{
If (a. length ()> 3 | B. length ()> 3 | c. length ()> 3 | d. length ()> 3)
{
Return false;
}
If (iscorrect (a) & iscorrect (B) & iscorrect (c) & iscorrect (d ))
{
Return true;
}
Else
{
Return false;
}
}
Vector <string> restoreIpAddresses (string s ){
Int n = s. length ();
Vector <string> result;
If (n <4 | n> 12) return result;
Int I = 0;
For (int j = 1; j <N-2; j ++)
{
For (int k = j + 1; k <n-1; k ++)
{
For (int t = k + 1; t <n; t ++)
{
String a = s. substr (I, j-I), B = s. substr (j, k-j), c = s. substr (k, t-k), d = s. substr (t, n-k );
If (istrue (a, B, c, d ))
{
String tmp;
Tmp = a + "." + B + "." + c + "." + d;
Result. push_back (tmp );
}
}
}
}
Return result;