[LeetCode][python3]0014. Longest Common Prefix

Start the journey
N2I -2020.03.18

  1. My first Solution
N2I -2020.03.18

Explanation:

A normal way to think about this problem. The solution compares chars from the smallest length string of strs to other strings in strs. The for loop will stop when someone diff from the compare.

2. A better Solution

N2I -2020.03.18

Explanation:

The solution takes advantage from the “Comparative of String objects”. The biggest different in sequence will appear in the comparison of min(strs) and max(strs). That is why the solution only compares those two strings in list. It is same idea when you check out the first vocabulary and the last vocabulary in your English Dictionary and you will confirm every vocabulary between them will share the same prefix if the first and the last vocabulary shares them.

--

--

HI I’m Ruka. Now a SWE in Taiwan. Contact via email: nayzi9999@gmail.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store