Skip to content

Commit fe9bacc

Browse files
authoredDec 19, 2023
feat: add solutions to lc problems: No.1771,1772 (#2124)
* No.1771.Maximize Palindrome Length From Subsequences * No.1772.Sort Features by Popularity
1 parent f8d7dc8 commit fe9bacc

File tree

12 files changed

+417
-203
lines changed

12 files changed

+417
-203
lines changed
 

‎solution/1700-1799/1771.Maximize Palindrome Length From Subsequences/README.md

+59-3
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565

6666
最后我们返回答案即可。
6767

68-
时间复杂度为 $O(n^2)$,其中 $n$ 是字符串 $s$ 的长度。
68+
时间复杂度 $O(n^2)$,空间复杂度 $O(n^2)$,其中 $n$ 为字符串 $s$ 的长度。
6969

7070
<!-- tabs:start -->
7171

@@ -82,11 +82,11 @@ class Solution:
8282
for i in range(n):
8383
f[i][i] = 1
8484
ans = 0
85-
for i in range(n - 1, -1, -1):
85+
for i in range(n - 2, -1, -1):
8686
for j in range(i + 1, n):
8787
if s[i] == s[j]:
8888
f[i][j] = f[i + 1][j - 1] + 2
89-
if i < len(word1) and j >= len(word1):
89+
if i < len(word1) <= j:
9090
ans = max(ans, f[i][j])
9191
else:
9292
f[i][j] = max(f[i + 1][j], f[i][j - 1])
@@ -180,6 +180,62 @@ func longestPalindrome(word1 string, word2 string) (ans int) {
180180
}
181181
```
182182

183+
### **TypeScript**
184+
185+
```ts
186+
function longestPalindrome(word1: string, word2: string): number {
187+
const s = word1 + word2;
188+
const n = s.length;
189+
const f: number[][] = Array.from({ length: n }, () => Array.from({ length: n }, () => 0));
190+
for (let i = 0; i < n; ++i) {
191+
f[i][i] = 1;
192+
}
193+
let ans = 0;
194+
for (let i = n - 2; ~i; --i) {
195+
for (let j = i + 1; j < n; ++j) {
196+
if (s[i] === s[j]) {
197+
f[i][j] = f[i + 1][j - 1] + 2;
198+
if (i < word1.length && j >= word1.length) {
199+
ans = Math.max(ans, f[i][j]);
200+
}
201+
} else {
202+
f[i][j] = Math.max(f[i + 1][j], f[i][j - 1]);
203+
}
204+
}
205+
}
206+
return ans;
207+
}
208+
```
209+
210+
### **Rust**
211+
212+
```rust
213+
impl Solution {
214+
pub fn longest_palindrome(word1: String, word2: String) -> i32 {
215+
let s: Vec<char> = format!("{}{}", word1, word2).chars().collect();
216+
let n = s.len();
217+
let mut f = vec![vec![0; n]; n];
218+
for i in 0..n {
219+
f[i][i] = 1;
220+
}
221+
let mut ans = 0;
222+
for i in (0..n - 1).rev() {
223+
for j in i + 1..n {
224+
if s[i] == s[j] {
225+
f[i][j] = f[i + 1][j - 1] + 2;
226+
if i < word1.len() && j >= word1.len() {
227+
ans = ans.max(f[i][j]);
228+
}
229+
} else {
230+
f[i][j] = f[i + 1][j].max(f[i][j - 1]);
231+
}
232+
}
233+
}
234+
ans
235+
}
236+
}
237+
```
238+
183239
### **...**
184240

185241
```

‎solution/1700-1799/1771.Maximize Palindrome Length From Subsequences/README_EN.md

+72-2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@
5050

5151
## Solutions
5252

53+
**Solution 1: Dynamic Programming**
54+
55+
First, we concatenate strings `word1` and `word2` to get string $s$. Then we can transform the problem into finding the length of the longest palindromic subsequence in string $s$. However, when calculating the final answer, we need to ensure that at least one character in the palindrome string comes from `word1` and another character comes from `word2`.
56+
57+
We define $f[i][j]$ as the length of the longest palindromic subsequence in the substring of string $s$ with index range $[i, j]$.
58+
59+
If $s[i] = s[j]$, then $s[i]$ and $s[j]$ must be in the longest palindromic subsequence, at this time $f[i][j] = f[i + 1][j - 1] + 2$. At this point, we also need to judge whether $s[i]$ and $s[j]$ come from `word1` and `word2`. If so, we update the maximum value of the answer to $ans=\max(ans, f[i][j])$.
60+
61+
If $s[i] \neq s[j]$, then $s[i]$ and $s[j]$ will definitely not appear in the longest palindromic subsequence at the same time, at this time $f[i][j] = max(f[i + 1][j], f[i][j - 1])$.
62+
63+
Finally, we return the answer.
64+
65+
The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$. Here, $n$ is the length of string $s$.
66+
5367
<!-- tabs:start -->
5468

5569
### **Python3**
@@ -63,11 +77,11 @@ class Solution:
6377
for i in range(n):
6478
f[i][i] = 1
6579
ans = 0
66-
for i in range(n - 1, -1, -1):
80+
for i in range(n - 2, -1, -1):
6781
for j in range(i + 1, n):
6882
if s[i] == s[j]:
6983
f[i][j] = f[i + 1][j - 1] + 2
70-
if i < len(word1) and j >= len(word1):
84+
if i < len(word1) <= j:
7185
ans = max(ans, f[i][j])
7286
else:
7387
f[i][j] = max(f[i + 1][j], f[i][j - 1])
@@ -159,6 +173,62 @@ func longestPalindrome(word1 string, word2 string) (ans int) {
159173
}
160174
```
161175

176+
### **TypeScript**
177+
178+
```ts
179+
function longestPalindrome(word1: string, word2: string): number {
180+
const s = word1 + word2;
181+
const n = s.length;
182+
const f: number[][] = Array.from({ length: n }, () => Array.from({ length: n }, () => 0));
183+
for (let i = 0; i < n; ++i) {
184+
f[i][i] = 1;
185+
}
186+
let ans = 0;
187+
for (let i = n - 2; ~i; --i) {
188+
for (let j = i + 1; j < n; ++j) {
189+
if (s[i] === s[j]) {
190+
f[i][j] = f[i + 1][j - 1] + 2;
191+
if (i < word1.length && j >= word1.length) {
192+
ans = Math.max(ans, f[i][j]);
193+
}
194+
} else {
195+
f[i][j] = Math.max(f[i + 1][j], f[i][j - 1]);
196+
}
197+
}
198+
}
199+
return ans;
200+
}
201+
```
202+
203+
### **Rust**
204+
205+
```rust
206+
impl Solution {
207+
pub fn longest_palindrome(word1: String, word2: String) -> i32 {
208+
let s: Vec<char> = format!("{}{}", word1, word2).chars().collect();
209+
let n = s.len();
210+
let mut f = vec![vec![0; n]; n];
211+
for i in 0..n {
212+
f[i][i] = 1;
213+
}
214+
let mut ans = 0;
215+
for i in (0..n - 1).rev() {
216+
for j in i + 1..n {
217+
if s[i] == s[j] {
218+
f[i][j] = f[i + 1][j - 1] + 2;
219+
if i < word1.len() && j >= word1.len() {
220+
ans = ans.max(f[i][j]);
221+
}
222+
} else {
223+
f[i][j] = f[i + 1][j].max(f[i][j - 1]);
224+
}
225+
}
226+
}
227+
ans
228+
}
229+
}
230+
```
231+
162232
### **...**
163233

164234
```
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
class Solution:
2-
def longestPalindrome(self, word1: str, word2: str) -> int:
3-
s = word1 + word2
4-
n = len(s)
5-
f = [[0] * n for _ in range(n)]
6-
for i in range(n):
7-
f[i][i] = 1
8-
ans = 0
9-
for i in range(n - 1, -1, -1):
10-
for j in range(i + 1, n):
11-
if s[i] == s[j]:
12-
f[i][j] = f[i + 1][j - 1] + 2
13-
if i < len(word1) and j >= len(word1):
14-
ans = max(ans, f[i][j])
15-
else:
16-
f[i][j] = max(f[i + 1][j], f[i][j - 1])
17-
return ans
1+
class Solution:
2+
def longestPalindrome(self, word1: str, word2: str) -> int:
3+
s = word1 + word2
4+
n = len(s)
5+
f = [[0] * n for _ in range(n)]
6+
for i in range(n):
7+
f[i][i] = 1
8+
ans = 0
9+
for i in range(n - 2, -1, -1):
10+
for j in range(i + 1, n):
11+
if s[i] == s[j]:
12+
f[i][j] = f[i + 1][j - 1] + 2
13+
if i < len(word1) <= j:
14+
ans = max(ans, f[i][j])
15+
else:
16+
f[i][j] = max(f[i + 1][j], f[i][j - 1])
17+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
impl Solution {
2+
pub fn longest_palindrome(word1: String, word2: String) -> i32 {
3+
let s: Vec<char> = format!("{}{}", word1, word2).chars().collect();
4+
let n = s.len();
5+
let mut f = vec![vec![0; n]; n];
6+
for i in 0..n {
7+
f[i][i] = 1;
8+
}
9+
let mut ans = 0;
10+
for i in (0..n - 1).rev() {
11+
for j in i + 1..n {
12+
if s[i] == s[j] {
13+
f[i][j] = f[i + 1][j - 1] + 2;
14+
if i < word1.len() && j >= word1.len() {
15+
ans = ans.max(f[i][j]);
16+
}
17+
} else {
18+
f[i][j] = f[i + 1][j].max(f[i][j - 1]);
19+
}
20+
}
21+
}
22+
ans
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function longestPalindrome(word1: string, word2: string): number {
2+
const s = word1 + word2;
3+
const n = s.length;
4+
const f: number[][] = Array.from({ length: n }, () => Array.from({ length: n }, () => 0));
5+
for (let i = 0; i < n; ++i) {
6+
f[i][i] = 1;
7+
}
8+
let ans = 0;
9+
for (let i = n - 2; ~i; --i) {
10+
for (let j = i + 1; j < n; ++j) {
11+
if (s[i] === s[j]) {
12+
f[i][j] = f[i + 1][j - 1] + 2;
13+
if (i < word1.length && j >= word1.length) {
14+
ans = Math.max(ans, f[i][j]);
15+
}
16+
} else {
17+
f[i][j] = Math.max(f[i + 1][j], f[i][j - 1]);
18+
}
19+
}
20+
}
21+
return ans;
22+
}

0 commit comments

Comments
 (0)