我需要将字符串转换为以下形式的代码:
'3[a]2[bc]'
到下图:
'aaabcbc'
我创建了以下代码:
function string_builder(expression) {
let string = expression; //I put the text string into a variable called string.
let splitString = string.replaceAll('[', ',').replaceAll(']', ',') .split(','); //Replace [and] with , then convert it to an array using that comma.
let ArrStr = splitString.slice(0,-1); //Delete the last element in the array which is just an empty element.
let result = []; //Just empty array inside a variable called result.
for(let i = 0; i < ArrStr.length; i+=2){ //this loop will take only the numbers by jumping two element every time.
let num = ArrStr[i]; //variable for numbers.
let str = ArrStr[i+1]; //variable for stings.
for(let e = 1; e <= num; e++){ //this loop put every sting in the empty array that called result with the required number.
result.push(str);
}
}
let finalresult = result.toString().replaceAll(',', ''); //Convert the array (result) to a string and delete the comma between each element.
return finalresult; //Returns the final result
}
但在以下情况下不起作用: '2[abc]3[cd]ef' '3[a2[c]]'
我是初学者,我已经尝试了三天来解决这个问题,但我没有找到任何合适的想法,有帮助吗?
我想要这个结果:
输入:
'2[abc]3[cd]ef'
输出:'abcabccdcdcdef'
输入:
'3[a2[c]]'
输出:'accaccacc'
您当前实施的问题是它仅适用于没有嵌套括号的简单情况。要处理嵌套的括号,您需要使用递归。这是一个更新的实现:
function string_builder(expression) {
const stack = [];
let num = 0;
let str = '';
for (const c of expression) {
if (c === '[') {
stack.push(str);
stack.push(num);
str = '';
num = 0;
} else if (c === ']') {
const prevNum = stack.pop();
const prevStr = stack.pop();
str = prevStr + str.repeat(prevNum);
} else if (c >= '0' && c <= '9') {
num = num * 10 + parseInt(c);
} else {
str += c;
}
}
console.log( str);
}
string_builder('2[abc]3[cd]ef');
string_builder('3[a2[c]]');
这是它的工作原理:
尝试这种使用堆栈数据结构的方法来跟踪输入字符串中的嵌套模式。应该工作。
function decodeString(s) {
// Create an empty stack to keep track of characters
const stack = [];
// Iterate over each character in the input string
for (let i = 0; i < s.length; i++) {
// If the character is a closing bracket, we need to decode a nested pattern
if (s[i] === ']') {
// Create an empty string to store the decoded string inside the nested pattern
let str = '';
// Pop characters off the stack until we reach the corresponding opening bracket
while (stack[stack.length - 1] !== '[') {
// Concatenate the popped characters to the decoded string
str = stack.pop() + str;
}
// Pop the opening bracket off the stack
stack.pop();
// Create an empty string to store the repeat count
let count = '';
// Pop characters off the stack until we reach a digit
while (stack.length > 0 && !isNaN(stack[stack.length - 1])) {
// Concatenate the popped characters to the repeat count
count = stack.pop() + count;
}
// Convert the repeat count to an integer using parseInt
count = parseInt(count);
// Push the repeated decoded string back onto the stack
stack.push(str.repeat(count));
} else {
// If the character is not a closing bracket, push it onto the stack
stack.push(s[i]);
}
}
// Join the characters remaining on the stack to form the final decoded string
return stack.join('');
}
// Test the function with the input string "2[abc]3[cd]ef"
const input = "2[abc]3[cd]ef";
const output = decodeString(input);
console.log(output) //abcabccdcdcdef