PIXELBANKv8.2.1
Menu

Longest Common Prefix

Given an array of strings, find the longest common prefix among all strings. If none, return "".

Example:

Input:
flower,flow,flight
Output:
fl
Reasoning:
  • The input strings are compared character by character to find the common prefix: flower, flow, and flight.
  • The first character f is common to all strings, so it is added to the prefix.
  • The second character l is also common to all strings, so it is added to the prefix, resulting in fl.
  • Since the third character differs among the strings (o in flower and flow, i in flight), the comparison stops, and the common prefix fl is returned as the output.

Constraints:

  • 1 <= len(strs) <= 200
  • 0 <= len(strs[i]) <= 200
  • strs[i] consists of lowercase English letters
Editor

Test Results

0/0
Run code to see test results.