Weighted sum of the characters of a string in an array | Set 2 | Function to return the required string score ; create a hash map of strings in str ; Store every string in the map along with its position in the array ; If given string is not present in str [ ] ; Multiply sum of alphabets with position ; Driver Code
function strScore ( str , s , n ) { let m = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) m . set ( str [ i ] , i + 1 ) ; if ( ! m . has ( s ) ) return 0 ; let score = 0 ; for ( let i = 0 ; i < s . length ; i ++ ) score += s [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) + 1 ; score = score * m . get ( s ) ; return score ; } let str = [ " " , " " , " " ] ; let s = " " ; let n = str . length ; document . write ( strScore ( str , s , n ) ) ;