Given a String, each character mapped with a weight( number), compute the total weight of the string.
Input : test_str = 'Lazyroar', {"G" : 1, "e" : 2, "k" : 5, "f" : 3, "s" : 15, "o" : 4, "r" : 6} Output : 63 Explanation : 2 (G*2) + 8(e*4) + 30(s*2) + 10(k*2) + 4(o) + 6(r) +3(f) = 63.
Input : test_str = 'Geeks', {"G" : 1, "e" : 2, "k" : 5, "s" : 15} Output : 25
Method#1: Using loop
This is one of the ways in which this task can be performed. In this, we iterate for all the characters and sum all the weights mapped from dictionary.
Python3
# Python3 code to demonstrate working of # Prefix key match in dictionary # Using dictionary comprehension + startswith() # Initialize dictionary test_dict = { 'tough' : 1 , 'to' : 2 , 'do' : 3 , 'todays' : 4 , 'work' : 5 } # printing original dictionary print ( "The original dictionary : " + str (test_dict)) # Initialize prefix test_pref = 'to' # Using dictionary comprehension + startswith() # Prefix key match in dictionary res = {key: val for key, val in test_dict.items() if key.startswith(test_pref)} # printing result print ( "Filtered dictionary keys are : " + str (res)) |
The original string is : Lazyroar The weighted sum : 81
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2: Using sum()
This is one more way in which this task can be performed. In this, we use generator expression, and sum() is used to compute the summation of individual weights.
Python3
# Python3 code to demonstrate working of # Prefix key match in dictionary # Using map() + filter() + items() + startswith() # Initialize dictionary test_dict = { 'tough' : 1 , 'to' : 2 , 'do' : 3 , 'todays' : 4 , 'work' : 5 } # printing original dictionary print ( "The original dictionary : " + str (test_dict)) # Initialize prefix test_pref = 'to' # Using map() + filter() + items() + startswith() # Prefix key match in dictionary res = dict ( filter ( lambda item: item[ 0 ].startswith(test_pref), test_dict.items())) # printing result print ( "Filtered dictionary keys are : " + str (res)) |
The original string is : Lazyroar The weighted sum : 81
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3 : Using list(),set(),count() methods
Python3
# Python3 code to demonstrate working of # String Weight # Using loop # initializing string test_str = 'Lazyroar' # printing original string print ( "The original string is : " + str (test_str)) # initializing sum dictionary sum_dict = { "G" : 5 , "e" : 2 , "k" : 10 , "f" : 3 , "s" : 15 , "o" : 4 , "r" : 6 } # referring dict for sum # iteration using loop res = 0 x = list ( set (test_str)) for i in x: res + = (test_str.count(i) * sum_dict[i]) # printing result print ( "The weighted sum : " + str (res)) |
The original string is : Lazyroar The weighted sum : 81
Approach#4: using for loop:
- Define a function that takes two arguments, the input string and the weight dictionary.
- Initialize a variable “weight” to 0.
- Iterate through each character in the string.
- If the character is present in the weight dictionary, add its weight to the “weight” variable.
- Return the “weight” variable.
Python3
# Python program for the above approach # Function to calculate the string weight def calculate_string_weight(test_str, weight_dict): weight = 0 for char in test_str: if char in weight_dict: weight + = weight_dict[char] return weight # Driver Code test_str = 'Lazyroar' weight_dict = { "G" : 1 , "e" : 2 , "k" : 5 , "f" : 3 , "s" : 15 , "o" : 4 , "r" : 6 } print (calculate_string_weight(test_str, weight_dict)) test_str = 'Geeks' weight_dict = { "G" : 1 , "e" : 2 , "k" : 5 , "s" : 15 } print (calculate_string_weight(test_str, weight_dict)) |
63 25
Time Complexity: O(n), where n is the length of the input string.
Space Complexity: O(1), as we are not using any additional space proportional to the input size.