Longest Common Prefix

Find the longest common prefix string amongst an array of strings.

Medium12/15/2023

Problem statement

Write a program that takes an array of lowercase strings and returns the longest common prefix (LCP) that is shared amongst all strings. If there is no common prefix, return an empty string. The words are separated by a space character.

Problem leaderboard

#
Size
%
Users
#1
32 c.
Top 5%
voytxt
#2
36 c.
Top 14%
oskar
Natanaelel
#3
37 c.
Top 27%
tree
Steffan
randolchance
#4
38 c.
Top 32%
scpchicken
#5
39 c.
Top 36%
Thomasdcrt
#6
67 c.
Top 41%
SephQ
#7
71 c.
Top 45%
Shanethegamer
#8
75 c.
Top 50%
alexisab
#9
77 c.
Top 55%
maximec-indy
#10
79 c.
Top 68%
mathis
ComputerKWasTaken
AlephSquirrel
#11
81 c.
Top 77%
madeforlosers
davyboyh
#12
84 c.
Top 86%
Pascal
rucin93
#13
99 c.
Top 91%
CGjupoulton
#14
150 c.
Top 95%
cabz0r
#15
398 c.
Top 100%
Arnaud