일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- English
- 프로젝트
- 개발자
- 읽기
- 미드시청
- Writing
- 운동
- 링피트
- 스탭퍼
- 영어원서읽기
- leetcode
- 30분
- 괜찮음
- FIT XR
- 화상영어
- 쓰릴오브파이트
- 매일
- Daily Challenge
- 리얼 클래스
- 월간
- 만화도
- realclass
- 파비최
- Problem Solving
- 뭐든
- 영어공부
- 사이드
- 잡생각
- 10분
- 3줄정리
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.07.09 Today's Challenge 본문
https://leetcode.com/problems/substring-with-largest-variance/
Substring With Largest Variance - LeetCode
Can you solve this real interview question? Substring With Largest Variance - The variance of a string is defined as the largest difference between the number of occurrences of any 2 characters present in the string. Note the two characters may or may not
leetcode.com
난이도가 조금 있던 문제임.
서브 스트링은 생각보다 여러 베리에이션 있는 것 같다.
class Solution:
def largestVariance(self, s: str) -> int:
cnt1, cnt2, max_variance = 0, 0, 0
pairs = [(l1, l2) for l1 in set(s) for l2 in set(s) if l1 != l2]
for runs in range(2):
for pair in pairs:
cnt1 = cnt2 = 0
for letter in s:
if letter not in pair:
continue
if letter == pair[0]:
cnt1 += 1
elif letter == pair[1]:
cnt2 += 1
if cnt1 < cnt2:
cnt1 = cnt2 = 0
elif cnt1 > 0 and cnt2 > 0:
max_variance = max(max_variance, cnt1 - cnt2)
s = s[::-1]
return max_variance
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
2023.07.11 Today's Challenge (0) | 2023.07.11 |
---|---|
2023.07.10 Today's Challenge (0) | 2023.07.10 |
2023.07.08 Today's Challenge (0) | 2023.07.08 |
2023.07.07 Today's Challenge (0) | 2023.07.07 |
2023.07.06 Today's Challenge (0) | 2023.07.06 |
Comments