Count substrings made up of a single distinct character | C # Program to implement the above approach ; Function to count the number of substrings made up of a single distinct character ; Stores the required count ; Stores the count of substrings possible by using current character ; Stores the previous character ; Traverse the string ; If current character is same as the previous character ; Increase count of substrings possible with current character ; Reset count of substrings possible with current character ; Update count of substrings ; Update previous character ; Driver code
using System ; class GFG { static void countSubstrings ( string s ) { int ans = 0 ; int subs = 1 ; char pre = '0' ; foreach ( char i in s ) { if ( pre == i ) { subs += 1 ; } else { subs = 1 ; } ans += subs ; pre = i ; } Console . WriteLine ( ans ) ; } static void Main ( ) { string s = " geeksforgeeks " ; countSubstrings ( s ) ; } }