Frequency of a substring in a string | JavaScript program to count occurrences of pat in txt . ; A loop to slide pat [ ] one by one ; For current index i , check for pattern match ; If pat [ 0. . . M - 1 ] = txt [ i , i + 1 , ... i + M - 1 ] ; Driver Code
let mod = 100000007 ; function countFreq ( pat , txt ) { let M = pat . length ; let N = txt . length ; let res = 0 ; for ( let i = 0 ; i <= N - M ; i ++ ) { let j ; for ( j = 0 ; j < M ; j ++ ) { if ( txt [ i + j ] != pat [ j ] ) { break ; } } if ( j == M ) { res ++ ; j = 0 ; } } return res ; } let txt = " " ; let pat = " " ; document . write ( countFreq ( pat , txt ) ) ;