문제 링크 : https://leetcode.com/problems/find-the-difference/
s와 t가 주어질 때 t 안에서 s에 없는걸 찾는 문제이다.
먼저 s,t를 정렬하고 탐색하면서 다른 점을 찾는다.
s의 범위만큼 탐색하면서 만약에 범위 내에서 다른 점을 찾지 못했다면 가장 마지막에 있는 걸 반환한다.
ex. s = abcd, t = abcde
s의 범위 내에서 탐색하고 남은 e가 반환됨
class Solution:
def findTheDifference(self, s: str, t: str) -> str:
s=sorted(s)
t=sorted(t)
for i in range(len(s)):
if t[i] != s[i]:
return t[i]
return t[-1]
Runtime: 81 ms, faster than 8.97% of Python3 online submissions for Find the Difference.
Memory Usage: 13.7 MB, less than 98.27% of Python3 online submissions for Find the Difference.
아래 코드는 초기에 짰다가 실패한 코드이다..
class Solution:
def findTheDifference(self, s: str, t: str) -> str:
for i in range(len(t)):
if t[i] not in s:
return t[i]
실패한 테.케.
Input
"a"
"aa"
Output
null
Expected
"a"