Weighted sum of the characters of a string in an array | Set 2 | C # implementation of the approach ; 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
using System ; using System . Collections . Generic ; class GfG { static int strScore ( string [ ] str , string s , int n ) { Dictionary < string , int > m = new Dictionary < string , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) m [ str [ i ] ] = i + 1 ; if ( ! m . ContainsKey ( s ) ) return 0 ; int score = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) score += s [ i ] - ' a ' + 1 ; score = score * m [ s ] ; return score ; } public static void Main ( ) { string [ ] str = { " geeksforgeeks " , " algorithms " , " stack " } ; string s = " algorithms " ; int n = str . Length ; Console . WriteLine ( strScore ( str , s , n ) ) ; } }