Longest Common Prefix
Longest Common Prefix Challenge
Let's have some fun with strings! Your mission, should you choose to accept it, is to find the longest common prefix from an array of strings. What is a prefix, you may ask? A prefix is a string composed of the beginning characters of a word. For example, in the word "butterfly", "butter" is a prefix.
Imagine you have a list of names of superheroes, and you want to determine the longest string starting at the beginning that they all share! Or, think about a group of magical spells - we want to find what they all start with before they branch out into their own unique incantations.
Task
Write a function longest_common_prefix that will receive an array of lowercase alphabet strings, and return the longest common prefix string amongst those strings. If there is no common prefix, return an empty string "".
Examples
Here are some magical examples:
# Example 1: longest_common_prefix(["flower","flow","flight"]) # returns "fl" # Example 2: longest_common_prefix(["dog","racecar","car"]) # returns "" # Example 3: longest_common_prefix(["interspecies","interstellar","interstate"]) # returns "inters" # Example 4: longest_common_prefix(["apple","ape","april"]) # returns "ap"
Tips
- If the list is empty, the function should return an empty string.
- Pay attention to edge cases, like strings with different lengths.
Challenge your brain and have fun with this coding quest!
Inputs
Arg 1:
["flower","flow","flight"]Expected Output
"fl"Actual Output