Decoded String at Index LeetCode solution in Java

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

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0