Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 | 29 | 30 |
| 31 |
Tags
- CS
- Data_Structure
- mysql
- meshgraphnet
- LeetCode
- 오블완
- 컴퓨터사이언스
- 데이터
- Mesh
- 컴퓨터공학
- 데베
- Python
- CNN
- 티스토리챌린지
- 자료구조
- db
- GNN
- Leet Code
- 데이터베이스
- coding
- sort
- 대학생
- 코테
- code
- DS
- Database
- SQL
- adaptive remeshing
- 개발자
- 코딩테스트
Archives
- Today
- Total
sy1214ei 님의 블로그
[Leet Code] 409. Longest Palindrome - Python 본문
https://leetcode.com/problems/longest-palindrome/description/
Level : Easy
Topics : Hash Table | String | Greedy
class Solution:
def longestPalindrome(self, s: str) -> int:
# "abccccdd" -> "dccaccd" "dccbccd"
# 해쉬테이블로 각 char타입의 수를 count 해준다.
# 각 문자별 저장되어있는 count가 짝수이면 -> length로 모두 사용
# count가 홀수이면 -> count-1만큼 length로 사용
char_cnt = {}
for char in s:
if char in char_cnt:
char_cnt[char] += 1
else:
char_cnt[char] = 1
length = 0
odd = False
for count in char_cnt.values(): ############
if count%2: # 홀수일때
length += count - 1
odd = True
else: # 짝수일때
length += count
if odd:
length += 1
return length
# Time Complexity : O(n)
# Space Complexity : O(n)

'[Coding]' 카테고리의 다른 글
| [Leet Code] 349. Intersection of Two Arrays - Python (0) | 2024.12.16 |
|---|---|
| [Leet Code] 41. First Missing Positive - Python (1) | 2024.12.16 |
| [Leet Code] 1. Two Sum - Python (0) | 2024.12.03 |
| [Leet Code] 9. Palindrome Number - Python (0) | 2024.11.30 |
| [Leet Code] 21. Merge Two Sorted Lists - Python (7) | 2024.11.29 |