diff --git a/README.md b/README.md index c30e6b6..9bd3e68 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,7 @@ other languages. - X of a kind in a deck of cards (https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards/) - Remove From Sorted Array (https://leetcode.com/problems/remove-duplicates-from-sorted-array/) - Sort the People (https://leetcode.com/problems/sort-the-people) +- Max Repeating Substring (https://leetcode.com/problems/maximum-repeating-substring/) #### Medium diff --git a/src/leetcode/max_repeating_substr.rs b/src/leetcode/max_repeating_substr.rs new file mode 100644 index 0000000..7478c00 --- /dev/null +++ b/src/leetcode/max_repeating_substr.rs @@ -0,0 +1,22 @@ +pub fn max_repeating(sequence: String, word: String) -> i32 { + let mut sub_str = "".to_string(); + while sequence.contains(&sub_str) { + sub_str.push_str(&word) + } + let result = (sub_str.len() - word.len()) / word.len(); + result as i32 +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn find_max_repeating() { + let actual = max_repeating("ababc".to_string(), "ab".to_string()); + assert_eq!(2, actual); + + let actual = max_repeating("ababc".to_string(), "abcd".to_string()); + assert_eq!(0, actual) + } +} diff --git a/src/leetcode/mod.rs b/src/leetcode/mod.rs index 4b9fff5..2078654 100644 --- a/src/leetcode/mod.rs +++ b/src/leetcode/mod.rs @@ -46,3 +46,4 @@ mod intersection_two; mod x_kind; mod remove_duplicates; mod sort_people; +mod max_repeating_substr;