FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/problems/src/stack/DecodedStringAtIndex.java at master · KindleBooks66/leetcode · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
KindleBooks66
/
leetcode
Public
forked from
gouthampradhan/leetcode
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode
/
problems
/
src
/
stack
/
DecodedStringAtIndex.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
68 lines (62 loc) · 1.71 KB
Breadcrumbs
leetcode
/
problems
/
src
/
stack
/
DecodedStringAtIndex.java
Copy path
File metadata and controls
68 lines (62 loc) · 1.71 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
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
""
;
}
}
Back
|
FazBrowse Home
|
New Git URL