Count a Target Character
Given a string s and a target character target, return the number of times the target appears in the string.
The comparison is case-sensitive: uppercase and lowercase letters are different characters. Count every exact occurrence, including digits, spaces, and special characters. If the target does not occur, return 0.
Example 1:
Input: s = "banana", target = "a"
Output: 3
Explanation: The lowercase character a appears at positions 1, 3, and 5 (0-based). Each matches the target exactly, so the total is 3.
Example 2:
Input: s = "AaAaA", target = "A"
Output: 3
Explanation: Uppercase A appears at positions 0, 2, and 4 and is counted three times. The lowercase a characters at positions 1 and 3 are excluded because the comparison is case-sensitive.
Now Your Turn!
Pick the correct output for the given inputInput: s = "hello world", target = "z"
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 0 <= s.length() <= 105
- s contains printable ASCII characters (codes 32–126), including spaces.
- target is exactly one printable ASCII character, including a space.