The Most Efficient Algorithm to Find First Prefix-Match From a Sorted String Array?

前端 未结 8 737
春和景丽
春和景丽 2021-01-31 00:27

Input:

1) A huge sorted array of string SA;

2) A prefix string P;

Output:

The index of the first string matching the input prefix if any. If ther

8条回答
  •  旧时难觅i
    2021-01-31 00:44

    If you only want to do this once, use binary search, if on the other hand you need to do it for many different prefixes but on the same string array, building a radix tree can be a good idea, after you've built the tree each look up will be very fast.

提交回复
热议问题