Skip to content

Commit 9950973

Browse files
Create 0880-decoded-string-at-index.java
1 parent 22fa926 commit 9950973

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
-----------------------
3+
Time Complexity : O(n)
4+
Space Complexity = O(1)
5+
-----------------------
6+
*/
7+
8+
class Solution {
9+
public String decodeAtIndex(String s, int k) {
10+
long wordLength = 0;
11+
for (char c : s.toCharArray()) {
12+
if (Character.isLetter(c)) {
13+
wordLength += 1;
14+
} else {
15+
wordLength *= Character.getNumericValue(c);
16+
}
17+
}
18+
19+
for (int i = s.length() - 1; i >= 0; i--) {
20+
char c = s.charAt(i);
21+
k %= wordLength;
22+
if (Character.isLetter(c)) {
23+
if (k == 0) {
24+
return String.valueOf(c);
25+
} else {
26+
wordLength -= 1;
27+
}
28+
} else {
29+
wordLength /= Character.getNumericValue(c);
30+
}
31+
}
32+
return "";
33+
}
34+
}

0 commit comments

Comments
 (0)