Skip to content

feat: add solutions to lc problem: No.1771 #2124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@

最后我们返回答案即可。

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

<!-- tabs:start -->

Expand All @@ -82,11 +82,11 @@ class Solution:
for i in range(n):
f[i][i] = 1
ans = 0
for i in range(n - 1, -1, -1):
for i in range(n - 2, -1, -1):
for j in range(i + 1, n):
if s[i] == s[j]:
f[i][j] = f[i + 1][j - 1] + 2
if i < len(word1) and j >= len(word1):
if i < len(word1) <= j:
ans = max(ans, f[i][j])
else:
f[i][j] = max(f[i + 1][j], f[i][j - 1])
Expand Down Expand Up @@ -180,6 +180,62 @@ func longestPalindrome(word1 string, word2 string) (ans int) {
}
```

### **TypeScript**

```ts
function longestPalindrome(word1: string, word2: string): number {
const s = word1 + word2;
const n = s.length;
const f: number[][] = Array.from({ length: n }, () => Array.from({ length: n }, () => 0));
for (let i = 0; i < n; ++i) {
f[i][i] = 1;
}
let ans = 0;
for (let i = n - 2; ~i; --i) {
for (let j = i + 1; j < n; ++j) {
if (s[i] === s[j]) {
f[i][j] = f[i + 1][j - 1] + 2;
if (i < word1.length && j >= word1.length) {
ans = Math.max(ans, f[i][j]);
}
} else {
f[i][j] = Math.max(f[i + 1][j], f[i][j - 1]);
}
}
}
return ans;
}
```

### **Rust**

```rust
impl Solution {
pub fn longest_palindrome(word1: String, word2: String) -> i32 {
let s: Vec<char> = format!("{}{}", word1, word2).chars().collect();
let n = s.len();
let mut f = vec![vec![0; n]; n];
for i in 0..n {
f[i][i] = 1;
}
let mut ans = 0;
for i in (0..n - 1).rev() {
for j in i + 1..n {
if s[i] == s[j] {
f[i][j] = f[i + 1][j - 1] + 2;
if i < word1.len() && j >= word1.len() {
ans = ans.max(f[i][j]);
}
} else {
f[i][j] = f[i + 1][j].max(f[i][j - 1]);
}
}
}
ans
}
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@

## Solutions

**Solution 1: Dynamic Programming**

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`.

We define $f[i][j]$ as the length of the longest palindromic subsequence in the substring of string $s$ with index range $[i, j]$.

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])$.

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])$.

Finally, we return the answer.

The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$. Here, $n$ is the length of string $s$.

<!-- tabs:start -->

### **Python3**
Expand All @@ -63,11 +77,11 @@ class Solution:
for i in range(n):
f[i][i] = 1
ans = 0
for i in range(n - 1, -1, -1):
for i in range(n - 2, -1, -1):
for j in range(i + 1, n):
if s[i] == s[j]:
f[i][j] = f[i + 1][j - 1] + 2
if i < len(word1) and j >= len(word1):
if i < len(word1) <= j:
ans = max(ans, f[i][j])
else:
f[i][j] = max(f[i + 1][j], f[i][j - 1])
Expand Down Expand Up @@ -159,6 +173,62 @@ func longestPalindrome(word1 string, word2 string) (ans int) {
}
```

### **TypeScript**

```ts
function longestPalindrome(word1: string, word2: string): number {
const s = word1 + word2;
const n = s.length;
const f: number[][] = Array.from({ length: n }, () => Array.from({ length: n }, () => 0));
for (let i = 0; i < n; ++i) {
f[i][i] = 1;
}
let ans = 0;
for (let i = n - 2; ~i; --i) {
for (let j = i + 1; j < n; ++j) {
if (s[i] === s[j]) {
f[i][j] = f[i + 1][j - 1] + 2;
if (i < word1.length && j >= word1.length) {
ans = Math.max(ans, f[i][j]);
}
} else {
f[i][j] = Math.max(f[i + 1][j], f[i][j - 1]);
}
}
}
return ans;
}
```

### **Rust**

```rust
impl Solution {
pub fn longest_palindrome(word1: String, word2: String) -> i32 {
let s: Vec<char> = format!("{}{}", word1, word2).chars().collect();
let n = s.len();
let mut f = vec![vec![0; n]; n];
for i in 0..n {
f[i][i] = 1;
}
let mut ans = 0;
for i in (0..n - 1).rev() {
for j in i + 1..n {
if s[i] == s[j] {
f[i][j] = f[i + 1][j - 1] + 2;
if i < word1.len() && j >= word1.len() {
ans = ans.max(f[i][j]);
}
} else {
f[i][j] = f[i + 1][j].max(f[i][j - 1]);
}
}
}
ans
}
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
class Solution:
def longestPalindrome(self, word1: str, word2: str) -> int:
s = word1 + word2
n = len(s)
f = [[0] * n for _ in range(n)]
for i in range(n):
f[i][i] = 1
ans = 0
for i in range(n - 1, -1, -1):
for j in range(i + 1, n):
if s[i] == s[j]:
f[i][j] = f[i + 1][j - 1] + 2
if i < len(word1) and j >= len(word1):
ans = max(ans, f[i][j])
else:
f[i][j] = max(f[i + 1][j], f[i][j - 1])
return ans
class Solution:
def longestPalindrome(self, word1: str, word2: str) -> int:
s = word1 + word2
n = len(s)
f = [[0] * n for _ in range(n)]
for i in range(n):
f[i][i] = 1
ans = 0
for i in range(n - 2, -1, -1):
for j in range(i + 1, n):
if s[i] == s[j]:
f[i][j] = f[i + 1][j - 1] + 2
if i < len(word1) <= j:
ans = max(ans, f[i][j])
else:
f[i][j] = max(f[i + 1][j], f[i][j - 1])
return ans
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
impl Solution {
pub fn longest_palindrome(word1: String, word2: String) -> i32 {
let s: Vec<char> = format!("{}{}", word1, word2).chars().collect();
let n = s.len();
let mut f = vec![vec![0; n]; n];
for i in 0..n {
f[i][i] = 1;
}
let mut ans = 0;
for i in (0..n - 1).rev() {
for j in i + 1..n {
if s[i] == s[j] {
f[i][j] = f[i + 1][j - 1] + 2;
if i < word1.len() && j >= word1.len() {
ans = ans.max(f[i][j]);
}
} else {
f[i][j] = f[i + 1][j].max(f[i][j - 1]);
}
}
}
ans
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function longestPalindrome(word1: string, word2: string): number {
const s = word1 + word2;
const n = s.length;
const f: number[][] = Array.from({ length: n }, () => Array.from({ length: n }, () => 0));
for (let i = 0; i < n; ++i) {
f[i][i] = 1;
}
let ans = 0;
for (let i = n - 2; ~i; --i) {
for (let j = i + 1; j < n; ++j) {
if (s[i] === s[j]) {
f[i][j] = f[i + 1][j - 1] + 2;
if (i < word1.length && j >= word1.length) {
ans = Math.max(ans, f[i][j]);
}
} else {
f[i][j] = Math.max(f[i + 1][j], f[i][j - 1]);
}
}
}
return ans;
}
Loading