Skip to content
Related Articles

Related Articles

Classify strings from an array using Custom Hash Function
  • Last Updated : 24 Feb, 2021

Given an array of strings arr[] consisting of N strings, the task is to categorize the strings according to the hash value obtained by adding ASCII values % 26 of the characters in the string.

Examples:

Input: arr[][] = {“geeks”, “for”, “geeks”}
Output:
geeks geeks
for
Explanation:
The hash value of string “for” is (102 + 111 + 114) % 26 = 14
The hash value of string “geeks” is (103 + 101 + 101 + 107 + 115) % 26 = 7
Therefore, two “geeks” are grouped together and “for” is kept in another group.

Input: arr[][] = {“adf”, “aahe”, “bce”, “bgdb”}
Output: 
aahe bgdb
bce
adf 
Explanation: 
The hash value of string “adf” is (97 + 100 + 102)%26 = 13
The hash value of string “aahe” is (97 + 97 + 104 + 101)%26 = 9
The hash value of string “bce” is (98 + 99 + 101)%26 = 12
The hash value of string “bgdb” is (98 + 103 + 100 + 98)%26 = 9
Therefore,  strings “aahe” and “bgdb” have same hashed value, so they are grouped together.

Approach: The idea is to use Map Data Structure for grouping together the strings with the same hash values. Follow the steps below to solve the problem:

  • Initialize a Map, say mp, to map hash values with respective strings in a vector.
  • Traverse the given array of strings and perform the following steps:
    • Calculate the hash value of the current string according to the given function.
    • Push the string into the vector with the calculated hash values of the string as key in the map mp.
  • Finally, traverse the map mp and print all the strings of respective keys.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the hash
// value of the string s
int stringPower(string s)
{
    // Stores hash value of string
    int power = 0;
    int n = s.length();
 
    // Iterate over characters of the string
    for (int i = 0; i < n; i++) {
 
        // Calculate hash value
        power = (power + (s[i])) % 26;
    }
 
    // Return the hash value
    return power;
}
 
// Function to classify the strings
// according to the given condition
void categorisation_Of_strings(
    vector<string> s, int N)
{
    // Maps strings with their strings
    // respective hash values
    map<int, vector<string> > mp;
 
    // Traverse the array of strings
    for (int i = 0; i < N; i++) {
 
        // Find the hash value of the
        // of the current string
        int temp = stringPower(s[i]);
 
        // Push the current string in
        // value vector of temp key
        mp[temp].push_back(s[i]);
    }
 
    // Traverse over the map mp
    for (auto power : mp) {
 
        // Print the result
        for (auto str : power.second) {
            cout << str << " ";
        }
        cout << endl;
    }
}
 
// Driver Code
int main()
{
    vector<string> arr{ "adf", "aahe",
                        "bce", "bgdb" };
    int N = arr.size();
 
    categorisation_Of_strings(arr, N);
 
    return 0;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program for the above approach
 
# Function to find the hash
# value of the string s
def stringPower(s):
   
    # Stores hash value of string
    power = 0
    n = len(s)
 
    # Iterate over characters of the string
    for i in range(n):
       
        # Calculate hash value
        power = (power + ord(s[i])) % 26
 
    # Return the hash value
    return power
 
# Function to classify the strings
# according to the given condition
def categorisation_Of_strings(s, N):
   
    # Maps strings with their strings
    # respective hash values
    mp = {}
 
    # Traverse the array of strings
    for i in range(N):
       
        # Find the hash value of the
        # of the current string
        temp = stringPower(s[i])
 
        # Push the current string in
        # value vector of temp key
        if temp in mp:
            mp[temp].append(s[i])
        else:
            mp[temp] = []
            mp[temp].append(s[i])
             
    # Traverse over the map mp
    for i in sorted (mp) :
       
        # Print the result
        for str in mp[i]:
            print(str,end = " ")
        print("\n",end = "")
 
# Driver Code
if __name__ == '__main__':
    arr =  ["adf", "aahe", "bce", "bgdb"]
    N = len(arr)
    categorisation_Of_strings(arr, N)
 
    # This code is contributed by ipg2016107.

chevron_right


Output: 

aahe bgdb 
bce 
adf

 

Time Complexity: O(N*M), where M is the length of the longest string.
Auxiliary Space: O(N*M)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :