@@ -96,25 +96,222 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3503.Lo
96
96
#### Python3
97
97
98
98
``` python
99
-
99
+ class Solution :
100
+ def longestPalindrome (self , s : str , t : str ) -> int :
101
+ def expand (s : str , g : List[int ], l : int , r : int ):
102
+ while l >= 0 and r < len (s) and s[l] == s[r]:
103
+ g[l] = max (g[l], r - l + 1 )
104
+ l, r = l - 1 , r + 1
105
+
106
+ def calc (s : str ) -> List[int ]:
107
+ n = len (s)
108
+ g = [0 ] * n
109
+ for i in range (n):
110
+ expand(s, g, i, i)
111
+ expand(s, g, i, i + 1 )
112
+ return g
113
+
114
+ m, n = len (s), len (t)
115
+ t = t[::- 1 ]
116
+ g1, g2 = calc(s), calc(t)
117
+ ans = max (* g1, * g2)
118
+ f = [[0 ] * (n + 1 ) for _ in range (m + 1 )]
119
+ for i, a in enumerate (s, 1 ):
120
+ for j, b in enumerate (t, 1 ):
121
+ if a == b:
122
+ f[i][j] = f[i - 1 ][j - 1 ] + 1
123
+ ans = max (ans, f[i][j] * 2 + (0 if i >= m else g1[i]))
124
+ ans = max (ans, f[i][j] * 2 + (0 if j >= n else g2[j]))
125
+ return ans
100
126
```
101
127
102
128
#### Java
103
129
104
130
``` java
105
-
131
+ class Solution {
132
+ public int longestPalindrome (String S , String T ) {
133
+ char [] s = S . toCharArray();
134
+ char [] t = new StringBuilder (T ). reverse(). toString(). toCharArray();
135
+ int m = s. length, n = t. length;
136
+ int [] g1 = calc(s), g2 = calc(t);
137
+ int ans = Math . max(Arrays . stream(g1). max(). getAsInt(), Arrays . stream(g2). max(). getAsInt());
138
+ int [][] f = new int [m + 1 ][n + 1 ];
139
+ for (int i = 1 ; i <= m; ++ i) {
140
+ for (int j = 1 ; j <= n; ++ j) {
141
+ if (s[i - 1 ] == t[j - 1 ]) {
142
+ f[i][j] = f[i - 1 ][j - 1 ] + 1 ;
143
+ ans = Math . max(ans, f[i][j] * 2 + (i < m ? g1[i] : 0 ));
144
+ ans = Math . max(ans, f[i][j] * 2 + (j < n ? g2[j] : 0 ));
145
+ }
146
+ }
147
+ }
148
+ return ans;
149
+ }
150
+
151
+ private void expand (char [] s , int [] g , int l , int r ) {
152
+ while (l >= 0 && r < s. length && s[l] == s[r]) {
153
+ g[l] = Math . max(g[l], r - l + 1 );
154
+ -- l;
155
+ ++ r;
156
+ }
157
+ }
158
+
159
+ private int [] calc (char [] s ) {
160
+ int n = s. length;
161
+ int [] g = new int [n];
162
+ for (int i = 0 ; i < n; ++ i) {
163
+ expand(s, g, i, i);
164
+ expand(s, g, i, i + 1 );
165
+ }
166
+ return g;
167
+ }
168
+ }
106
169
```
107
170
108
171
#### C++
109
172
110
173
``` cpp
111
-
174
+ class Solution {
175
+ public:
176
+ int longestPalindrome(string s, string t) {
177
+ int m = s.size(), n = t.size();
178
+ ranges::reverse(t);
179
+ vector<int > g1 = calc(s), g2 = calc(t);
180
+ int ans = max(ranges::max(g1), ranges::max(g2));
181
+ vector<vector<int >> f(m + 1, vector<int >(n + 1));
182
+ for (int i = 1; i <= m; ++i) {
183
+ for (int j = 1; j <= n; ++j) {
184
+ if (s[ i - 1] == t[ j - 1] ) {
185
+ f[ i] [ j ] = f[ i - 1] [ j - 1 ] + 1;
186
+ ans = max(ans, f[ i] [ j ] * 2 + (i < m ? g1[ i] : 0));
187
+ ans = max(ans, f[ i] [ j ] * 2 + (j < n ? g2[ j] : 0));
188
+ }
189
+ }
190
+ }
191
+ return ans;
192
+ }
193
+
194
+ private:
195
+ void expand(const string& s, vector<int >& g, int l, int r) {
196
+ while (l >= 0 && r < s.size() && s[ l] == s[ r] ) {
197
+ g[ l] = max(g[ l] , r - l + 1);
198
+ --l;
199
+ ++r;
200
+ }
201
+ }
202
+
203
+ vector<int> calc(const string& s) {
204
+ int n = s.size();
205
+ vector<int> g(n, 0);
206
+ for (int i = 0; i < n; ++i) {
207
+ expand(s, g, i, i);
208
+ expand(s, g, i, i + 1);
209
+ }
210
+ return g;
211
+ }
212
+ };
112
213
```
113
214
114
215
#### Go
115
216
116
217
``` go
218
+ func longestPalindrome (s , t string ) int {
219
+ m , n := len (s), len (t)
220
+ t = reverse (t)
221
+
222
+ g1 , g2 := calc (s), calc (t)
223
+ ans := max (slices.Max (g1), slices.Max (g2))
224
+
225
+ f := make ([][]int , m+1 )
226
+ for i := range f {
227
+ f[i] = make ([]int , n+1 )
228
+ }
229
+
230
+ for i := 1 ; i <= m; i++ {
231
+ for j := 1 ; j <= n; j++ {
232
+ if s[i-1 ] == t[j-1 ] {
233
+ f[i][j] = f[i-1 ][j-1 ] + 1
234
+ a , b := 0 , 0
235
+ if i < m {
236
+ a = g1[i]
237
+ }
238
+ if j < n {
239
+ b = g2[j]
240
+ }
241
+ ans = max (ans, f[i][j]*2 +a)
242
+ ans = max (ans, f[i][j]*2 +b)
243
+ }
244
+ }
245
+ }
246
+ return ans
247
+ }
248
+
249
+ func calc (s string ) []int {
250
+ n , g := len (s), make ([]int , len (s))
251
+ for i := 0 ; i < n; i++ {
252
+ expand (s, g, i, i)
253
+ expand (s, g, i, i+1 )
254
+ }
255
+ return g
256
+ }
257
+
258
+ func expand (s string , g []int , l , r int ) {
259
+ for l >= 0 && r < len (s) && s[l] == s[r] {
260
+ g[l] = max (g[l], r-l+1 )
261
+ l, r = l-1 , r+1
262
+ }
263
+ }
264
+
265
+ func reverse (s string ) string {
266
+ r := []rune (s)
267
+ slices.Reverse (r)
268
+ return string (r)
269
+ }
270
+ ```
117
271
272
+ #### TypeScript
273
+
274
+ ``` ts
275
+ function longestPalindrome(s : string , t : string ): number {
276
+ function expand(s : string , g : number [], l : number , r : number ): void {
277
+ while (l >= 0 && r < s .length && s [l ] === s [r ]) {
278
+ g [l ] = Math .max (g [l ], r - l + 1 );
279
+ l -- ;
280
+ r ++ ;
281
+ }
282
+ }
283
+
284
+ function calc(s : string ): number [] {
285
+ const n = s .length ;
286
+ const g: number [] = Array (n ).fill (0 );
287
+ for (let i = 0 ; i < n ; i ++ ) {
288
+ expand (s , g , i , i );
289
+ expand (s , g , i , i + 1 );
290
+ }
291
+ return g ;
292
+ }
293
+
294
+ const m = s .length ,
295
+ n = t .length ;
296
+ t = t .split (' ' ).reverse ().join (' ' );
297
+ const g1 = calc (s );
298
+ const g2 = calc (t );
299
+ let ans = Math .max (... g1 , ... g2 );
300
+
301
+ const f: number [][] = Array .from ({ length: m + 1 }, () => Array (n + 1 ).fill (0 ));
302
+
303
+ for (let i = 1 ; i <= m ; i ++ ) {
304
+ for (let j = 1 ; j <= n ; j ++ ) {
305
+ if (s [i - 1 ] === t [j - 1 ]) {
306
+ f [i ][j ] = f [i - 1 ][j - 1 ] + 1 ;
307
+ ans = Math .max (ans , f [i ][j ] * 2 + (i >= m ? 0 : g1 [i ]));
308
+ ans = Math .max (ans , f [i ][j ] * 2 + (j >= n ? 0 : g2 [j ]));
309
+ }
310
+ }
311
+ }
312
+
313
+ return ans ;
314
+ }
118
315
```
119
316
120
317
<!-- tabs:end -->
0 commit comments