-
Notifications
You must be signed in to change notification settings - Fork 824
/
Copy pathDecodedStringAtIndex.java
69 lines (63 loc) · 1.74 KB
/
DecodedStringAtIndex.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* (C) 2024 YourCompanyName */
package stack;
import java.util.Stack;
/** Created by gouthamvidyapradhan on 12/05/2019 */
public class DecodedStringAtIndex {
public static void main(String[] args) {
System.out.println(new DecodedStringAtIndex().decodeAtIndex("ha22", 5));
}
class Node {
String S;
long count;
int multiple;
Node(String S, long count, int multiple) {
this.S = S;
this.count = count;
this.multiple = multiple;
}
}
public String decodeAtIndex(String S, int K) {
Stack<Node> stack = new Stack<>();
StringBuilder sb = new StringBuilder();
char prev = ' ';
for (char c : S.toCharArray()) {
if (Character.isDigit(c)) {
String currStr = sb.toString();
long len = 0L;
if (!stack.isEmpty()) {
len = stack.peek().count * stack.peek().multiple;
}
stack.push(
new Node(currStr, len + (currStr.length()), Integer.parseInt(String.valueOf(c))));
if (((len + (currStr.length())) * Integer.parseInt(String.valueOf(c))) >= K) {
break;
}
prev = c;
} else {
if (Character.isDigit(prev)) {
sb = new StringBuilder();
}
sb.append(c);
prev = c;
}
}
while (!stack.isEmpty()) {
Node top = stack.peek();
long l = top.count;
if (K <= l) {
return String.valueOf(top.S.charAt((int) l - K - 1));
}
long mod = (K % l);
if (mod == 0) {
return String.valueOf(top.S.charAt(top.S.length() - 1));
}
if (l - top.S.length() < mod) {
long i = l - mod;
return String.valueOf(top.S.charAt(top.S.length() - (int) i - 1));
} else {
stack.pop();
}
}
return "";
}
}