from typing import *
def decode_brackets(s: str) -> str:
stack: List[str] = []
current_num = 0
result = ""
for ch in s:
if ch.isdigit():
current_num = current_num * 10 + int(ch)
elif ch == "[":
stack.append(result)
stack.append(current_num)
result = ""
current_num = 0
elif ch == "]":
num = stack.pop()
prev = stack.pop()
result = prev + num * result
else:
result += ch
return result
#include <string>
#include <vector>
#include <cctype>
using namespace std;
string decodeBrackets(string s) {
vector<string> stack;
int currentNum = 0;
string result = "";
for (char ch : s) {
if (isdigit(ch)) {
currentNum = currentNum * 10 + (ch - '0');
} else if (ch == '[') {
stack.push_back(result);
stack.push_back(to_string(currentNum));
result = "";
currentNum = 0;
} else if (ch == ']') {
int num = stoi(stack.back());
stack.pop_back();
string prev = stack.back();
stack.pop_back();
string tempResult = "";
for (int i = 0; i < num; ++i) {
tempResult += result;
}
result = prev + tempResult;
} else {
result += ch;
}
}
return result;
}
package main
func decodeBrackets(s string) string {
var stack []interface{}
currentNum := 0
result := ""
for _, ch := range s {
if ch >= '0' && ch <= '9' {
currentNum = currentNum*10 + int(ch-'0')
} else if ch == '[' {
stack = append(stack, result)
stack = append(stack, currentNum)
result = ""
currentNum = 0
} else if ch == ']' {
num := stack[len(stack)-1].(int)
stack = stack[:len(stack)-1]
prev := stack[len(stack)-1].(string)
stack = stack[:len(stack)-1]
result = prev + stringRepeat(result, num)
} else {
result += string(ch)
}
}
return result
}
func stringRepeat(s string, count int) string {
if count <= 0 {
return ""
}
result := ""
for i := 0; i < count; i++ {
result += s
}
return result
}
import java.util.*;
public class Solution {
public String decodeBrackets(String s) {
Stack<Object> stack = new Stack<>();
int currentNum = 0;
StringBuilder result = new StringBuilder();
for (char ch : s.toCharArray()) {
if (Character.isDigit(ch)) {
currentNum = currentNum * 10 + (ch - '0');
} else if (ch == '[') {
stack.push(result.toString());
stack.push(currentNum);
result = new StringBuilder();
currentNum = 0;
} else if (ch == ']') {
int num = (int) stack.pop();
String prev = (String) stack.pop();
result = new StringBuilder(prev + result.toString().repeat(num));
} else {
result.append(ch);
}
}
return result.toString();
}
}
from typing import *
def decode_brackets(s: str) -> str:
stack: List[str] = []
current_num = 0
result = ""
for ch in s:
if ch.isdigit():
current_num = current_num * 10 + int(ch)
elif ch == "[":
stack.append(result)
stack.append(current_num)
result = ""
current_num = 0
elif ch == "]":
num = stack.pop()
prev = stack.pop()
result = prev + num * result
else:
result += ch
return result
/**
* @param {string} s
* @returns {string}
*/
export function decodeBrackets(s) {
const stack = [];
let currentNum = 0;
let result = "";
for (let ch of s) {
if (!isNaN(ch)) {
currentNum = currentNum * 10 + Number(ch);
} else if (ch === "[") {
stack.push(result);
stack.push(currentNum);
result = "";
currentNum = 0;
} else if (ch === "]") {
const num = stack.pop();
const prev = stack.pop();
result = prev + result.repeat(num);
} else {
result += ch;
}
}
return result;
}