일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 읽기
- 화상영어
- 3줄정리
- 영어공부
- 매일
- 영어원서읽기
- 리얼 클래스
- 스탭퍼
- Daily Challenge
- 잡생각
- 프로젝트
- 만화도
- 30분
- 사이드
- realclass
- leetcode
- 운동
- English
- Problem Solving
- 월간
- 파비최
- 개발자
- 10분
- 링피트
- Writing
- 쓰릴오브파이트
- FIT XR
- 미드시청
- 괜찮음
- 뭐든
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.07.03 Today's Challenge 본문
https://leetcode.com/problems/buddy-strings/
Buddy Strings - LeetCode
Can you solve this real interview question? Buddy Strings - Given two strings s and goal, return true if you can swap two letters in s so the result is equal to goal, otherwise, return false. Swapping letters is defined as taking two indices i and j (0-ind
leetcode.com
오랫만에 푸는 시도를 해봄 ㅠㅠ 실패했지만..
생각보다 조건이 많았음.
class Solution:
def buddyStrings(self, s: str, goal: str) -> bool:
if len(s) != len(goal):
return False
if s == goal:
freq = [0 for _ in range(26)]
for ch in s:
freq[ord(ch) - ord('a')] += 1
if freq[ord(ch) - ord('a')] == 2:
return True
return False
fi, si = -1, -1
for i in range(len(s)):
if s[i] != goal[i]:
if fi == -1:
fi = i
elif si == -1:
si = i
else:
return False
if si == -1:
return False
return s[fi] == goal[si] and s[si] == goal[fi]
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
2023.07.05 Today's Challenge (0) | 2023.07.05 |
---|---|
2023.07.04 Today's Challenge (0) | 2023.07.04 |
2023.07.02 Today's Challenge (0) | 2023.07.02 |
2023.07.01 Today's Challenge (0) | 2023.07.01 |
2023.06.30 Today's Challenge (0) | 2023.06.30 |
Comments