[LeetCode/Python] 187. Repeated DNA Sequences
2025. 4. 14. 20:17
99클럽 코테 스터디 | 비기너 | 11일차
https://leetcode.com/problems/repeated-dna-sequences/description/
접근 방법
해시를 이용하여 문제를 해결하였음
1. input s에 나타나는 10개의 letter로 된 DNA Sequence들을 dna_dict의 key에 저장함
2. input에서 각 DNA Sequence들이 몇 번씩 출현하는지 횟수를 dna_dict의 value에 저장함
3. dna_dict에서 count가 2 이상인 key들을 output 리스트에 추가함
class Solution:
def findRepeatedDnaSequences(self, s: str) -> List[str]:
dna_dict = {}
for i in range(0, len(s) - 9):
dna = s[i:i+10]
dna_dict.setdefault(dna, 0)
for j in range(0, len(s) - 9):
dna_check_if_repeat = s[j:j+10]
if dna_check_if_repeat in dna_dict:
dna_dict[dna_check_if_repeat] += 1
output = []
for dna, count in dna_dict.items():
if count >= 2:
output.append(dna)
return output
'코딩테스트 > LeetCode' 카테고리의 다른 글
[LeetCode/Python] 1385. Find the Distance Value Between Two Arrays (0) | 2025.04.23 |
---|---|
[LeetCode/Python] 349. Intersection of Two Arrays (0) | 2025.04.21 |
[LeetCode/Python] 1464. Maximum Product of Two Elements in an Array (0) | 2025.04.10 |
[LeetCode/Python] 706. Design HashMap (0) | 2025.04.10 |
[LeetCode/Python] 2283. Check if Number Has Equal Digit Count and Digit Value (0) | 2025.04.09 |