Be the first user to complete this post

Add to List 
Nuts & Bolts Problem (Lock & Key problem)
Objective: Given ‘n’ Nuts and ‘n’ Bolts of different sizes. There is onetoone mapping between nuts and bolts. Write an algorithm to find all matches between nuts and bolts
Note: This problem can also be framed as Given ‘n’ keys and ‘n’ locks. There is onetoone mapping between keys and locks, means each lock has a specific key and can be unlocked using that key only. Write an algorithm to find all matches between keys and locks.
You are allowed to compare only nuts with bolts (or keys with locks), nuts with nuts or bolts with bolts comparisons are not allowed.
Example:
[] nuts = {'%', '&', '*', 'x'}
[] bolts = {'%', 'x', '*', '&'} Output:
Matched nuts and bolts are:
[$, %, &, *, x]
[$, %, &, *, x]
Approach  Brute Force:
Compare each nut (or key) will all the bolts (or locks) till we do not find the match.
Time Complexity: O(n^{2})
Code:
Use Hash Map:
 Insert all the nuts as key and its position as value into Hash Map.
 Iterate through all the bolts and check for the nuts in the hash map and put it into the right place.
Code:
Output:
(Hash Map) Matched nuts and bolts are: [%, @, x, $, &] [%, @, x, $, &]
Reference  http://www.geeksforgeeks.org/nutsboltsproblemlockkeyproblemset2hashmap/
Also Read: