Count in string
Practice
3.5 (23 votes)
Basic programming
String
Problem
67% Success 8434 Attempts 20 Points 2s Time Limit 256MB Memory 1024 KB Max Code

You are given a string S containing lowercase English alphabets and a character k.  

Task

Count the number of occurrences of k in S.

Example

Assumptions

  • S = "abdbs"
  • k = "b"

Approach

  • You can see that the number of occurrences of b in S is 2. Hence, the answer is 2.

Function description

Complete the solve function which takes a string S and a character k as the argument and returns the number of occurrences of k in S:

  • S: Represents the string
  • k: Represents the character

Input format

Note: This is the input format that you must use to provide custom input (available above the Compile and Test button).

  • The first line contains a single integer T, which denotes the number of test cases. T also specifies the number of times you have to run the solve function on a different set of inputs.
  • For each test case:
    • The first line contains a string S.
    • The second line contains the character k.

Output format

For each test case, print a single line representing the number of occurrences of k in S.

Constraints

\(1\leq T \leq 10 \\ 1\leq |S| \leq 10^5 \\ \)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
26 votes
Tags:
ApprovedBasic ProgrammingEasyOpen
Points:20
159 votes
Tags:
Ad-HocApprovedEasyOpen
Points:20
95 votes
Tags:
Basic ProgrammingInclusion-exclusionMath