1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| class Solution { public int maxRepOpt1(String text) { Map<Character, Integer> map = new HashMap<>(); int n = text.length(); for (int i = 0; i < n; ++i) { char c = text.charAt(i); map.merge(c, 1, Integer::sum); } int ans = 0; for (int i = 0; i < n; ) { int j = i; while (j < n && text.charAt(j) == text.charAt(i)) ++j; int curCnt = j - i;
if (curCnt < map.getOrDefault(text.charAt(i), 0) && (j < n || i > 0)) { ans = Math.max(ans, curCnt + 1); }
int k = j + 1; while (k < n && text.charAt(k) == text.charAt(i)) ++k; ans = Math.max(ans, Math.min(k - i, map.getOrDefault(text.charAt(i), 0))); i = j; } return ans; } }
|