-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
150 lines (135 loc) · 3.26 KB
/
main.go
File metadata and controls
150 lines (135 loc) · 3.26 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Source: https://leetcode.com/problems/shortest-common-supersequence
// Title: Shortest Common Supersequence
// Difficulty: Hard
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Given two strings `str1` and `str2`, return the shortest string that has both `str1` and `str2` as **subsequences**. If there are multiple valid strings, return **any** of them.
//
// A string `s` is a **subsequence** of string `t` if deleting some number of characters from `t` (possibly `0`) results in the string `s`.
//
// **Example 1:**
//
// ```
// Input: str1 = "abac", str2 = "cab"
// Output: "cabac"
// Explanation:
// str1 = "abac" is a subsequence of "cabac" because we can delete the first "c".
// str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac".
// The answer provided is the shortest such string that satisfies these properties.
// ```
//
// **Example 2:**
//
// ```
// Input: str1 = "aaaaaaaa", str2 = "aaaaaaaa"
// Output: "aaaaaaaa"
// ```
//
// **Constraints:**
//
// - `1 <= str1.length, str2.length <= 1000`
// - `str1` and `str2` consist of lowercase English letters.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
import (
"strings"
)
// 2D Needleman-Wunsch Algorithm
func shortestCommonSupersequence(str1 string, str2 string) string {
m, n := len(str1), len(str2)
mat := make([][]int, m+1)
for i := range m + 1 {
mat[i] = make([]int, n+1)
}
dir := make([][]byte, m+1)
for i := range m + 1 {
dir[i] = make([]byte, n+1)
}
for i := range m {
dir[i+1][0] = 'u'
}
for j := range n {
dir[0][j+1] = 'l'
}
// DP
for i, c1 := range str1 {
for j, c2 := range str2 {
if c1 == c2 {
mat[i+1][j+1] = mat[i][j] + 1
dir[i+1][j+1] = 'd'
} else {
if mat[i+1][j] >= mat[i][j+1] {
mat[i+1][j+1] = mat[i+1][j]
dir[i+1][j+1] = 'l'
} else {
mat[i+1][j+1] = mat[i][j+1]
dir[i+1][j+1] = 'u'
}
}
}
}
i, j := m, n
mn := m + n - mat[m][n]
res := make([]byte, mn)
for k := mn - 1; k >= 0; k-- {
switch dir[i][j] {
case 'd':
res[k] = str1[i-1]
i--
j--
case 'l':
res[k] = str2[j-1]
j--
case 'u':
res[k] = str1[i-1]
i--
}
}
return string(res)
}
// 2D Needleman-Wunsch Algorithm
func shortestCommonSupersequence2(str1 string, str2 string) string {
m, n := len(str1), len(str2)
// Make sure str1 is the longest
if m < n {
return shortestCommonSupersequence2(str2, str1)
}
dp := make([][]int, m+1)
for i := range m + 1 {
dp[i] = make([]int, n+1)
}
// DP
for i := m - 1; i >= 0; i-- {
for j := n - 1; j >= 0; j-- {
if str1[i] == str2[j] {
dp[i][j] = dp[i+1][j+1] + 1
} else {
dp[i][j] = max(dp[i+1][j], dp[i][j+1])
}
}
}
var res strings.Builder
i, j := 0, 0
for i < m && j < n {
if str1[i] == str2[j] {
// same char
res.WriteByte(str1[i])
i++
j++
} else if dp[i+1][j] >= dp[i][j+1] {
// pick str1
// when equal, pick str1 since it's longer
res.WriteByte(str1[i])
i++
} else {
// pick str2
res.WriteByte(str2[j])
j++
}
}
// Copy remainings
res.WriteString(str1[i:])
res.WriteString(str2[j:])
return res.String()
}