Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string "".
We use analytics and advertising cookies to understand how the site is used and whether our ads on Facebook and Instagram work. They are set only if you accept. See our Privacy Policy for details.
Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string "".
An easy strings problem, graded against 6 test cases (3 of them hidden).
Parsing, comparison, and character-frequency reasoning.
Reach for it when you see: String input where the work is scanning, comparing, or counting characters.
More Strings problems →Compare characters column by column across all strings. Stop when a mismatch is found or we reach the end of any string.
The full reference solution in every supported language stays in the editor above - reveal it there once you have had a real attempt.
Read off this problem's own test suite, so these are the cases a submission actually has to survive.
These apply to the pattern as a whole, not just this problem.