Skip to content

Commit cde15a2

Browse files
authored
Create 0093-restore-ip-addresses.kt
1 parent 545be8a commit cde15a2

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

kotlin/0093-restore-ip-addresses.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
fun restoreIpAddresses(s: String): List<String> {
3+
val res = mutableListOf<String>()
4+
5+
fun backTrack(i: Int, dots: Int, cur: String) {
6+
if (dots == 4 && i == s.length) {
7+
res.add(cur.substring(0, cur.lastIndex))
8+
return
9+
}
10+
if (dots > 4) return
11+
12+
for (j in i until minOf(i + 3, s.length)) {
13+
val digits = s.substring(i, j + 1)
14+
if (digits.toInt() <= 255 && (i == j || s[i] != '0'))
15+
backTrack(j + 1, dots + 1, cur + digits + ".")
16+
}
17+
}
18+
19+
backTrack(0, 0, "")
20+
return res
21+
}
22+
}

0 commit comments

Comments
 (0)