문제
Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string ""
.
Example 1:
Input: ["flower","flow","flight"]
Output: "fl"
Example 2:
Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.
Note:
All given inputs are in lowercase letters a-z
.
풀이
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
ret = ""
if not strs :
return ret
minStr = min(strs, key=len)
for i in range(len(minStr)) :
cur = strs[0][i]
for j in range(1, len(strs)) :
if strs[j][i] != cur :
return ret
ret = ret + cur
return ( ret )
'알고리즘 트레이닝 > LeetCode' 카테고리의 다른 글
20. Valid Parentheses (0) | 2020.05.04 |
---|---|
9. Palindrome Number (0) | 2020.05.04 |
1. Two Sum (0) | 2020.05.03 |
13. Roman to Integer (0) | 2020.05.02 |
7.Reverse Integer (0) | 2020.05.02 |