| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 22 | 23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 하늘과 바람과 별과 詩
- LEVEL 2
- 코딩테스트
- 리트코드
- Qualification Round
- leetcode
- Code Jam
- Kubernetes
- Algorithm
- 2022
- First Unique Character in a String
- MySQL
- Certbot/dns-route53
- K8S
- C++
- 문제해결
- 파이썬
- 프로그래머스
- ProblemSolving
- swift
- 하늘과 바람과 별과 시
- 3D PRINTING
- 해커랭크
- Count Monobit Integers
- 알고리즘
- hackerrank
- Code Jam 2022
- Python
- GitLab
Archives
- Today
- Total
공대생의 비망록
[LeetCode][Easy] Longest Common Prefix 문제 Python 풀이 본문
Programming Language/Python
[LeetCode][Easy] Longest Common Prefix 문제 Python 풀이
myungsup1250 2026. 2. 9. 18:06주어진 문자열 배열 strs에 대하여, 가장 긴 공통문자열을 찾는 문제.
예를 들어 "flower", "flow", "flight" 문자열들이 주어졌다면 "fl"을 찾아 반환해야 한다.
문제 풀이:
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
length = 201 # Since the max length for this problem is 200.
# length = min(len(w) for w in strs) # Pythonic way
for s in strs: # set var length as the shortest string's length.
if length > len(s):
length = len(s)
common = ""
for i in range(length):
tmp = strs[0][i]; add = True
for s in strs:
if tmp != s[i]:
return common
common += tmp
return common'Programming Language > Python' 카테고리의 다른 글
| [LeetCode][Easy] Valid Palindrome 문제 Python 풀이 (0) | 2026.02.09 |
|---|---|
| [LeetCode][Easy] First Unique Character in a String 문제 Python 풀이 (0) | 2026.02.09 |
| [LeetCode][Easy] Valid Anagram 문제 Python 풀이 (0) | 2026.02.08 |
| [LeetCode][Easy] Count Monobit Integers 문제 Python 풀이 (0) | 2026.02.08 |
| [LeetCode][Easy] Single Number 문제 Python 풀이 (0) | 2026.02.08 |
Comments
