Back to all solutions
#3084 - Count Substrings Starting and Ending with Given Character
Problem Description
You are given a string s and a character c. Return the total number of substrings of s that start and end with c.
Solution
/**
* @param {string} s
* @param {character} c
* @return {number}
*/
var countSubstrings = function(s, c) {
let count = 0;
for (const char of s) {
if (char === c) count++;
}
return (count * (count + 1)) / 2;
};