text: Count substrings that starts with character X and ends with character Y | function to count substrings starting with character X and ending with character Y ; to store total count of required substrings ; to store count of character ' x ' up to the point the string ' str ' has been traversed so far ; traverse ' str ' form left to right ; if true , increment ' count _ x ' ; if true accumulate ' count _ x ' to ' tot _ count ' ; required count ; Driver code
code: function countSubstr ( str , n , x , y ) { var tot_count = 0 ; var count_x = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( str [ i ] === x ) count_x ++ ; if ( str [ i ] === y ) tot_count += count_x ; } return tot_count ; } var str = " " ; var n = str . length ; var x = " " , y = " " ; document . write ( " " + countSubstr ( str , n , x , y ) ) ;