Count Word with Given Prefix
Given an lexicographical order array $words$ with size of $n$, the strings among $words$ are all writen in lower case letters. And given a specific string $prefix$ with a length of $m$, count the number of strings in $words$ that have a prefix of $prefix$.
Example:
1 | Input: ["aaa", "ba", "bac", "bat", "batbat", "batq", "bta", "c"]; |
Constraints:
- The time complexity should be $O(m\log n)$
Solution
1 | class CountPrefix{ |
Explanation:
The main idea of this algorithm is using binary search to find the boundary of words which contain the specific prefix. The function $findFirst$ denotes the position where first word has the prefix and function $findLast$ denotes the last one. The only difference between $findFirst$ and $findLast$ is that when hit a proper case, $findFirst$ continues to search the section before but the $findLast$ search the section behind. By doing this in repetition, the first one and the last one will be found.
About the time complexity, the time complexity of function $hasPrefix$ is $O(m)$. Suppose the time complexity of function $findFirst$ and $findLast$ is $T(n)$, then
$$
T(n)=T(\frac{n}{2}) + O(m), T(1) \in O(m),
$$
which means $T(n) \in O(m \log n)$. Thus the time complexity of $countPrefix$ is $O(m\log n)$.