|
1 | 1 | // Longest common substring via Dynamic Programming |
2 | 2 | // longest_common_substring(a, b) returns the length of longest common substring between the strings a and b. |
3 | | -pub fn longest_common_substring(text1: String, text2: String) -> i32 { |
| 3 | +pub fn longest_common_substring(text1: &str, text2: &str) -> i32 { |
4 | 4 | let m = text1.len(); |
5 | 5 | let n = text2.len(); |
6 | 6 |
|
@@ -32,74 +32,44 @@ mod tests { |
32 | 32 |
|
33 | 33 | #[test] |
34 | 34 | fn test1() { |
35 | | - assert_eq!( |
36 | | - longest_common_substring(String::from(""), String::from("")), |
37 | | - 0 |
38 | | - ); |
| 35 | + assert_eq!(longest_common_substring("", ""), 0); |
39 | 36 | } |
40 | 37 | #[test] |
41 | 38 | fn test2() { |
42 | | - assert_eq!( |
43 | | - longest_common_substring(String::from("a"), String::from("")), |
44 | | - 0 |
45 | | - ); |
| 39 | + assert_eq!(longest_common_substring("a", ""), 0); |
46 | 40 | } |
47 | 41 | #[test] |
48 | 42 | fn test3() { |
49 | | - assert_eq!( |
50 | | - longest_common_substring(String::from(""), String::from("a")), |
51 | | - 0 |
52 | | - ); |
| 43 | + assert_eq!(longest_common_substring("", "a"), 0); |
53 | 44 | } |
54 | 45 | #[test] |
55 | 46 | fn test4() { |
56 | | - assert_eq!( |
57 | | - longest_common_substring(String::from("a"), String::from("a")), |
58 | | - 1 |
59 | | - ); |
| 47 | + assert_eq!(longest_common_substring("a", "a"), 1); |
60 | 48 | } |
61 | 49 | #[test] |
62 | 50 | fn test5() { |
63 | | - assert_eq!( |
64 | | - longest_common_substring(String::from("abcdef"), String::from("bcd")), |
65 | | - 3 |
66 | | - ); |
| 51 | + assert_eq!(longest_common_substring("abcdef", "bcd"), 3); |
67 | 52 | } |
68 | 53 | #[test] |
69 | 54 | fn test6() { |
70 | | - assert_eq!( |
71 | | - longest_common_substring(String::from("abcdef"), String::from("xabded")), |
72 | | - 2 |
73 | | - ); |
| 55 | + assert_eq!(longest_common_substring("abcdef", "xabded"), 2); |
74 | 56 | } |
75 | 57 | #[test] |
76 | 58 | fn test7() { |
77 | | - assert_eq!( |
78 | | - longest_common_substring(String::from("GeeksforGeeks"), String::from("GeeksQuiz")), |
79 | | - 5 |
80 | | - ); |
| 59 | + assert_eq!(longest_common_substring("GeeksforGeeks", "GeeksQuiz"), 5); |
81 | 60 | } |
82 | 61 | #[test] |
83 | 62 | fn test8() { |
84 | | - assert_eq!( |
85 | | - longest_common_substring(String::from("abcdxyz"), String::from("xyzabcd")), |
86 | | - 4 |
87 | | - ); |
| 63 | + assert_eq!(longest_common_substring("abcdxyz", "xyzabcd"), 4); |
88 | 64 | } |
89 | 65 | #[test] |
90 | 66 | fn test9() { |
91 | | - assert_eq!( |
92 | | - longest_common_substring(String::from("zxabcdezy"), String::from("yzabcdezx")), |
93 | | - 6 |
94 | | - ); |
| 67 | + assert_eq!(longest_common_substring("zxabcdezy", "yzabcdezx"), 6); |
95 | 68 | } |
96 | 69 | #[test] |
97 | 70 | fn test10() { |
98 | 71 | assert_eq!( |
99 | | - longest_common_substring( |
100 | | - String::from("OldSite:GeeksforGeeks.org"), |
101 | | - String::from("NewSite:GeeksQuiz.com") |
102 | | - ), |
| 72 | + longest_common_substring("OldSite:GeeksforGeeks.org", "NewSite:GeeksQuiz.com"), |
103 | 73 | 10 |
104 | 74 | ); |
105 | 75 | } |
|
0 commit comments