Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Main {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- while (sc.hasNext()) {
- String S = sc.next();
- int n = sc.nextInt();
- System.out.println(getMaxLength(S, n));
- }
- }
- private static int getMaxLength(String s, int n) {
- ArrayList<ArrayList<Integer>> index = new ArrayList<>();
- for (int i = 0; i < 26; ++i) {
- index.add(new ArrayList<>());
- }
- for (int i = 0; i < s.length(); i++) {
- char ch = s.charAt(i);
- index.get(ch - 'a').add(i);
- }
- int ans = 0;
- for (int i = 0; i < 26; i++) {
- for (int j = 0; j < index.get(i).size(); j++) {
- for (int k = j + 1; k <= index.get(i).size(); ++k) {
- if (judge(index.get(i), j, k, n)) {
- ans = Math.max(ans, k - j);
- }
- }
- }
- }
- return ans;
- }
- private static boolean judge(ArrayList<Integer> index, int from, int end, int n) {
- int mid = (from + end - 1) / 2;
- int left = index.get(mid) - (end - from - 1) / 2;
- int dis = 0;
- for (int i = 0; i < end - from; ++i) {
- int target = left + i;
- int origin = index.get(i + from);
- dis += Math.abs(target - origin);
- }
- return dis <= n;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement