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

前端 未结 8 722
春和景丽
春和景丽 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条回答
  • 2021-01-31 00:31

    My solution: Used binary search.

    private static int search(String[] words, String searchPrefix) {
    
            if (words == null || words.length == 0) {
                return -1;
            }
            int low = 0;
            int high = words.length - 1;
            int searchPrefixLength = searchPrefix.length();
    
            while (low <= high) {
                int mid = low + (high - low) / 2;
    
                String word = words[mid];
                int compare = -1;
    
                if (searchPrefixLength <= word.length()) {
                    compare = word.substring(0, searchPrefixLength).compareTo(searchPrefix);
                }
    
                if (compare == 0) {
                    return mid;
                } else if (compare > 0) {
                    high = mid - 1;
                } else {
                    low = mid + 1;
                }
    
            }
            return -1;
        }
    
    0 讨论(0)
  • 2021-01-31 00:32

    My current solution in mind is, instead of to find the "prefix", try to find a "virtual prefix".

    For example, prefix is “abd", try to find a virtual-prefix “abc(255)". (255) just represents the max char number. After locating the "abc(255)". The next word should be the first word matching "abd" if any.

    0 讨论(0)
  • 2021-01-31 00:35

    The FreeBSD kernel use a Radix tree for its routing table, you should check that.

    0 讨论(0)
  • 2021-01-31 00:37

    This is just a modified bisection search:

    • Only check as many characters in each element as are in the search string; and
    • If you find a match, keep searching backwards (either linearly or by further bisection searches) until you find a non-matching result and then return the index of the last matching result.
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-31 00:46

    Are you in the position to precalculate all possible prefixes?

    If so, you can do that, then use a binary search to find the prefix in the precalculated table. Store the subscript to the desired value with the prefix.

    0 讨论(0)
提交回复
热议问题