1class Solution {2public int compress(char[] chars) {3int index = 0;4int i = 0;56while (i < chars.length) {7int j = i;89while (j < chars.length && chars[j] == chars[i]) {10j++;11}1213chars[index++] = chars[i];1415if (j - i > 1) {16String count = j - i + "";1718for (char c : count.toCharArray()) {19chars[index++] = c;20}21}2223i = j;24}2526return index;27}28}2930// TC: O(n), SC: O(1)