Suppose we have a string s of lowercase letters, we have to find the total number of substrings that contain one unique character.
So, if the input is like "xxyy", then the output will be 6 as substrings are [x, x, xx, y, y, yy]
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
class Solution: def solve(self, s): total = 0 previous = '' for c in s: if c != previous: previous = c in_a_row = 1 else: in_a_row += 1 total += in_a_row return total ob = Solution() print(ob.solve("xxyy"))