Longest common prefix

Beginner Problems Basic Strings Easy

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

Examples:

Input : str = ["flowers" , "flow" , "fly", "flight" ]

Output : "fl"

Explanation : All strings given in array contains common prefix "fl".

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

Output : ""

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

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

Constraints

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

Company Tags

TCS Cognizant Accenture Infosys Capgemini Wipro IBM HCL Tech Mahindra MindTree