DSA · Strings · #100
Longest Common Prefix
Module 62 · difficulty 2/5·⏱ 30:00starts on first keystroke
Write a function `longestCommonPrefix(strs)` that takes an array of strings `strs` and returns the longest string that is a prefix of every string in the array. If there is no common prefix, return the empty string `""`.
Examples
strs = ["flower", "flow", "flight"]→"fl"— All three share the prefix "fl".strs = ["dog", "racecar", "car"]→""— No common prefix exists.strs = ["interspecies", "interstellar", "interstate"]→"inters"
Constraints
- · 1 <= strs.length <= 200
- · 0 <= strs[i].length <= 200
- · strs[i] consists of only lowercase English letters.
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.