<commit_before>package leetcode;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * https://leetcode.com/problems/longest-repeating-character-replacement/
 */
public class Problem424 {
    public int characterReplacement(String s, int k) {
        int max = 0;
        Map<Character, Set<Integer>> charIdxMap = charIdxMap(s);
        for (Map.Entry<Character, Set<Integer>> entry : charIdxMap.entrySet()) {
            char c = entry.getKey();
            Set<Integer> indices = entry.getValue();
            for (int idx : indices) {
                int tmpMax = 0;
                int i = idx;
                for (int j = 0; i < s.length() && j < k; i++) {
                    if (!indices.contains(i)) {
                        j++;
                    }
                    tmpMax++;
                }
                while (i < s.length() && s.charAt(i++) == c) {
                    tmpMax++;
                }
                max = Math.max(max, tmpMax);
            }
        }
        return max;
    }

    private static Map<Character, Set<Integer>> charIdxMap(String s) {
        Map<Character, Set<Integer>> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (!map.containsKey(c)) {
                map.put(c, new HashSet<>());
            }
            map.get(c).add(i);
        }
        return map;
    }

    public static void main(String[] args) {
        Problem424 prob = new Problem424();
//        System.out.println(prob.characterReplacement("ABAB", 2)); // 4
//        System.out.println(prob.characterReplacement("AABABBA", 1)); // 4
//        System.out.println(prob.characterReplacement("AABAABBA", 1)); // 5
//        System.out.println(prob.characterReplacement("ACABD", 2)); // 4
        System.out.println(prob.characterReplacement("ABBB", 2)); // 4
        System.out.println(prob.characterReplacement("ABBBBABABBC", 2)); // 9
    }
}<commit_msg>Update problem 424<commit_after>package leetcode;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * https://leetcode.com/problems/longest-repeating-character-replacement/
 */
public class Problem424 {
    public int characterReplacement(String s, int k) {
        int max = 0;
        Map<Character, Set<Integer>> charIdxMap = charIdxMap(s);
        for (Map.Entry<Character, Set<Integer>> entry : charIdxMap.entrySet()) {
            char c = entry.getKey();
            Set<Integer> indices = entry.getValue();
            for (int idx : indices) {
                int tmpMax = 0;
                int i = idx;
                int j = 0;
                for (; i < s.length() && j < k; i++) {
                    if (!indices.contains(i)) {
                        j++;
                    }
                    tmpMax++;
                }
                while (i < s.length() && s.charAt(i++) == c) {
                    tmpMax++;
                }
                if (j < k) {
                    i = idx - 1;
                    for (; i >= 0 && j < k; i--) {
                        if (!indices.contains(i)) {
                            j++;
                        }
                        tmpMax++;
                    }
                    while (i >= 0 && s.charAt(i--) == c) {
                        tmpMax++;
                    }
                }
                max = Math.max(max, tmpMax);
            }
        }
        return max;
    }

    private static Map<Character, Set<Integer>> charIdxMap(String s) {
        Map<Character, Set<Integer>> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (!map.containsKey(c)) {
                map.put(c, new HashSet<>());
            }
            map.get(c).add(i);
        }
        return map;
    }

    public static void main(String[] args) {
        Problem424 prob = new Problem424();
//        System.out.println(prob.characterReplacement("ABAB", 2)); // 4
//        System.out.println(prob.characterReplacement("AABABBA", 1)); // 4
//        System.out.println(prob.characterReplacement("AABAABBA", 1)); // 5
//        System.out.println(prob.characterReplacement("ACABD", 2)); // 4
//        System.out.println(prob.characterReplacement("ABBB", 2)); // 4
//        System.out.println(prob.characterReplacement("ABBBBABABBC", 2)); // 9
        System.out.println(prob.characterReplacement("<KEY>RX<KEY>TR<KEY>QLU<KEY>", 2666));
    }
}
{'fraction_non_alphanumeric': 0.14962538563243719, 'fraction_numerical': 0.013221683561040106, 'mean_word_length': 1.5022050716648292, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 31, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4539836', 'n_tokens_mistral': 1395, 'n_tokens_neox': 1300, 'n_words': 282}