https://leetcode.com/problems/repeated-dna-sequences/description/
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.
Write a function to find all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule.
For example,
Given s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT", Return: ["AAAAACCCCC", "CCCCCAAAAA"].
Sol:
public class Solution { public List<String> findRepeatedDnaSequences(String s) { // Brute Force. // Time O(n) Space O(10*n) final List<String> result = new ArrayList<>(); if (s.length() < 10) return result; final Map<String, Integer> counter = new HashMap<>(); for (int i = 0; i < s.length() - 9; ++i){ final String key = s.substring(i, i + 10); int value = counter.getOrDefault(key, 0); counter.put(key, value + 1); } for (Map.Entry <String, Integer> entry : counter.entrySet()){ if (entry.getValue() > 1){ result.add(entry.getKey()); } } return result; } }