Skip to content

Commit

Permalink
Added max repeating substring solution.
Browse files Browse the repository at this point in the history
  • Loading branch information
jusexton committed Jul 23, 2024
1 parent 9f02f4d commit c7d1f61
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 22 additions & 0 deletions src/leetcode/max_repeating_substr.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}
1 change: 1 addition & 0 deletions src/leetcode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ mod intersection_two;
mod x_kind;
mod remove_duplicates;
mod sort_people;
mod max_repeating_substr;

0 comments on commit c7d1f61

Please sign in to comment.