Menu
CoddyTech

Count a Character

You get a string s and a single letter c. Return how many times c appears in s. The match is case-sensitive: B and b are different characters, so only exact copies of c count.

Function

countChar(s: string, c: string) → integer
sstring
the string of English letters to search
cstring
the one letter to count
Returnsinteger
how many characters of s are equal to c

Constraints

  • 1 ≤ s.length ≤ 5 × 104
  • s contains only English letters (a to z, A to Z).
  • c is exactly one English letter.

Examples

Input
s = "Mississippi"c = "s"
Output
4
Explanation
Mississippi has an s at positions 2, 3, 5 and 6, counting from 0, so the answer is 4.

lock icon+18 hidden tests on Submit

challenge icon

Follow-up

What if c could be a word of several letters, such as ss? Do overlapping matches count, and how does your loop change?

Reset code
def countChar(s, c):
    # Write code here
Test cases

Case 1

Case 2

Input

s = "Mississippi"
c = "s"

Expected

4