Skip to content

Commit 4b1ca4e

Browse files
authored
Improve 14.longest-common-prefix.jl
This change makes the algorithm able to handle trivial cases where there is only one input string or where all input strings are equal
1 parent 7452e24 commit 4b1ca4e

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

src/problems/14.longest-common-prefix.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ using LeetCode
4848
function longest_common_prefix(strs::Vector{String})::String
4949
s1, s2 = minimum(strs), maximum(strs)
5050
pos = findfirst(i -> s1[i] != s2[i], 1:length(s1))
51-
return isnothing(pos) ? "" : s1[1:(pos - 1)]
51+
return isnothing(pos) ? s1 : s1[1:(pos - 1)]
5252
end
5353

5454
## @lc code=end

0 commit comments

Comments
 (0)