Longest Common Prefix

Think before you code.

Find the longest common prefix shared by every string in an array.

easy

Longest Common Prefix

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 : str = ["flowers", "flow", "fly", "flight"]
Output : fl
Explanation :
All strings given in array contains common prefix "fl".

Example 2

Input : str = ["dog", "cat", "animal", "monkey"]
Output :

 

Explanation :
There is no common prefix among the given strings in array.

Example 3

Input : str = ["lady", "lazy"]
Output : la

Constraints

  • 1 <= str.length <= 200
  • 1 <= str[i].length <= 200
  • str[i] contains only lowercase English letters.