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 4%
voytxt
#2
34 c.
Top 7%
helbling
#3
35 c.
Top 11%
c
#4
36 c.
Top 19%
oskar
Natanaelel
#5
37 c.
Top 30%
randolchance
tree
Steffan
#6
38 c.
Top 33%
scpchicken
#7
39 c.
Top 37%
Thomasdcrt
#8
67 c.
Top 41%
SephQ
#9
71 c.
Top 44%
Shanethegamer
#10
75 c.
Top 48%
alexisab
#11
77 c.
Top 52%
maximec-indy
#12
79 c.
Top 63%
AlephSquirrel
ComputerKWasTaken
mathis
#13
81 c.
Top 70%
madeforlosers
davyboyh
#14
84 c.
Top 78%
Pascal
rucin93
#15
87 c.
Top 81%
Mif712
#16
99 c.
Top 85%
CGjupoulton
#17
150 c.
Top 89%
cabz0r
#18
344 c.
Top 93%
c
#19
398 c.
Top 96%
Arnaud
#20
433 c.
Top 100%
Sidd5arth