Skip to content

Commit

Permalink
Add longest common substring (TheAlgorithms#410)
Browse files Browse the repository at this point in the history
  • Loading branch information
RohitSingh107 authored Oct 16, 2022
1 parent 567d6cb commit 5e0824b
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
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

0 comments on commit 5e0824b

Please sign in to comment.