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 3%
voytxt
#2
34 c.
Top 6%
helbling
#3
35 c.
Top 9%
c
#4
36 c.
Top 16%
oskar
Natanaelel
#5
37 c.
Top 28%
Steffan
randolchance
XenThe
tree
#6
38 c.
Top 31%
scpchicken
#7
39 c.
Top 34%
Thomasdcrt
#8
65 c.
Top 38%
Lv0Sys
#9
67 c.
Top 41%
SephQ
#10
71 c.
Top 44%
Shanethegamer
#11
75 c.
Top 47%
alexisab
#12
76 c.
Top 50%
MonsPropre
#13
77 c.
Top 53%
maximec-indy
#14
78 c.
Top 56%
Alex
#15
79 c.
Top 66%
mathis
AlephSquirrel
ComputerKWasTaken
#16
81 c.
Top 72%
madeforlosers
davyboyh
#17
84 c.
Top 78%
rucin93
Pascal
#18
87 c.
Top 81%
Mif712
#19
99 c.
Top 84%
CGjupoulton
#20
150 c.
Top 88%
cabz0r
#21
322 c.
Top 91%
Luke
#22
344 c.
Top 94%
c
#23
398 c.
Top 97%
Arnaud
#24
433 c.
Top 100%
Sidd5arth