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
- 파이썬알고리즘
- 알고리즘풀이
- python sorted
- 파이썬릿코드풀기
- python 알고리즘
- 파이썬알고리즘풀기
- python priority queue
- leetcode풀이
- 파이썬 프로그래머스
- leetcode 풀기
- 파이썬 알고리즘 풀기
- 파이썬릿코드
- leetcode풀기
- python xor
- 잇츠디모
- 상가수익률계산기
- 릿코드풀기
- 파이썬 릿코드
- 알고리즘풀기
- LeetCode
- 릿코드풀이
- 파이썬 알고리즘
- 릿코드 풀기
- 코틀린기초
- python 릿코드
- python zip_longest
- python Leetcode
- 릿코드
- binary search
- 릿코드 파이썬
Archives
- Today
- Total
소프트웨어에 대한 모든 것
LeetCode 풀기 - 1347. Minimum Number of Steps to Make Two Strings Anagram 본문
알고리즘/LeetCode
LeetCode 풀기 - 1347. Minimum Number of Steps to Make Two Strings Anagram
앤테바 2021. 12. 15. 21:11반응형
1347. Minimum Number of Steps to Make Two Strings Anagram
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/
Minimum Number of Steps to Make Two Strings Anagram - 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) dict 자료구조 사용
class Solution:
def minSteps(self, s: str, t: str) -> int:
s = Counter(s)
t = Counter(t)
for c, count in t.items():
if c in s:
t[c] -= s[c]
return sum([count for c, count in t.items() if count > 0])
솔루션2) Counter 객체 사용
Counter 객체는 +, -, &, | operation을 지원합니다.
class Solution:
def minSteps(self, s: str, t: str) -> int:
s = Counter(s)
t = Counter(t)
return sum((t - s).values())
Counter Mathematical operations
c = Counter(a=3, b=1)
d = Counter(a=1, b=2)
c + d # add two counters together: c[x] + d[x]
Counter({'a': 4, 'b': 3})
c - d # subtract (keeping only positive counts)
Counter({'a': 2})
c & d # intersection: min(c[x], d[x])
Counter({'a': 1, 'b': 1})
c | d # union: max(c[x], d[x])
Counter({'a': 3, 'b': 2})
반응형
'알고리즘 > LeetCode' 카테고리의 다른 글
LeetCode 풀기 - 1829. Maximum XOR for Each Query (0) | 2021.12.18 |
---|---|
LeetCode 풀기 - 2032. Two Out of Three (0) | 2021.12.18 |
LeetCode 풀기 - 1079. Letter Tile Possibilities (0) | 2021.12.15 |
LeetCode 풀기 - 1742. Maximum Number of Balls in a Box (0) | 2021.12.14 |
LeetCode 풀기 - 2053. Kth Distinct String in an Array (0) | 2021.12.13 |
Comments