-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path93-RestoreIpAddresses.kt
72 lines (55 loc) · 1.95 KB
/
93-RestoreIpAddresses.kt
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
/**
*
* 93. Restore IP Addresses
* Daily Coding Problem #213
* https://leetcode.com/problems/restore-ip-addresses/
*
*/
class Solution {
val result = mutableSetOf<String>()
val memo = mutableSetOf<String>()
fun restoreIpAddresses(s: String): List<String> {
restoreIpAddresses(s, "", Pair(0, 1), 0)
restoreIpAddresses(s, "", Pair(0, 2), 0)
restoreIpAddresses(s, "", Pair(0, 3), 0)
return result.toList()
}
fun restoreIpAddresses(s: String, currRestored: String, group: Pair<Int, Int>, groupCount: Int) {
if (group.first >= s.length || group.second > s.length) {
return
}
val currentGroup = s.substring(group.first, group.second)
if (!isGroupValid(currentGroup) || groupCount == 4) {
return
}
val newGroup = bulidNewGroup(currRestored, currentGroup)
if (memo.contains("$currRestored$newGroup")) {
return
}
if (groupCount == 3) {
if (group.second == s.length) {
result.add(newGroup)
}
return
}
restoreIpAddresses(s, newGroup, Pair(group.second, group.second + 3), groupCount + 1)
restoreIpAddresses(s, newGroup, Pair(group.second, group.second + 2), groupCount + 1)
restoreIpAddresses(s, newGroup, Pair(group.second, group.second + 1), groupCount + 1)
memo.add("$currRestored$newGroup")
}
private fun bulidNewGroup(currRestored: String, currentGroup: String): String {
var separator = "."
if (currRestored.isEmpty()) {
separator = ""
}
val newGroup = "$currRestored$separator$currentGroup"
return newGroup
}
fun isGroupValid(group: String): Boolean {
if(group.isEmpty() || (group[0] == '0' && group.length > 1)) {
return false
}
val groupAsInt = group.toInt()
return groupAsInt in 0..255
}
}