1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/// Binary substring /// /// Count substrings with equal consecutive 0s and 1s /// /// # Examples /// /// Basic usage: /// ``` /// let result = algorithmz::string::count_binary_substring("00110011"); /// assert_eq!(result,6); /// ``` pub fn count_binary_substring(input_string: &str) -> i32 { let input = input_string.as_bytes(); let mut current = 1; let mut previous = 0; let mut count = 0; for index in 1..input.len() { if input[index] != input[index - 1] { count = count + std::cmp::min(previous, current); previous = current; current = 1; } else { current = current + 1; } } count = count + std::cmp::min(previous, current); count }