Open In App

Consecutive characters frequency - Python

Last Updated : 17 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

This problem involves identifying characters that appear consecutively and counting how many times they appear together. Here, we will explore different methods to calculate the frequency of consecutive characters in a string.

Using regular expressions

We can use the re module to efficiently count consecutive character frequencies in the string using regular expressions.

Python
import re
s = "aaabbccaaaa"

# Count consecutive characters using regex
res = re.findall(r"(.)\1*", s)
print(res)  

Output
['a', 'b', 'c', 'a']

Explanation:

  • Regular expression (.)\1* matches any character followed by zero or more occurrences of the same character.
  • This way, we capture groups of consecutive characters and can easily calculate their frequency.

Let's explore some more methods and see how to find the frequency of consecutive characters in a string.

Using for loop

We can iterate through the string and manually count consecutive characters using a for loop.

Python
s = "aaabbccaaaa"

# Initialize result list
res = []
count = 1

# Iterate through the string to count consecutive characters
for i in range(1, len(s)):
    if s[i] == s[i - 1]:
        count += 1
    else:
        res.append(s[i - 1] * count)
        count = 1
res.append(s[-1] * count)  # Append last group
print(res)  

Output
['aaa', 'bb', 'cc', 'aaaa']

Explanation:

  • We iterate through the string and compare each character with the previous one.
  • When characters are the same, we increase the count; otherwise, we store the result and reset the count.

Using groupby() from itertools

groupby() function from the itertools() module can also be used to group consecutive characters and count them.

Python
from itertools import groupby

# Input string
s = "aaabbccaaaa"

# Group and count consecutive characters
res = [''.join(g) for k, g in groupby(s)]

print(res) 

Output
['aaa', 'bb', 'cc', 'aaaa']

Explanation:

  • groupby() function groups consecutive elements in the string, and we join the grouped characters together to form the desired substrings.
  • This method provides a concise way to get consecutive characters.

Using collections.Counter

We can use the Counter from the collections module to count the frequency of characters, but for consecutive characters, this method is less direct.

Python
from collections import Counter
s = "aaabbccaaaa"

# Count frequency of all characters
count = Counter(s)
print(count)  

Output
Counter({'a': 7, 'b': 2, 'c': 2})

Explanation:

  • While the Counter method works well for counting individual characters, it doesn't capture consecutive occurrences directly.
  • For consecutive counting, we'd need extra logic to group characters first.

Using simple string iteration

A basic way is to manually count consecutive characters by iterating through the string and comparing each character to the next.

Python
s = "aaabbccaaaa"

# Initialize result list
res = []
count = 1

# Iterate through the string to count consecutive characters
for i in range(len(s) - 1):
    if s[i] == s[i + 1]:
        count += 1
    else:
        res.append(s[i] * count)
        count = 1
res.append(s[-1] * count)  # Append last group
print(res)  

Output
['aaa', 'bb', 'cc', 'aaaa']

Explanation: This method is similar to the iteration method above but slightly less efficient in handling the last group of consecutive characters, which requires additional logic to append it at the end.


Next Article
Practice Tags :

Similar Reads