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 59958ab

Browse files
committed
Add longest_common_subsequence
1 parent ba21fe7 commit 59958ab

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

‎README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ LeetCode is a website that has programming-related questions that are designed t
3232
| 452 | Minimum Number of Arrows to Burst Balloons | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/find_min_arrow_shots.rs) | [Leetcode](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/) | Medium |
3333
| 485 | Max Consecutive Ones | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/find_max_consecutive_ones.rs) | [Leetcode](https://leetcode.com/problems/max-consecutive-ones/) | Easy |
3434
| 495 | Teemo Attacking | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/find_poisoned_duration.rs) | [Leetcode](https://leetcode.com/problems/teemo-attacking/) | Easy |
35+
| 698 | Partition to K Equal Sum Subsets | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/can_partition_k_subsets.rs) | [Leetcode](https://leetcode.com/problems/partition-to-k-equal-sum-subsets/) | Medium |
3536
| 713 | Subarray Product Less Than K | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/num_subarray_product_less_than_k.rs) | [Leetcode](https://leetcode.com/problems/subarray-product-less-than-k/) | Medium |
3637
| 725 | Split Linked List in Parts | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/split_list_to_parts.rs) | [Leetcode](https://leetcode.com/problems/split-linked-list-in-parts/) | Medium |
3738
| 774 | Minimize Max Distance to Gas Station | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/minmax_gas_dist.rs) | [Leetcode](https://leetcode.com/problems/minimize-max-distance-to-gas-station/) | Medium |
@@ -41,6 +42,7 @@ LeetCode is a website that has programming-related questions that are designed t
4142
| 1035 | Uncrossed Lines | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/max_uncrossed_lines.rs) | [Leetcode](https://leetcode.com/problems/uncrossed-lines/) | Medium |
4243
| 1134 | Armstrong Number | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/is_good_array.rs) | [Leetcode](https://leetcode.com/problems/armstrong-number/) | Easy |
4344
| 1137 | N-th Tribonacci Number | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/tribonacci.rs) | [Leetcode](https://leetcode.com/problems/n-th-tribonacci-number/) | Easy |
45+
| 1143 | Longest Common Subsequence | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/longest_common_subsequence.rs) | [Leetcode](https://leetcode.com/problems/longest-common-subsequence/) | Medium |
4446
| 1239 | Maximum Length of a Concatenated String with Unique Characters | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/max_length.rs) | [Leetcode](https://leetcode.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/) | Medium |
4547
| 1275 | Find Winner on a Tic Tac Toe Game | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/tictactoe.rs) | [Leetcode](https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/) | Easy |
4648
| 1293 | Shortest Path in a Grid with Obstacles Elimination | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/shortest_path.rs) | [Leetcode](https://leetcode.com/problems/shortest-path-in-a-grid-with-obstacles-elimination/) | Hard |
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// 1143. Longest Common Subsequence, Medium
2+
// https://leetcode.com/problems/longest-common-subsequence/
3+
impl Solution {
4+
pub fn longest_common_subsequence(text1: String, text2: String) -> i32 {
5+
let [n, m] = [text1.len(), text2.len()];
6+
let mut dp = vec![vec![0; n + 1]; m + 1];
7+
8+
for i in 0..n {
9+
for j in 0..m {
10+
if text1.chars().nth(i).unwrap() == text2.chars().nth(j).unwrap() {
11+
dp[j + 1][i + 1] = dp[j][i] + 1;
12+
} else {
13+
dp[j + 1][i + 1] = dp[j + 1][i].max(dp[j][i + 1]);
14+
}
15+
}
16+
}
17+
18+
*dp.last().unwrap().last().unwrap()
19+
}
20+
}
21+
22+
struct Solution {}
23+
24+
#[cfg(test)]
25+
mod tests {
26+
use super::*;
27+
28+
#[test]
29+
fn test_longest_common_subsequence() {
30+
assert_eq!(Solution::longest_common_subsequence("abcde".to_string(), "ace".to_string()), 3);
31+
}
32+
33+
#[test]
34+
fn test_longest_common_subsequence2() {
35+
assert_eq!(Solution::longest_common_subsequence("abc".to_string(), "abc".to_string()), 3);
36+
}
37+
38+
#[test]
39+
fn test_longest_common_subsequence3() {
40+
assert_eq!(Solution::longest_common_subsequence("abc".to_string(), "def".to_string()), 0);
41+
}
42+
43+
#[test]
44+
fn test_longest_common_subsequence4() {
45+
assert_eq!(Solution::longest_common_subsequence("abc".to_string(), "".to_string()), 0);
46+
}
47+
}

‎src/leetcode/challenge/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mod break_palindrome;
22
mod can_partition_k_subsets;
33
mod find_max_consecutive_ones;
44
mod intersect;
5+
mod longest_common_subsequence;
56
mod max_length;
67
mod max_sub_array_len;
78
mod minmax_gas_dist;

0 commit comments

Comments
(0)

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