Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started