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
35 c.
Top 12%
helbling
c
#3
36 c.
Top 19%
oskar
Natanaelel
#4
37 c.
Top 31%
Steffan
randolchance
tree
#5
38 c.
Top 35%
scpchicken
#6
39 c.
Top 38%
Thomasdcrt
#7
67 c.
Top 42%
SephQ
#8
71 c.
Top 46%
Shanethegamer
#9
75 c.
Top 50%
alexisab
#10
77 c.
Top 54%
maximec-indy
#11
79 c.
Top 65%
AlephSquirrel
mathis
ComputerKWasTaken
#12
81 c.
Top 73%
madeforlosers
davyboyh
#13
84 c.
Top 81%
Pascal
rucin93
#14
87 c.
Top 85%
Mif712
#15
99 c.
Top 88%
CGjupoulton
#16
150 c.
Top 92%
cabz0r
#17
398 c.
Top 96%
Arnaud
#18
433 c.
Top 100%
Sidd5arth