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.

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 indices 2, 3, 5 and 6, so the answer is 4.
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 #1

Test #2

Arguments

s = "Mississippi"
c = "s"

Expected Output

4