-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0093.Restore IP Addresses.swift
51 lines (42 loc) · 1.46 KB
/
0093.Restore IP Addresses.swift
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
class Solution {
var result = [String]()
func restoreIpAddresses(_ s: String) -> [String] {
var dots = [Int]()
backtrack(Array(s), 0, &dots);
return result
}
private func backtrack(_ chars: [Character], _ start: Int, _ dots: inout [Int]) {
let remaining = chars.count - start
let remainingSegments = 4 - dots.count
if remaining > remainingSegments * 3 || remaining < remainingSegments {
return
}
if dots.count == 3 {
if valid(chars, start, remaining) {
var last = 0
var candidate = [Character]()
for dot in dots {
candidate += chars[last..<last + dot]
last += dot
candidate.append(".")
}
candidate += chars[start...]
result.append(String(candidate))
}
return
}
var position = 1
while position <= 3, position <= remaining {
dots.append(position)
if valid(chars, start, position) {
backtrack(chars, start + position, &dots)
}
dots.removeLast()
position += 1
}
}
private func valid(_ chars: [Character], _ start: Int, _ count: Int) -> Bool {
return count == 1 ||
(chars[start] != "0" && (count < 3 || String(chars[start..<start + count]) <= "255"))
}
}