Given a string 'S' of length 'N' and a character 'C', the task is to count the occurrence of the given character in the string.
Return the total number of times the character 'C' appears in the string 'S'.
Let 'N' = 5, 'S' = "bbacb", 'C' = 'b'.
The character 'b' appears 3 times in the string "bbacb".
Therefore, the answer is 3.
The first line contains the integer 'N', the length of the string 'S'.
The second line contains the string 'S' of length 'N'.
The third line contains the character 'C' to be counted.
Output Format :
Return the total count of the character 'C' in the string 'S'.
Note :
You don’t need to print anything. Just implement the given function.
1 <= 'N' <= 10^5
'a' <= characters in 'S' <= 'z'
'a' <= 'C' <= 'z'
Time Limit: 1 sec
7
abacaba
a
4
The input string 'S' is "abacaba" and the character to count 'C' is 'a'.
The character 'a' appears at indices 0, 2, 4, and 6 in the string.
Thus, the total count of 'a' in "abacaba" is 4.
Therefore, the answer is 4.
5
hello
l
2
Iterate through the string and check if each character matches the given character.
Approach:
Algorithm:
O(N), where 'N' is the length of the string 'S'.
We iterate through each of the 'N' characters in the string 'S' once. Thus, the overall time complexity is of the order O(N).
O(1).
We only use a single integer variable 'count' for storing the result, which takes constant space. Thus, the overall space complexity is of the order O(1).