Count the number of occurrences of a character in a string
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Counting the number of occurrences of a specific character in a string is a fundamental operation in string manipulation often encountered in programming and text processing. This operation involves scanning through the sequence of characters in the string and counting how many times a particular character appears. This task can be accomplished using various programming techniques and is supported by built-in functions in many programming languages. Below, we explore different methods to achieve this, using Python as the primary example due to its popularity and readability.
Overview of Techniques
- Naive Method: Iterate through the string, checking each character.
- Using Python’s Count Function: Utilize the built-in string method
count(). - Using Collections Module: Implement the
Counterclass for character frequency. - Using Dictionary: Manually build a dictionary to store frequencies.
- Using Regular Expressions: Suitable for pattern matching scenarios.
1. Naive Method
The simplest approach to count a character in a string is by iterating through the string and maintaining a count of occurrences.
2. Using Python’s Count Function
Python strings come with a built-in method count(), which returns the number of times a substring appears in the string, making it very efficient for this purpose.
3. Using Collections Module
The Counter class from the collections module can be particularly useful if you want to find the frequency of all characters in a string.
4. Using Dictionary
Manually building a dictionary can also provide a way to count occurrences, offering flexibility in handling and extending the logic.
5. Using Regular Expressions
For more complex patterns or when dealing with special cases, regular expressions (regex) can be useful, though for counting single characters, they might be an overkill.
Summary Table
| Method | Complexity | Use Case |
| Naive | O(n) | Simple cases |
str.count() | O(n) | Single character counting |
collections.Counter | O(n) | Multi-character analysis |
| Dictionary | O(n) | Flexibility in processing |
| Regular Expressions | O(n) | Complex pattern matching |
Conclusion
Counting characters in a string is common in both elementary and advanced programming scenarios. The choice of method depends largely on specific requirements, such as performance considerations and the need for counting single or multiple characters. Python provides several efficient and easy-to-use tools to facilitate this, making it a preferred choice for string operations in many cases.
Related reading
- Count the number of set bits in a 32-bit integer
- Count the number of set bits in a 32-bit integer
- Count the subsequences of length 4 divisible by 9
- count the total number of 1's in integers from 1 to N
- Counting according to query
- Counting alternating numbers in an array
- counting boolean parenthesizations implementation
- Counting inversions in an array

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.