194. Count Substrings Starting and Ending with Given Character

EasyStringString

You are given a string s and a character c.

Return the total number of substrings of s that start AND end with c.

A single character c at any position also counts as a valid substring (starts and ends with itself).

Input: Two comma-separated values: string s and character c.

Output: An integer — the count of substrings starting and ending with c.

Examples

Example 1
Input: abada,a
Output: 6
Explanation: 'a' at indices 0,2,4 (count=3). Substrings: [0,0],[2,2],[4,4],[0,2],[0,4],[2,4] = 3+2+1 = 6 = 3*4//2.
Example 2
Input: aaaa,a
Output: 10
Explanation: 4 occurrences of 'a'. Answer = 4*5//2 = 10.
Example 3
Input: hello,z
Output: 0
Explanation: 'z' not in 'hello'.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →