Leetcode 11: Longest common prefix
Problem statement: 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 ""
.
First, let’s set the obvious limitation of our result: it is either the empty string or our result is at most equal to the smallest string in the given array. With this in mind, we get a first obvious solution, check all substrings of all of the given strings (in ascending order of length) up until the point where we find a mismatch between them. The previously observed substring will then correspond to the longest common prefix.