I appeared for an interview, where I was asked to write an algorithm for partial hashing of keys; if ABCBC is inserted into the hash, then searching for any of the substrings should return the stored value. My answer was to create a collection of all possible substrings of a given key and maintain a match between each substring with one or more parent strings. Then support the BST from the collection of all substrings. Each node will point to a set of actual values that may correspond to this substring. E.g. A, AB, ABC, ABCB, ABCBC, B, BC, BCB, BCBC, C, CB, CBC are possible substrings for this string. There may be other strings like BAB, of which AB and B are substrings. So, given AB, it will be displayed on two lines BAB and ABCBC.
Is there an even more efficient way? Thanks
source
share