Skip to content

Commit 31cffd0

Browse files
authored
2022-10-07 update: added "Decoded String at Index" (#128)
1 parent ec8de56 commit 31cffd0

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
588588
| 863. All Nodes Distance K in Binary Tree | [Link](https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/medium/AllNodesDistanceKInBinaryTree.java) |
589589
| 865. Smallest Subtree with all the Deepest Nodes | [Link](https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/medium/SmallestSubtreeWithAllTheDeepestNodes.java) |
590590
| 869. Reordered Power of 2 | [Link](https://leetcode.com/problems/reordered-power-of-2/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/medium/ReorderedPowerOf2.java) |
591+
| 880. Decoded String at Index | [Link](https://leetcode.com/problems/decoded-string-at-index/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/medium/DecodedStringAtIndex.java) |
591592
| 889. Construct Binary Tree from Preorder and Postorder Traversal | [Link](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/medium/ConstructBinaryTreeFromPreorderAndPostorderTraversal.java) |
592593
| 890. Find and Replace Pattern | [Link](https://leetcode.com/problems/find-and-replace-pattern/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/medium/FindAndReplacePattern.java) |
593594
| 894. All Possible Full Binary Trees | [Link](https://leetcode.com/problems/all-possible-full-binary-trees/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/medium/AllPossibleFullBinaryTrees.java) |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.smlnskgmail.jaman.leetcodejava.medium;
2+
3+
// https://leetcode.com/problems/decoded-string-at-index/
4+
public class DecodedStringAtIndex {
5+
6+
private final String s;
7+
private int k;
8+
9+
public DecodedStringAtIndex(String s, int k) {
10+
this.s = s;
11+
this.k = k;
12+
}
13+
14+
public String solution() {
15+
long size = 0;
16+
int n = s.length();
17+
for (int i = 0; i < n; i++) {
18+
char c = s.charAt(i);
19+
if (Character.isDigit(c)) {
20+
size *= c - '0';
21+
} else {
22+
size++;
23+
}
24+
}
25+
for (int i = n - 1; i >= 0; i--) {
26+
char c = s.charAt(i);
27+
k %= size;
28+
if (k == 0 && Character.isLetter(c)) {
29+
return Character.toString(c);
30+
}
31+
if (Character.isDigit(c)) {
32+
size /= c - '0';
33+
} else {
34+
size--;
35+
}
36+
}
37+
throw null;
38+
}
39+
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.smlnskgmail.jaman.leetcodejava.medium;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
public class DecodedStringAtIndexTest {
8+
9+
@Test
10+
public void defaultTest() {
11+
assertEquals(
12+
"o",
13+
new DecodedStringAtIndex("leet2code3", 10).solution()
14+
);
15+
}
16+
17+
}

0 commit comments

Comments
 (0)