Count Case-Sensitive Occurrences in a Text
StringsAuto-graded · 23 test casesLast updated
Count how many times a target character, or substring, appears in a text under case-sensitive, non-overlapping matching.
Write a function that reports how many times a target appears inside a piece of text.
You are given two strings, text and target. Return the number of non-overlapping occurrences of target in text, scanning from left to right.
Rules:
- Matching is case-sensitive:
"t"and"T"are different characters. - Usually
targetis a single character, and the answer is simply how many times that character appears intext. - If
targetis longer than one character, treat it as a substring and count its non-overlapping occurrences. When the wholetargetnever appears intext, the answer is0. - If
targetis the empty string, returnlen(text) + 1. - Return an integer.
Constraints
textandtargetare Python strings and may contain letters, digits, spaces and punctuation.0 <= len(text) <= 10^50 <= len(target) <= 10^5- No input validation or exceptions are required.
Examples
Example 1
Input: count_symbol_occurrences('Today is Tuesday', 't')
Output: 0
Example 2
Input: count_symbol_occurrences('This is the first message', 'Hello this is the second message')
Output: 0
Example 3
Input: count_symbol_occurrences('Hello, hello!', 'l')
Output: 4
Hints
Hint 1
For a single-character target, one pass over the text with a counter is enough.
Hint 2
Because a longer target must be matched as a whole substring, comparing character by character is not enough. Check whether the slice of text at each position equals the target.
Hint 3
After a match of a longer target, continue scanning after the end of that match so occurrences never overlap. Python's built-in string counting does exactly this.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Scan text from left to right. At each position, check whether target starts there. If it does, add one to the counter and jump past the matched part, so matches never overlap. Otherwise move forward by one character.
Python's str.count implements exactly this rule. It is case-sensitive, it counts non-overlapping matches, it returns 0 when a multi-character target does not occur, and for an empty target it returns len(text) + 1, because the empty string matches at every gap between characters, including both ends.
Why it is correct
- For a one-character
target, every position holding that character is a match, and matches cannot overlap, so the scan counts each occurrence exactly once. - Comparing characters with
==keeps the match case-sensitive, so"t"does not match"T". - For a longer
target, a match requires the entire substring to be present. A text like"This is the first message"never contains"Hello this is the second message", so the result is0. - Skipping past each match guarantees the non-overlapping rule.
Complexity
- Time: O(n) for a single character, and O(n * m) worst case for a pattern of length m, although CPython's implementation is much faster in practice.
- Space: O(1).
Reference solution
def count_symbol_occurrences(text: str, target: str) -> int:
return text.count(target)