+ 1

Any better Approach?

You are given an input string and an array of strings. You have to find out that string from the array such that its common prefix with the given input string is maximum possible. If there are more that such strings in the given array, give the one which comes first lexicographically. My Approach: Sort the given array. Run a loop through each string element of the array. For every element, count the number of characters in common prefix by comparing given input string and element string character b

6th Jun 2018, 11:09 AM
Babin Dutta
Babin Dutta - avatar
1 Resposta
0
i don‘t know about best for only one string but if you have to find the longest prefix match for a few strings you could use a radix tree(look it up on wikipedia)(the problem you want to solve is called longest prefix match in networking where you need to match ip prefixes), its nice if you want to find the longest orefix for multiple values, because you can reuse it. maybe you could also use that stringcontend xnor stringcontend only contains ones(viewed in binary) + some sorting function
6th Jun 2018, 12:29 PM
Max
Max - avatar