-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCCI1713-ReSpace.go
More file actions
69 lines (60 loc) · 2.69 KB
/
LCCI1713-ReSpace.go
File metadata and controls
69 lines (60 loc) · 2.69 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
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
package main
// 面试题 17.13. Re-Space LCCI
// Oh, no! You have accidentally removed all spaces, punctuation, and capitalization in a lengthy document.
// A sentence like "I reset the computer. It still didn't boot!" became "iresetthecomputeritstilldidntboot".
// You'll deal with the punctuation and capitalization later; right now you need to re-insert the spaces.
// Most of the words are in a dictionary but a few are not.
// Given a dictionary (a list of strings) and the document (a string), design an algorithm to unconcatenate the document in a way that minimizes the number of unrecognized characters.
// Return the number of unrecognized characters.
// Note: This problem is slightly different from the original one in the book.
// Example:
// Input:
// dictionary = ["looked","just","like","her","brother"]
// sentence = "jesslookedjustliketimherbrother"
// Output: 7
// Explanation: After unconcatenating, we got "jess looked just like tim her brother", which containing 7 unrecognized characters.
// Note:
// 0 <= len(sentence) <= 1000
// The total number of characters in dictionary is less than or equal to 150000.
// There are only lowercase letters in dictionary and sentence.
import "fmt"
func respace(dictionary []string, sentence string) int {
n := len(sentence)
dp := make([]int, n + 1)
for i := 1; i < n + 1; i++ {
dp[i] = 1 << 31
}
min := func (x, y int) int { if x < y { return x; }; return y; }
for i := 0; i < n; i++ {
for _, v := range dictionary {
if i - len(v) + 1 >= 0 && sentence[i - len(v) + 1:i + 1] == v {
dp[i + 1] = min(dp[i + 1], dp[i - len(v) + 1])
}
}
dp[i + 1] = min(dp[i + 1], dp[i] + 1)
}
return dp[n]
}
func respace1(dictionary []string, sentence string) int {
n := len(sentence)
dp := make([]int, n+1)
min := func (x, y int) int { if x < y { return x; }; return y; }
for i := 1; i <= n; i++ {
dp[i] = dp[i - 1] +1
for _, v := range dictionary {
if i < len(v) || sentence[i - len(v):i] != v { continue }
dp[i] = min(dp[i], dp[i - len(v)])
}
}
return dp[n]
}
func main() {
// Example:
// Input:
// dictionary = ["looked","just","like","her","brother"]
// sentence = "jesslookedjustliketimherbrother"
// Output: 7
// Explanation: After unconcatenating, we got "jess looked just like tim her brother", which containing 7 unrecognized characters.
fmt.Println(respace([]string{"looked","just","like","her","brother"}, "jesslookedjustliketimherbrother")) // 7
fmt.Println(respace1([]string{"looked","just","like","her","brother"}, "jesslookedjustliketimherbrother")) // 7
}