Longest Common Prefix

update Jun 29, 2017, 22:42:43

leetcodearrow-up-right

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 "".

Example 1:

Input: ["flower","flow","flight"]
Output: "fl"
Example 2:

Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.

Note:

All given inputs are in lowercase letters a-z.

思路 (排序比首尾):

首先想到的是用BFS解决 shortest path 的方法,即每次比较每个string的同一位置的character看是否相同,不同则返回当前res。但是这样还是比较慢,更好的方法是先按字典顺序排序各String,然后每次只要比较首末两个string。

BFS 的java code:

从左往右,比较每个string在同一个位置的字符是否相等, O(L*N) 时间复杂度,其中 L 为 Common Prefix 的长度,N 为 String 的数量。

BFS CPP Code:

sort 的 python code:

先对所有 String 排序,然后每次从左到右比较首末两个string相应index的char是否相等。 时间复杂度 O(L + NlogN)