ntree.ai
ntree.aiLongest Common Prefix

Longest Common Prefix

easysolvedattempted

Given an array of strings strs, return the longest substring that every one of them starts with.

If they share no prefix, return the empty string "".

Example 1:

Input:  strs = ["flower", "flow", "flight"]
Output: "fl"
Explanation: every string starts with "fl", and the third letters already differ.

Example 2:

Input:  strs = ["dog", "racecar", "car"]
Output: ""
Explanation: the first letters differ, so nothing is shared.

Constraints:

  • 1 <= length of strs <= 200
  • 0 <= length of strs[i] <= 200
  • strs[i] holds only lowercase English letters
def longestCommonPrefix(strs: list[str]) -> str:
pass
 

Press Visualise to step through your code.