-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathvalid_IP_address.cpp
More file actions
126 lines (115 loc) · 2.08 KB
/
valid_IP_address.cpp
File metadata and controls
126 lines (115 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Java Program to generate all possible
// valid IP addresses from given string
import java.util.*;
class GFG
{
public static ArrayList<String> list;
// Function to restore Ip Addresses
public static ArrayList<String>
restoreIpAddresses(String s)
{
int n = s.length();
list = new ArrayList<>();
if (n < 4 || n > 12)
return list;
// initialize the dp array
int dp[][] = new int[4][n];
for (int i = 0; i < 4; i++)
{
for (int j = n - 1; j >= 0; j--)
{
if (i == 0)
{
// take the substring
String sub = s.substring(j);
if (isValid(sub))
{
dp[i][j] = 1;
}
else if (j < n - 3)
{
break;
}
}
else
{
if (j <= n - i)
{
for (int k = 1;
k <= 3 && j + k <= n;
k++)
{
String temp
= s.substring(j, j + k);
if (isValid(temp))
{
if (j + k < n
&& dp[i - 1][j + k]
== 1)
{
dp[i][j] = 1;
break;
}
}
}
}
}
}
}
if (dp[3][0] == 0)
return list;
// Call function createfromDp
createIpFromDp(dp, 3, 0, s, "");
return list;
}
public static void createIpFromDp(int dp[][],
int r,
int c, String s,
String ans)
{
if (r == 0)
{
ans += s.substring(c);
list.add(ans);
return;
}
for (int i = 1;
i <= 3 && c + i < s.length();
i++)
{
if (isValid(s.substring(c, c + i))
&& dp[r - 1] == 1)
{
createIpFromDp(dp, r - 1, c + i, s,
ans +
s.substring(c, c + i)
+ ".");
}
}
}
private static boolean isValid(String ip)
{
String a[] = ip.split("[.]");
for (String s : a)
{
int i = Integer.parseInt(s);
if (s.length() > 3 || i < 0 || i > 255)
{
return false;
}
if (s.length() > 1 && i == 0)
return false;
if (s.length() > 1 && i != 0
&& s.charAt(0) == '0')
return false;
}
return true;
}
// Driver Code
public static void main(String[] args)
{
// Function call
System.out.println(
restoreIpAddresses("25525511135").toString());
}
}