StringsAnimated
Longest Common Prefix
Compare strings column by column until a mismatch.
Time
O(n · m)
Extra space
O(1)
Use it for
Autocomplete, file path handling.
How it works
- Take the first word as the candidate prefix.
- Compare it with the next word character by character.
- Cut the candidate down to the part both words share.
- Repeat for every word. What is left is the longest common prefix.
Watch it run
Loading animation…