Find the Longest Common Prefix of a set of words using a Trie. Walk the single-branch path until a branch or word-end is reached.
Find the Longest Common Prefix (LCP) of all words in a Trie. Walk down the single-branch path until a branch or word-end is encountered.
The LCP is the deepest point in the trie reachable by a single unbranching path from the root. Stop when: (a) a node has 2+ children (branch), or (b) a node is a word-end (isEnd=true).
Walk from root: while the current node has exactly 1 child AND is not a word-end, advance to that child and append the character to the prefix.
There are three reasons the walk stops, each meaning the LCP has been fully found.
O(L) where L = length of the LCP. The walk is bounded by the shortest word length in the worst case.
L = length of the common prefix. LCP is used in string compression, DNS routing, and autocomplete ranking.