Java for search

import java.util.*;
public class Main {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            String str = sc.nextLine();
            int n = sc.nextInt();
            double rate = 0;
            int index = 0;
            for (int i = 0;i <= str.length() - n; i++) {
                 String sub ="";
                if (i + n == str.length()) {
                      sub = str.substring(i);
                } else {
                      sub = str.substring(i, i + n);
                }
               if (sub.contains("C") && sub.contains("G")) {
                   String str1 = sub.replaceAll("[AT]","");
                   double count = (double)str1.length() / n;
                   if (count > rate) {
                       rate = count;
                       index = i;
                   }
               }
            }
            
            System.out.println(str.substring(index, index+n));
        }
}