题解 | #DNA序列#
DNA序列
http://www.nowcoder.com/practice/e8480ed7501640709354db1cc4ffd42a
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.next();
int n = in.nextInt();
int cnt;
Map<String,Integer> hm = new LinkedHashMap<>();
int i = 0;
//将子串以及CG出现的次数存入LinkedHashMap
while(i<=s.length()-n){
cnt = 0;
String s1 = s.substring(i,n+i);
for(int j = 0; j<s1.length(); j++){
if(s1.charAt(j) == 'G' || s1.charAt(j) == 'C'){
cnt++;
}
}
if(!hm.containsKey(s1)){
hm.put(s1,cnt);
}
i++;
}
List<Integer> al = new ArrayList<>();
//取出LinkedHashMap中的value,存入list中
for(Map.Entry<String,Integer> me : hm.entrySet()){
al.add(me.getValue());
}
//降序排序
al.sort(new Comparator<Integer>(){
public int compare(Integer i1,Integer i2){
return i2.intValue() - i1.intValue();
}
});
//第一次value == 最大值时输出Key
for(Map.Entry<String,Integer> me : hm.entrySet()){
if(me.getValue() == al.get(0)){
System.out.print(me.getKey());
break;
}
}
}
}