Hashing is a computer science technique for mapping arbitrary-size input to a fixed-size result. A Hash table is a data structure that stores some information, and the information has basically two main components, i.e., key and value. Cryptography, data indexing, data retrieval, and checksum creation are among applications that employ hash functions.
Note:
The time complexity of hashing is determined by the precise operation being performed, as well as the implementation specifics of the hash table.
Operation | Description |
HashTable | This action generates a new hash table by allocating memory for an array of buckets that will hold key-value pairs. |
Delete | This action deletes a hash table key-value pair. |
Get | This action obtains from the hash table the value associated with a specific key. |
Put | Inserts a new key-value pair into the hash table. |
DeleteHashTable | This action deletes the complete hash table by releasing the memory needed by the bucket array. |
The hash function and the hash table are the two basic components of hashing.
For instance,
Because a hash function returns a tiny number for a large key, it is possible that two keys will return the same value. A collision occurs when a newly added key maps to an existing occupied space in a hash table and must be handled using a collision handling strategy. The following are some methods for dealing with collisions:
For instance, we have a hash table of size 5, and we want to store the following key-value pairs:
To store these values in the hash table, we’ll use the division method to calculate a hash code for each key. However, the keys “cherry” and “dog” both have the same hash code of 2, i.e., both will be stored at index 2 in the table which is a collision and needs to be handled.
Using chaining, where each table entry is a linked list of key-value pairs that have the same hash code we can store the key-value pairs:
index 2: ("cherry", 12) -> ("dog", 2)
index 3: ("apple", 3)
index 4: ("banana", 7)
Now we have a linked list at index 2, which contains the key-value pairs for both “cherry” and “dog”.
To retrieve a value from the hash table, first calculate the hash code for the key, and then traverse the linked list at the corresponding index until we find the key we’re looking for.
For example, to retrieve the value for the key “cherry”, we calculate the hash code as hash(“cherry”) = 2, and then traverse the linked list at index 2 until we find the key “cherry”, whose value is 12.
Given a binary string s
without leading zeros, return true
if s
contains at most one contiguous segment of ones. Otherwise, return false
.
Example 1:
Input: s = "1001" Output: false Explanation: The ones do not form a contiguous segment.
Example 2:
Input: s = "110" Output: true
Solution:
class Solution {
public boolean checkOnesSegment(String s) {
Set<Integer> onesIndices = new HashSet<>();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '1') {
onesIndices.add(i);
}
}
if (onesIndices.size() <= 1) {
return true;
}
int minIndex = Integer.MAX_VALUE;
int maxIndex = Integer.MIN_VALUE;
for (int index : onesIndices) {
minIndex = Math.min(minIndex, index);
maxIndex = Math.max(maxIndex, index);
}
return maxIndex - minIndex + 1 == onesIndices.size();
}
}
Time complexity: O(n), where n is the length of the input string
Space complexity: O(n), where n is the length of the input string
In this implementation, we use a HashSet
named onesIndices
to store the indices of the ones in the binary string s
. We iterate through the characters of s
and whenever we encounter a ‘1’, we add its index to the onesIndices
set.
Next, we check if the size of the onesIndices
set is less than or equal to 1. If so, it means there is at most one contiguous segment of ones in the string, and we return true
.
Note: also read about DSA: Collision in Hashing
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…
There is a staircase with N steps, and you can ascend either 1 step or…
Build an autocomplete system that, given a query string s and a set of possible…
Design a job scheduler that accepts a function f and an integer n. The scheduler…