📜  数组中字符串的字符的加权和|套装2

📅  最后修改于: 2021-04-22 07:55:45             🧑  作者: Mango

给您一个字符串数组str [] ,任务是从数组中找到给定字符串s的得分。的字符串的分数被定义为它的字符的字母值与所述阵列中的字符串的位置乘积

例子:

方法:
在SET 1中,我们看到了一种方法,每次执行查询时,都必须通过str []的一次遍历来找到字符串的位置。当存在大量使用哈希表的查询时,可以对此进行优化。

  • 创建存在于str []中的所有字符串及其在数组中的相应位置的哈希图。
  • 然后,对于每个查询s ,检查s是否存在于地图中。如果是,则计算s的字母值之和并将其存储在sum中
  • 打印sum * pos ,其中pos是与s在映射中关联的值,即其在str []中的位置。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the required string score
int strScore(string str[], string s, int n)
{
    // create a hash map of strings in str
    unordered_map m;
  
    // Store every string in the map
    // along with its position in the array
    for (int i = 0; i < n; i++)
        m[str[i]] = i + 1;
  
    // If given string is not present in str[]
    if (m.find(s) == m.end())
        return 0;
  
    int score = 0;
  
    for (int i = 0; i < s.length(); i++)
        score += s[i] - 'a' + 1;
  
    // Multiply sum of alphabets with position
    score = score * m[s];
  
    return score;
}
  
// Driver code
int main()
{
    string str[] = { "geeksforgeeks", "algorithms", "stack" };
    string s = "algorithms";
    int n = sizeof(str) / sizeof(str[0]);
    int score = strScore(str, s, n);
    cout << score;
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.HashMap;
import java.util.Map;
  
class GfG
{
    // Function to return the required string score 
    static int strScore(String str[], String s, int n) 
    { 
        // create a hash map of strings in str 
        HashMap m = new HashMap<>(); 
      
        // Store every string in the map 
        // along with its position in the array 
        for (int i = 0; i < n; i++) 
            m.put(str[i], i + 1); 
      
        // If given string is not present in str[] 
        if (!m.containsKey(s)) 
            return 0; 
      
        int score = 0; 
      
        for (int i = 0; i < s.length(); i++) 
            score += s.charAt(i) - 'a' + 1; 
      
        // Multiply sum of alphabets with position 
        score = score * m.get(s); 
      
        return score; 
    } 
  
    // Driver code
    public static void main(String []args)
    {
        String str[] = { "geeksforgeeks", "algorithms", 
                                            "stack" }; 
        String s = "algorithms"; 
        int n = str.length; 
        System.out.println(strScore(str, s, n));
          
    }
}
  
// This code is contributed by Rituraj Jain


Python3
# Python3 implementation of the approach 
  
# Function to return the required 
# string score 
def strScore(string, s, n) : 
  
    # create a hash map of strings in str 
    m = {} 
  
    # Store every string in the map 
    # along with its position in the array 
    for i in range(n) : 
        m[string[i]] = i + 1
  
    # If given string is not present in str[] 
    if s not in m.keys() :
        return 0
  
    score = 0
  
    for i in range(len(s)) :
        score += ord(s[i]) - ord('a') + 1
  
    # Multiply sum of alphabets 
    # with position 
    score = score * m[s] 
  
    return score 
  
# Driver code 
if __name__ == "__main__" : 
  
    string = [ "geeksforgeeks", 
               "algorithms", "stack" ] 
    s = "algorithms"
    n = len(string)
    score = strScore(string, s, n); 
    print(score) 
  
# This code is contributed by Ryuga


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
  
class GfG
{
    // Function to return the required string score 
    static int strScore(string [] str, string s, int n) 
    { 
        // create a hash map of strings in str 
        Dictionary m = new Dictionary();
          
        // Store every string in the map 
        // along with its position in the array 
        for (int i = 0; i < n; i++) 
            m[str[i]] = i + 1; 
      
        // If given string is not present in str[] 
        if (!m.ContainsKey(s)) 
            return 0; 
      
        int score = 0; 
      
        for (int i = 0; i < s.Length; i++) 
            score += s[i] - 'a' + 1; 
      
        // Multiply sum of alphabets with position 
        score = score * m[s]; 
      
        return score; 
    } 
  
    // Driver code
    public static void Main()
    {
        string [] str = { "geeksforgeeks", "algorithms", 
                                            "stack" }; 
        string s = "algorithms"; 
        int n = str.Length; 
        Console.WriteLine(strScore(str, s, n));
          
    }
}
  
// This code is contributed by ihritik


输出:
244