Decoded String at Index LeetCode solution in Java
1class Solution {2public String decodeAtIndex(String s, int k) {3long sz = 0;4for (char ch : s.toCharArray()) { // total length5sz = Character.isDigit(ch) ? sz * (ch - '0') : ++sz;6}7--k; // make it 0 index based.8for (int i = s.length() - 1; true; i--) {9if (Character.isLetter(s.charAt(i)) && --sz == k) { // found!10return "" + s.charAt(i);11} else if (Character.isDigit(s.charAt(i))) {12sz /= (s.charAt(i) - '0');13k %= sz; // we are at the end of a multplied string, we can mod k with sz.14}15}16}17}