From 47361c871315a7365a72a576499725714613cc5f Mon Sep 17 00:00:00 2001 From: Pavel Karateev Date: Sat, 11 Nov 2023 14:48:48 +0100 Subject: [PATCH] "Count Number of Homogenous Substrings" solutions --- src/count_number_of_homogenous_substrings.py | 19 +++++++++++++++++++ ...t_count_number_of_homogenous_substrings.py | 15 +++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 src/count_number_of_homogenous_substrings.py create mode 100644 tests/test_count_number_of_homogenous_substrings.py diff --git a/src/count_number_of_homogenous_substrings.py b/src/count_number_of_homogenous_substrings.py new file mode 100644 index 0000000..9ddee98 --- /dev/null +++ b/src/count_number_of_homogenous_substrings.py @@ -0,0 +1,19 @@ +MOD = 10**9 + 7 + + +class Solution: + def countHomogenous(self, s: str) -> int: + result = 0 + prev = "" + curr_stack = 1 + + for x in s: + if x == prev: + curr_stack += 1 + else: + curr_stack = 1 + + result += curr_stack + prev = x + + return result % MOD diff --git a/tests/test_count_number_of_homogenous_substrings.py b/tests/test_count_number_of_homogenous_substrings.py new file mode 100644 index 0000000..5e9c14b --- /dev/null +++ b/tests/test_count_number_of_homogenous_substrings.py @@ -0,0 +1,15 @@ +import pytest + +from src.count_number_of_homogenous_substrings import Solution + + +@pytest.mark.parametrize( + "s,expected", + ( + ("abbcccaa", 13), + ("xy", 2), + ("zzzzz", 15), + ), +) +def test_solution(s, expected): + assert Solution().countHomogenous(s) == expected