Nuts & Bolts Problem (Lock & Key problem) using Hashmap
Last Updated :
03 Nov, 2022
Given a set of n nuts of different sizes and n bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently.
Constraint: Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with a nut to see which one is bigger/smaller.
Examples:
Input : nuts[] = {'@', '#', '$', '%', '^', '&'}
bolts[] = {'$', '%', '&', '^', '@', '#'}
Output : Matched nuts and bolts are-
$ % & ^ @ #
$ % & ^ @ #
Another way of asking this problem is, given a box with locks and keys where one lock can be opened by one key in the box. We need to match the pair.
We have discussed a sorting based solution below post.
Nuts & Bolts Problem (Lock & Key problem) | Set 1
In this post, hashmap based approach is discussed.
- Traverse the nuts array and create a hashmap
- Traverse the bolts array and search for it in hashmap.
- If it is found in the hashmap of nuts then this means bolts exist for that nut.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void nutboltmatch( char nuts[], char bolts[], int n)
{
unordered_map< char , int > hash;
for ( int i = 0; i < n; i++)
hash[nuts[i]] = i;
for ( int i = 0; i < n; i++)
if (hash.find(bolts[i]) != hash.end())
nuts[i] = bolts[i];
cout << "matched nuts and bolts are-" << endl;
for ( int i = 0; i < n; i++)
cout << nuts[i] << " " ;
cout << endl;
for ( int i = 0; i < n; i++)
cout << bolts[i] << " " ;
}
int main()
{
char nuts[] = { '@' , '#' , '$' , '%' , '^' , '&' };
char bolts[] = { '$' , '%' , '&' , '^' , '@' , '#' };
int n = sizeof (nuts) / sizeof (nuts[0]);
nutboltmatch(nuts, bolts, n);
return 0;
}
|
Java
import java.util.HashMap;
class GFG
{
static void nutboltmatch( char nuts[], char bolts[], int n)
{
HashMap<Character, Integer> hash = new HashMap<>();
for ( int i = 0 ; i < n; i++)
hash.put(nuts[i], i);
for ( int i = 0 ; i < n; i++)
if (hash.containsKey(bolts[i]))
nuts[i] = bolts[i];
System.out.println( "matched nuts and bolts are-" );
for ( int i = 0 ; i < n; i++)
System.out.print(nuts[i] + " " );
System.out.println();
for ( int i = 0 ; i < n; i++)
System.out.print(bolts[i] + " " );
}
public static void main(String[] args)
{
char nuts[] = { '@' , '#' , '$' , '%' , '^' , '&' };
char bolts[] = { '$' , '%' , '&' , '^' , '@' , '#' };
int n = nuts.length;
nutboltmatch(nuts, bolts, n);
}
}
|
Python3
def nutboltmatch(nuts,
bolts, n):
hash1 = {}
for i in range (n):
hash1[nuts[i]] = i
for i in range (n):
if (bolts[i] in hash1):
nuts[i] = bolts[i]
print ( "matched nuts and bolts are-" )
for i in range (n):
print (nuts[i],
end = " " )
print ()
for i in range (n):
print (bolts[i],
end = " " )
if __name__ = = "__main__" :
nuts = [ '@' , '#' , '$' ,
'%' , '^' , '&' ]
bolts = [ '$' , '%' , '&' ,
'^' , '@' , '#' ]
n = len (nuts)
nutboltmatch(nuts, bolts, n)
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static void nutboltmatch( char [] nuts, char [] bolts, int n)
{
Dictionary< char , int > hash = new Dictionary< char , int >();
for ( int i = 0; i < n; i++)
{
hash.Add(nuts[i], i);
}
for ( int i = 0; i < n; i++)
if (hash.ContainsKey(bolts[i]))
nuts[i] = bolts[i];
Console.WriteLine( "matched nuts and bolts are-" );
for ( int i = 0; i < n; i++)
Console.Write(nuts[i] + " " );
Console.WriteLine();
for ( int i = 0; i < n; i++)
Console.Write(bolts[i] + " " );
}
static public void Main ()
{
char [] nuts = { '@' , '#' , '$' , '%' , '^' , '&' };
char [] bolts = { '$' , '%' , '&' , '^' , '@' , '#' };
int n = nuts.Length;
nutboltmatch(nuts, bolts, n);
}
}
|
Javascript
<script>
function nutboltmatch(nuts, bolts, n) {
let hash = new Map();
for (let i = 0; i < n; i++)
hash.set(nuts[i], i);
for (let i = 0; i < n; i++)
if (hash.has(bolts[i]))
nuts[i] = bolts[i];
document.write( "matched nuts and bolts are-<br>" );
for (let i = 0; i < n; i++)
document.write(nuts[i] + " " );
document.write( "<br>" );
for (let i = 0; i < n; i++)
document.write(bolts[i] + " " );
}
let nuts = [ '@' , '#' , '$' , '%' , '^' , '&' ];
let bolts = [ '$' , '%' , '&' , '^' , '@' , '#' ];
let n = nuts.length;
nutboltmatch(nuts, bolts, n);
</script>
|
Output
matched nuts and bolts are-
$ % & ^ @ #
$ % & ^ @ #
The time complexity for this solution is O(n).
Auxiliary space: O(n) because using hashmap
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...