187. Repeated DNA Sequences
发布时间:2020-12-14 00:27:54 所属栏目:Linux 来源:网络整理
导读: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 se
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. Example: Input: s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT" Output: ["AAAAACCCCC","CCCCCAAAAA"] class Solution { public List<String> findRepeatedDnaSequences(String s) { List<String> res = new ArrayList(); if(s.length() < 10) return res; Map<String,Integer> map = new HashMap(); for(int i = 0; i < s.length() - 9; i++){ String k = s.substring(i,i + 10); int value = map.getOrDefault(k,0); map.put(k,value + 1); } for(Map.Entry<String,Integer> m: map.entrySet()){ if(m.getValue() > 1){ res.add(m.getKey()); } } return res; } } ? ?https://soulmachine.gitbooks.io/algorithm-essentials/java/bitwise-operations/repeated-dna-sequences.html (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |