163. Longest Common Prefix

EasyStringString

Given an array of strings, find and return the longest common prefix string among all the strings.

If there is no common prefix, return an empty string ''.

Input strings are given as a comma-separated list.

Input: A comma-separated list of strings.

Output: A string — the longest common prefix, or '' if none.

Examples

Example 1
Input: flower,flow,flight
Output: fl
Explanation: All three strings share 'fl' as the longest common prefix.
Example 2
Input: dog,racecar,car
Output: 
Explanation: There is no common prefix among the three strings.
Example 3
Input: interview,internal,interact
Output: inter
Explanation: All three start with 'inter'.

Constraints

Asked by

WiproBloombergAppleGoogleAmazonMicrosoft
Solve this problem in the editor →