Count of all sub | Function to count all substrings ; Hashmap to store substrings ; Iterate over all substrings ; Variable to maintain sum of all characters encountered ; Variable to maintain substring till current position ; Get position of character in string W ; Add weight to current sum ; Add current character to substring ; Check if sum of characters is <= K insert in Hashmap ; Initialise string ; Initialise weight
function distinctSubstring ( P , Q , K , N ) { let S = new Set ( ) ; for ( let i = 0 ; i < N ; ++ i ) { let sum = 0 ; let s = " " ; for ( let j = i ; j < N ; ++ j ) { let pos = P [ j ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ; sum += Q [ pos ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ; s += P [ j ] ; if ( sum <= K ) { S . add ( s ) ; } else { break ; } } } return S . size ; } let S = " " ; let W = " " ; let K = 5 ; let N = S . length ; document . write ( distinctSubstring ( S , W , K , N ) ) ;