20
TreeNode(int start, int end) {28
public int[] longestRepeating(String s, String queryCharacters, int[] queryIndices) {29
char[] sChar = s.toCharArray();30
char[] qChar = queryCharacters.toCharArray();32
TreeNode root = buildTree(sChar, 0, sChar.length - 1);34
int[] result = new int[qChar.length];36
for (int i = 0; i < qChar.length; i++) {37
updateTree(root, queryIndices[i], qChar[i]);43
private TreeNode buildTree(char[] s, int from, int to) {44
if (from > to) return null;45
TreeNode root = new TreeNode(from, to);48
root.rightChar = root.leftChar = s[from];49
root.leftCharLen = root.rightCharLen = 1;53
int middle = from + (to - from) / 2;55
root.left = buildTree(s, from, middle);56
root.right = buildTree(s, middle + 1, to);62
private void updateTree(TreeNode root, int index, char c) {63
if (root == null || root.start > index || root.end < index) {66
if (root.start == index && root.end == index) {67
root.leftChar = root.rightChar = c;70
updateTree(root.left, index, c);71
updateTree(root.right, index, c);76
private void updateNode(TreeNode root) {77
if (root == null) return;78
root.leftChar = root.left.leftChar;79
root.leftCharLen = root.left.leftCharLen;80
root.rightChar = root.right.rightChar;81
root.rightCharLen = root.right.rightCharLen;82
root.max = Math.max(root.left.max, root.right.max);83
if (root.left.rightChar == root.right.leftChar) {84
int len = root.left.rightCharLen + root.right.leftCharLen;85
if (root.left.leftChar == root.left.rightChar86
&& root.left.leftCharLen == root.left.end - root.left.start + 1) {87
root.leftCharLen = len;89
if (root.right.leftChar == root.right.rightChar90
&& root.right.leftCharLen == root.right.end - root.right.start + 1) {91
root.rightCharLen = len;93
root.max = Math.max(root.max, len);