93. Restore IP Addresses
Last updated
Last updated
/**
* Time complexity :
* Space complexity :
*/
class Solution {
public List<String> restoreIpAddresses(String s) {
List<String> resultList = new ArrayList<>();
int n = s.length();
if(n < 4 || n > 12) return resultList;
for(int p = 1; p < 4; p++) {
for(int q = 1; q < 4; q++) {
for(int r = 1; r < 4; r++) {
for(int t = 1; t < 4; t++) {
if(p+q+r+t == n) {
int v1 = Integer.parseInt(s.substring(0, p));
if(v1 > 255) continue;
int v2 = Integer.parseInt(s.substring(p, p+q));
if(v2 > 255) continue;
int v3 = Integer.parseInt(s.substring(p+q, p+q+r));
if(v3 > 255) continue;
int v4 = Integer.parseInt(s.substring(p+q+r));
if(v4 > 255) continue;
String value = v1 + "." + v2 + "." + v3 + "." + v4;
if(value.length() == n+3) {
resultList.add(value);
}
}
}
}
}
}
return resultList;
}
}