Longest common prefix
MediumStrings · JavaScript
The task
Write longestCommonPrefix(strs) that returns the longest prefix shared by every string in the array.
If they share nothing, return an empty string. An empty array also returns an empty string.
Examples
- longestCommonPrefix(["flower", "flow", "flight"]) returns "fl"
Your code runs in your browser, in a separate worker, and is never sent to us. Edit the function, then press Run tests. Your work is saved here as you type.
JavaScriptCtrl or Cmd + Enter to run