1. How I sloved
A common prefix had to be found. "" was returned if there were no elements in the list. The first element was set as the reference point, all the words were checked from the first letter to the other letter, and the common prefix was set before the other characters were present.The prefix so found was returned. But if any one element was empty, "" returned.
2. Code
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
if len(strs) == 0:
return ""
for i in range(len(strs[0])):
for e in strs:
if len(e)==i or e[i] != strs[0][i]:
return strs[0][:i]
return strs[0]
3. Result
Runtime : 28 ms(91.60%), Memory usage : 14.3 MB(100.00%)
(Runtime can be different by a system even if it is a same code.)