Count all sub | Function to find the count of all the subStrings with weight of characters atmost K ; Hashmap to store all subStrings ; Iterate over all subStrings ; Maintain the sum of all characters encountered so far ; Maintain the subString till the current position ; Get the position of the character in String Q ; Add weight to current sum ; Add current character to subString ; If sum of characters is <= K then insert in leto the set ; Finding the size of the set ; Driver code
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 P = " " ; let Q = " " ; let K = 5 ; let N = P . length ; document . write ( distinctSubString ( P , Q , K , N ) ) ;