Longest Common Prefix

Longest Common Prefix

This function should take an list of strings and determine the longest common prefix among all the strings. If there is no common prefix, it should return an empty string.

example

Input: List of strings (str).

Output: String (str).

Examples:

assert longest_prefix(["flower", "flow", "flight"]) == "fl"
assert longest_prefix(["dog", "racecar", "car"]) == ""
assert longest_prefix(["apple", "application", "appetizer"]) == "app"
assert longest_prefix(["a"]) == "a"

How it’s used:

  • in autocomplete systems to suggest the most likely completion;
  • in file systems to suggest directory names based on current input;
  • in DNA sequence alignment to identify common substrings.