-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRestoreIPAddresses.java
More file actions
29 lines (29 loc) · 1.26 KB
/
RestoreIPAddresses.java
File metadata and controls
29 lines (29 loc) · 1.26 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
/**
* reference to:https://leetcode.com/discuss/12790/my-code-in-java
* 3-loop divides the string s into 4 substring: s1, s2, s3, s4. Check if each substring is valid.
* In isValid, strings whose length greater than 3 or equals to 0 is not valid;
* or if the string's length is longer than 1 and the first letter is '0' then it's invalid;
* or the string whose integer representation greater than 255 is invalid.
*/
public class Solution {
public List<String> restoreIpAddresses(String s) {
List<String> res = new ArrayList<String>();
int len = s.length();
for(int i = 1; i<4 && i<len-2; i++){
for(int j = i+1; j<i+4 && j<len-1; j++){
for(int k = j+1; k<j+4 && k<len; k++){
String s1 = s.substring(0,i), s2 = s.substring(i,j), s3 = s.substring(j,k), s4 = s.substring(k,len);
if(isValid(s1) && isValid(s2) && isValid(s3) && isValid(s4)){
res.add(s1+"."+s2+"."+s3+"."+s4);
}
}
}
}
return res;
}
public boolean isValid(String s){
if(s.length()>3 || s.length()==0 || (s.charAt(0)=='0' && s.length()>1) || Integer.parseInt(s)>255)
return false;
return true;
}
}