Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/emahtab/repeated-dna-sequences
https://github.com/emahtab/repeated-dna-sequences
leetcode repeated-substring
Last synced: 1 day ago
JSON representation
- Host: GitHub
- URL: https://github.com/emahtab/repeated-dna-sequences
- Owner: eMahtab
- Created: 2024-10-07T15:08:50.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2025-01-21T06:39:46.000Z (5 days ago)
- Last Synced: 2025-01-21T07:27:44.148Z (5 days ago)
- Topics: leetcode, repeated-substring
- Homepage:
- Size: 1.95 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Repeated DNA Sequences
## https://leetcode.com/problems/repeated-dna-sequencesThe DNA sequence is composed of a series of nucleotides abbreviated as 'A', 'C', 'G', and 'T'.
For example, "ACGAATTCCG" is a DNA sequence.
When studying DNA, it is useful to identify repeated sequences within the DNA.Given a string s that represents a DNA sequence, return all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in any order.
```java
Example 1:Input: s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
Output: ["AAAAACCCCC","CCCCCAAAAA"]Example 2:
Input: s = "AAAAAAAAAAAAA"
Output: ["AAAAAAAAAA"]
```### Constraints:
1. 1 <= s.length <= 105
2. s[i] is either 'A', 'C', 'G', or 'T'.## Implementation 1 :
```java
class Solution {
public List findRepeatedDnaSequences(String s) {
List result = new ArrayList<>();
if(s.length() <= 10)
return result;
Map map = new HashMap<>();
for(int i = 0; i <= s.length() - 10; i++) {
String substr = s.substring(i, i+10);
int occurrence = map.getOrDefault(substr, 0);
map.put(substr, occurrence + 1);
}
for(String str : map.keySet()) {
if(map.get(str) > 1)
result.add(str);
}
return result;
}
}
```## Implementation 1a :
```java
class Solution {
public List findRepeatedDnaSequences(String s) {
if(s == null || s.length() < 11)
return new ArrayList<>();Map map = new HashMap<>();
map.put(s.substring(0, 10), 1);
int left = 1;for(int i = 10; i < s.length(); i++) {
String str = s.substring(left, i+1);
int occurrences = map.getOrDefault(str, 0);
map.put(str, occurrences+1);
left++;
}
return map.entrySet()
.stream()
.filter(entry -> entry.getValue() > 1)
.map(entry -> entry.getKey())
.collect(Collectors.toList());
}
}
```