PS/LeetCode
[LeetCode] 389. Find the Difference
판교토끼
2020. 12. 8. 23:23
728x90
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
문자열이 두 개 주어지는데, 임의의 위치에서 하나의 문자가 다르게 들어오는데, 이 문자를 찾는 문제이다.
두 문자를 모두 정렬한 후에 비교해서 다른 index의 문자를 반환하는 식으로 풀었다.
class Solution:
def findTheDifference(self, s: str, t: str) -> str:
sorted_s = sorted(s)
sorted_t = sorted(t)
for index in range(len(s)):
if sorted_t[index] != sorted_s[index]:
return sorted_t[index]
return sorted_t[-1]728x90