Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 더현대서울 맛집
- 맥북
- 편스토랑 우승상품
- 파이썬
- 프로그래머스
- dacon
- AI 경진대회
- leetcode
- Docker
- 우분투
- 데이콘
- hackerrank
- ubuntu
- PYTHON
- 캐치카페
- 금융문자분석경진대회
- gs25
- 자연어처리
- 코로나19
- 백준
- Git
- github
- Baekjoon
- 프로그래머스 파이썬
- 편스토랑
- Kaggle
- SW Expert Academy
- programmers
- ChatGPT
- Real or Not? NLP with Disaster Tweets
Archives
- Today
- Total
솜씨좋은장씨
[leetCode] 389. Find the Difference (Python) 본문
728x90
반응형
You are given two strings s and t.
String t is generated by random shuffling string s and then add one more letter at a random position.
Return the letter that was added to t.
Example 1:
Input: s = "abcd", t = "abcde"
Output: "e"
Explanation: 'e' is the letter that was added.
Example 2:
Input: s = "", t = "y"
Output: "y"
Example 3:
Input: s = "a", t = "aa"
Output: "a"
Example 4:
Input: s = "ae", t = "aea"
Output: "a"
Constraints:
- 0 <= s.length <= 1000
- t.length == s.length + 1
- s and t consist of lower-case English letters.
Solution
from collections import Counter
class Solution:
def findTheDifference(self, s: str, t: str) -> str:
cnt_s = Counter(list(s))
cnt_t = Counter(list(t))
s_keys = list(cnt_s.keys())
for key in s_keys:
cnt_t[key] = cnt_t[key] - cnt_s[key]
t_items = [item[0] for item in cnt_t.items() if item[1] != 0]
answer = "".join(t_items)
return answer
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[Programmers] 가운데 글자 가져오기 (Python) (0) | 2021.02.07 |
---|---|
[Programmers] 나누어 떨어지는 숫자 배열 (Python) (0) | 2021.02.06 |
[leetCode] 162. Find Peak Element (Python) (0) | 2021.02.02 |
[leetCode] 1232. Check If It Is a Straight Line (Python) (2) | 2021.02.01 |
[leetCode] 1009. Complement of Base 10 Integer (Python) (0) | 2021.01.31 |
Comments