I assume you want to know the number of times that all letters are consecutive in a string and not just for the letter 'O'.
Make a character dictionary that will hold this count as values to keys as the characters in a string. char_dict = {}
The idea is to have two conditions to match
(1) Is the current character same as the previous character
(2) If the first condition is true then is the current pair of consecutive characters part of a larger substring that has the same consecutive characters.
Simply put, take for example ABBBCBB. When we encounter the third B we want to check whether it is part of sub-string that is consecutive and already accounted for. i.e. BBB should give consecutive count to be 1 and not 2. To implement this we use a flag variable that checks this conditions.
If we use only the (1)st condition BBB
will count as BB
and BB
and not as a single BBB
.
Rest of the code is pretty straight forward.
char_dict = {}
string = "EOOOEOEEFFFOFEOOO"
prev_char = None
flag=0
for char in list(string):
if char not in char_dict:
#initialize the count by zero
char_dict[char] = 0
if char == prev_char and flag !=0:
char_dict[char] += 1
flag = 0
else:
flag = 1
prev_char = char
print(char_dict)