-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
51 lines (50 loc) · 1.46 KB
/
Copy pathsolution.java
File metadata and controls
51 lines (50 loc) · 1.46 KB
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
// 394. Decode String
// https://leetcode.com/problems/decode-string/
// Medium | Java | Accepted 2026-08-25
// Runtime 0 ms | Memory 43.1 MB
class Solution {
public String decodeString(String s) {
StringBuilder ans = new StringBuilder();
int ind = 0;
while(ind<s.length())
{
if(Character.isDigit(s.charAt(ind)))
{
int temp1 = ind+1;
while(Character.isDigit(s.charAt(temp1)))
{
temp1++;
}
int num = Integer.parseInt(s.substring(ind, temp1));
ind = temp1+1;
int temp = temp1+2;
int count = 1;
while(s.charAt(temp)!=']' || count!=0)
{
if(s.charAt(temp)=='[')
{
count++;
}
if(s.charAt(temp)==']')
{
count--;
}
if(count==0)
{
break;
}
temp++;
}
String res = decodeString(s.substring(ind, temp));
ans.repeat(res, num);
ind = temp+1;
}
else
{
ans.append(s.charAt(ind));
ind++;
}
}
return ans.toString();
}
}