题解 | #DNA序列#

DNA序列

https://www.nowcoder.com/practice/e8480ed7501640709354db1cc4ffd42a

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String a = in.nextLine();
            int b = Integer.parseInt(in.nextLine());
            Map<String, Integer> map = new LinkedHashMap();
            for (int i = 0; i <= a.length() - b; i++) {
                int index = 0;
                String str = a.substring(i, i + b);
                String[] strs = str.split("");
                for (String s : strs) {
                    if (s.equals("C") || s.equals("G")) {
                        index++;
                    }
                }
                map.put(str, index);
            }
            int[] is = new int[map.size()];
            int n = 0;
            for (int x : map.values()) {
                is[n] = x;
                n++;
            }
            Arrays.sort(is);
            for (Map.Entry<String, Integer> en : map.entrySet()) {
                if (en.getValue() == is[is.length - 1]) {
                    System.out.print(en.getKey());
                    break;
                }
            }
        }
    }
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务