Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- suppose we got the longest common prefix let's say ans then ans will be prefix part of all the strings and the and suppose ith character differs in any string,then longest common prefix will be till the i-1 chars so take a random string for eg. 0th string and check for each of it's chars with all the chars, the moment it differs you will return else if the oth string gets over,still that is gonna be the longest common prefix
- '''
- class Solution:
- def longestCommonPrefix(self, strs: List[str]) -> str:
- if not strs:
- return ""
- for i in range(len(strs[0])):
- char = strs[0][i]
- for s in strs:
- if i >= len(s) or s[i] != char:
- return strs[0][:i]
- return strs[0]
Advertisement
Add Comment
Please, Sign In to add comment