Skip to content

added longest common substring #410

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
Oct 16, 2022
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
106 changes: 106 additions & 0 deletions src/dynamic_programming/longest_common_substring.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Longest common substring via Dynamic Programming
// longest_common_substring(a, b) returns the length of longest common substring between the strings a and b.
pub fn longest_common_substring(text1: String, text2: String) -> i32 {
let m = text1.len();
let n = text2.len();

let t1 = text1.as_bytes();
let t2 = text2.as_bytes();

// BottomUp Tabulation
let mut dp = vec![vec![0; n + 1]; m + 1];
let mut ans = 0;
for i in 1..=m {
for j in 1..=n {
if i == 0 || j == 0 {
dp[i][j] = 0;
continue;
}
if t1[i - 1] == t2[j - 1] {
dp[i][j] = 1 + dp[i - 1][j - 1];
ans = std::cmp::max(ans, dp[i][j]);
}
}
}

ans
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test1() {
assert_eq!(
longest_common_substring(String::from(""), String::from("")),
0
);
}
#[test]
fn test2() {
assert_eq!(
longest_common_substring(String::from("a"), String::from("")),
0
);
}
#[test]
fn test3() {
assert_eq!(
longest_common_substring(String::from(""), String::from("a")),
0
);
}
#[test]
fn test4() {
assert_eq!(
longest_common_substring(String::from("a"), String::from("a")),
1
);
}
#[test]
fn test5() {
assert_eq!(
longest_common_substring(String::from("abcdef"), String::from("bcd")),
3
);
}
#[test]
fn test6() {
assert_eq!(
longest_common_substring(String::from("abcdef"), String::from("xabded")),
2
);
}
#[test]
fn test7() {
assert_eq!(
longest_common_substring(String::from("GeeksforGeeks"), String::from("GeeksQuiz")),
5
);
}
#[test]
fn test8() {
assert_eq!(
longest_common_substring(String::from("abcdxyz"), String::from("xyzabcd")),
4
);
}
#[test]
fn test9() {
assert_eq!(
longest_common_substring(String::from("zxabcdezy"), String::from("yzabcdezx")),
6
);
}
#[test]
fn test10() {
assert_eq!(
longest_common_substring(
String::from("OldSite:GeeksforGeeks.org"),
String::from("NewSite:GeeksQuiz.com")
),
10
);
}
}
2 changes: 2 additions & 0 deletions src/dynamic_programming/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod fibonacci;
mod is_subsequence;
mod knapsack;
mod longest_common_subsequence;
mod longest_common_substring;
mod longest_continuous_increasing_subsequence;
mod longest_increasing_subsequence;
mod maximal_square;
Expand All @@ -23,6 +24,7 @@ pub use self::fibonacci::recursive_fibonacci;
pub use self::is_subsequence::is_subsequence;
pub use self::knapsack::knapsack;
pub use self::longest_common_subsequence::longest_common_subsequence;
pub use self::longest_common_substring::longest_common_substring;
pub use self::longest_continuous_increasing_subsequence::longest_continuous_increasing_subsequence;
pub use self::longest_increasing_subsequence::longest_increasing_subsequence;
pub use self::maximal_square::maximal_square;
Expand Down