SprintCode.pro

Подготовка к алгоритмическим задачам

Super

Longest Common Prefix

Description: Find the longest common prefix among all strings in the array. Return an empty string if there is none.

Example 1:

Input: strs = ["flower","flow","flight"]
Output: fl

Example 2:

Input: strs = ["interspecies","interstellar","interstate"]
Output: inters

Constraints:

1 <= strs.length <= 200

Строки состоят из строчных латинских букв

Recommended time and space complexity

O(total length of all strings) time.


Hint 1

Take the first string as a prefix candidate.


Hint 2

For each next string, shorten the candidate until it is a prefix of it.


Hint 3

If the candidate becomes empty, there is no common prefix.

A simple string problem popular at screening. Teaches careful boundary handling and the empty-prefix case.

Expected Input :

["flower","flow","flight"]

Expected Output

fl