Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5166971

Browse files
committed
longest com subseq
1 parent cc30094 commit 5166971

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

‎src/longest_common_subseq.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub struct Solution {}
2+
impl Solution {
3+
pub fn longest_common_subsequence(text1: String, text2: String) -> i32 {
4+
let mut dp = vec![vec![0; text2.len() + 1]; text1.len() + 1];
5+
let t1 = text1.as_bytes();
6+
let t2 = text2.as_bytes();
7+
for i in (0..text1.len()).rev() {
8+
for j in (0..text2.len()).rev() {
9+
if t1[i] == t2[j] {
10+
dp[i][j] = 1 + dp[i + 1][j + 1];
11+
} else {
12+
dp[i][j] = std::cmp::max(dp[i][j + 1], dp[i + 1][j]);
13+
}
14+
}
15+
}
16+
dp[0][0]
17+
}
18+
}

‎src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
mod unique_paths;
1+
mod longest_common_subseq;
22
fn main() {}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /