Longest Common Prefix of an Array of Strings
Implement longestCommonPrefix
Given an array of strings
strs, return the longest common prefix shared by every string in the array. If there is no common prefix, return an empty string "".
Two natural ways to search for it: vertical scanningVertical ScanningComparing one character POSITION across every string before moving to the next position — column by column, as if the strings were stacked on top of each other. checks one character position across every string before moving to the next; horizontal scanning starts with the whole first string as a guess and shrinks it against each remaining string until it fits everywhere. Both do the same character comparisons in the worst case — the difference is just which order you visit them in.
Example 1:
Input: strs = ["flower","flow","flight"]
Output: "fl"
Example 2:
Input: strs = ["dog","racecar","car"]
Output: ""
Example 3:
Input: strs = ["single"]
Output: "single"
+ 8 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ strs.length ≤ 200 - ●
0 ≤ strs[i].length ≤ 200 - ●
strs[i] consists of lowercase English letters
strs =
["flower", "flow", "flight"]