Skip to content

Commit c0bc4ab

Browse files
authored
Create Decoded String at Index.java
1 parent f65450e commit c0bc4ab

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Medium/Decoded String at Index.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public String decodeAtIndex(String s, int k) {
3+
long size = 0;
4+
int n = s.length();
5+
for (int i = 0; i < n; i++) {
6+
char c = s.charAt(i);
7+
if (Character.isDigit(c)) {
8+
size *= (c - '0');
9+
} else {
10+
size++;
11+
}
12+
}
13+
for (int i = n - 1; i >= 0; i--) {
14+
char c = s.charAt(i);
15+
k %= size;
16+
if (k == 0 && Character.isLetter(c)) {
17+
return Character.toString(c);
18+
}
19+
if (Character.isDigit(c)) {
20+
size /= (c - '0');
21+
} else {
22+
size--;
23+
}
24+
}
25+
return "";
26+
}
27+
}

0 commit comments

Comments
 (0)