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 |
Tags
- 파이썬 알고리즘 풀기
- 파이썬알고리즘풀기
- 파이썬알고리즘
- 릿코드
- leetcode풀이
- 릿코드 풀기
- python 릿코드
- python Leetcode
- 파이썬 프로그래머스
- LeetCode
- 릿코드 파이썬
- 잇츠디모
- python priority queue
- 파이썬 릿코드
- 릿코드풀기
- binary search
- 상가수익률계산기
- python sorted
- 파이썬 알고리즘
- python xor
- 코틀린기초
- leetcode 풀기
- leetcode풀기
- 릿코드풀이
- 파이썬릿코드
- python zip_longest
- 알고리즘풀이
- python 알고리즘
- 파이썬릿코드풀기
- 알고리즘풀기
Archives
- Today
- Total
소프트웨어에 대한 모든 것
LeetCode 풀기 - 389. Find the Difference 본문
반응형
389. Find the Difference
https://leetcode.com/problems/find-the-difference/
Find the Difference - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
문제)
솔루션1) HashMap
각각의 해쉬맵 구성.
원래 문자의 hashmap에서 random 생성된 문자 해쉬맵을 하나씩 제거해나가서 최종적으로 남은 것이 추가된 문자가됩니다.
class Solution:
def findTheDifference(self, s: str, t: str) -> str:
s = Counter(s)
t = Counter(t)
for ch, freq in s.items():
if ch in t:
t[ch] -= freq
if t[ch] == 0:
del t[ch]
return list(t.keys())[0]
솔루션2) XOR
XOR은 교환법칙, 결합 법칙 성립.
xor 특징:
- A^0 = A
- A^A = 0
결국, s와 t의 각 문자에 대해서 xor을 취하면 추가된 한 문자만 남게됩니다.
class Solution:
def findTheDifference(self, s: str, t: str) -> str:
res = 0
for c in s+t:
res ^= ord(c)
return chr(res)
반응형
'알고리즘 > LeetCode' 카테고리의 다른 글
LeetCode 풀기 - 1288. Remove Covered Intervals (0) | 2022.02.20 |
---|---|
LeetCode 풀기 - 104. Maximum Depth of Binary Tree (0) | 2022.02.15 |
LeetCode 풀기 - 532. K-diff Pairs in an Array (0) | 2022.02.09 |
LeetCode 풀기 - 258. Add Digits (0) | 2022.02.08 |
LeetCode 풀기 - 454. 4Sum II (0) | 2022.02.07 |
Comments