Question: class Solution: def longestCommonPrefix(self, strs: List[str]) -> str: i=0 prefix = size = len(strs) if size ==0: return if size ==1: return strs[0]

class Solution:

def longestCommonPrefix(self, strs: List[str]) -> str:

i=0

prefix = ""

size = len(strs)

if size ==0:

return ""

if size ==1:

return strs[0]

strs.sort()

while i < len(strs)-1 and strs[i][i] == strs[i+1][i]:

prefix +=strs[i][i]

i+=1

return prefix

Leet code is giving me an error:

IndexError: string index out of range while i < len(strs)-1 and strs[i][i] == strs[i+1][i]: Line 11 in longestCommonPrefix (Solution.py) ret = Solution().longestCommonPrefix(param_1) Line 35 in _driver (Solution.py) _driver() Line 46 in (Solution.py)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!