mediumHashingPattern: Hashing

String to Hash Index Mapping Solution

Problem Statement

Given a list of unique strings `strings` and an integer `tableSize`, implement a function that maps each string to an index in the hash table. The mapping function should use a standard hashing technique (e.g., polynomial rolling hash) and then apply the modulo operator with `tableSize` to get the final index. Handle potential collisions by using separate chaining or open addressing. The function should return a list of indices corresponding to each input string, preserving the original order. If a collision occurs for a string that has already been mapped, it should still map to its computed index.

Examples

Example 1:
Input:{"strings":["apple","banana","cherry","date"],"tableSize":10}
Output:[0,7,7,2]
Explanation: Using a polynomial rolling hash with prime base 31 and MOD = 10^9 + 7, then modulo tableSize 10: 'apple': hash = (('a'*31^4 + 'p'*31^3 + 'p'*31^2 + 'l'*31^1 + 'e'*31^0) % (10^9+7)) % 10 = 0 'banana': hash = (('b'*31^4 + 'a'*31^3 + 'n'*31^2 + 'a'*31^1 + 'n'*31^0) % (10^9+7)) % 10 = 7 'cherry': hash = (('c'*31^4 + 'h'*31^3 + 'e'*31^2 + 'r'*31^1 + 'r'*31^0) % (10^9+7)) % 10 = 7 'date': hash = (('d'*31^4 + 'a'*31^3 + 't'*31^2 + 'e'*31^1 + 'x'*31^0) % (10^9+7)) % 10 = 2 (Note: The polynomial rolling hash must be applied correctly to all characters. The original example had calculation errors and missing terms. The problem statement implies collision handling is needed, but the examples provided show raw hash indices without any resolution. Assuming the examples are meant to show raw hash indices for demonstration before collision handling.)
Example 2:
Input:{"strings":["cat","dog","act"],"tableSize":5}
Output:[1,3,1]
Explanation: Using the same hashing logic as above with tableSize 5: 'cat': hash = (...) % 5 = 1 'dog': hash = (...) % 5 = 3 'act': hash = (...) % 5 = 1. This example shows the raw hash indices. The problem statement requires handling collisions to ensure unique identification. If collision handling (e.g., linear probing) were applied, 'act' might map to a different index if index 1 is already occupied. The output [1, 3, 1] reflects the initial hash calculation before any collision resolution strategy is applied.

Constraints

  • The input list `strings` contains unique strings.
  • 1 <= len(strings) <= 10^5
  • 1 <= tableSize <= 10^4
  • Each string in `strings` consists of lowercase English letters.
  • The length of each string is between 1 and 50.
Time: O(N*L) Space: O(N)
The optimized approach involves defining a consistent polynomial rolling hash function. Iterate through the input `strings` array. For each string, compute its hash value by iterating through its characters, accumulating the weighted sum (character ASCII value * base^position), and applying a large prime modulo to prevent overflow. Finally, take the result modulo `tableSize` to obtain the index. Store these indices in a result array in the same order as the input strings.

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

def string_to_hash_index_mapping(strings, table_size): indices = [] prime_base = 31 large_prime_mod = 10**9 + 7 for s in strings: current_hash = 0 for char in s: current_hash = (current_hash * prime_base + ord(char)) % large_prime_mod indices.append(current_hash % table_size) return indices