Skip to content

Commit 0b92528

Browse files
committed
Polqt
Signed-off-by: plutov <a.pliutau@gmail.com>
1 parent 91f6c75 commit 0b92528

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
- [x] ([@nguyengiabk](https://github.com/nguyengiabk), [@denpeshkov](https://github.com/denpeshkov)) [mergesort](https://github.com/plutov/practice-go/tree/master/mergesort)
2020
- [x] ([@nguyengiabk](https://github.com/nguyengiabk)) [wordladder](https://github.com/plutov/practice-go/tree/master/wordladder)
2121
- [x] ([@EvenPeng](https://github.com/EvenPeng)) [sumdecimal](https://github.com/plutov/practice-go/tree/master/sumdecimal)
22-
- [x] ([@bediger4000](https://github.com/bediger4000)) [buildword](https://github.com/plutov/practice-go/tree/master/buildword)
22+
- [x] ([@bediger4000](https://github.com/bediger4000), [Polqt](https://github.com/Polqt)) [buildword](https://github.com/plutov/practice-go/tree/master/buildword)
2323
- [x] ([@zerkms](https://github.com/zerkms)) [shorthash](https://github.com/plutov/practice-go/tree/master/shorthash)
2424
- [x] ([@zerkms](https://github.com/zerkms)) [romannumerals](https://github.com/plutov/practice-go/tree/master/romannumerals)
2525
- [x] ([@zerkms](https://github.com/zerkms)) [lastlettergame](https://github.com/plutov/practice-go/tree/master/lastlettergame)

buildword/buildword_dp.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package buildword
2+
3+
func BuildWordDP(word string, fragments []string) int {
4+
n := len(word) // Get the length of the word
5+
if n == 0 { // If the word is empty, then we can return 0
6+
return 0
7+
}
8+
9+
// I created the dp array to store the minimum number of fragments needed to build the prefix word
10+
dp := make([]int, n+1)
11+
12+
// Initialize the dp array with a large value (n + 1) to represent infinity
13+
for i := 1; i <= n; i++ {
14+
dp[i] = n + 1 // Set the initial value to n + 1 (infinity)
15+
}
16+
17+
// For the base case, we can build an empty string with 0 fragments
18+
for i := 1; i <= n; i++ {
19+
for _, fragment := range fragments { // Iterate through each fragment
20+
fragLen := len(fragment)
21+
22+
if i >= fragLen { // Does the fragment fit in the current position?
23+
prevIndex := i - fragLen // Calculate the previous index
24+
25+
if dp[prevIndex] <= n && word[prevIndex:i] == fragment { // Check if the fragment matches the substring of the word
26+
if dp[prevIndex]+1 < dp[i] { // If we can build the word with fewer fragments, update dp[i]
27+
dp[i] = dp[prevIndex] + 1
28+
}
29+
}
30+
}
31+
}
32+
}
33+
34+
if dp[n] > n { // If dp[n] is still n + 1, it means we couldn't build the word with the fragments
35+
return 0
36+
}
37+
return dp[n] // Return the minimum number of fragments needed to build the word
38+
}

buildword/buildword_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,27 @@ func TestBuildWord(t *testing.T) {
2424
}
2525
}
2626

27+
func TestBuildWordDP(t *testing.T) {
28+
for _, test := range tests {
29+
actual := BuildWordDP(test.word, test.fragments)
30+
if actual != test.expected {
31+
t.Errorf("BuildWordDP(%s, %v) expected %d, got %d", test.word, test.fragments, test.expected, actual)
32+
}
33+
}
34+
}
35+
2736
func BenchmarkBuildWord(b *testing.B) {
2837
for i := 0; i < b.N; i++ {
2938
for _, test := range tests {
3039
BuildWord(test.word, test.fragments)
3140
}
3241
}
3342
}
43+
44+
func BenchmarkBuildWordDP(b *testing.B) {
45+
for i := 0; i < b.N; i++ {
46+
for _, test := range tests {
47+
BuildWordDP(test.word, test.fragments)
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)