通过重复字符串,次数与之前的数字相同,来构建一个字符串。

huangapple go评论76阅读模式
英文:

How can I build a string by repeating it as many as the number before it?

问题

I need code that converts a string into the following form:
'3[a]2[bc]'
to the following figure:
'aaabcbc'

I have created the following code:

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 an 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 elements every time.
        let num = ArrStr[i]; //variable for numbers.
        let str = ArrStr[i+1]; //variable for strings.
        for(let e = 1; e <= num; e++){ //this loop puts every string in the empty array that is called result with the required number.
            result.push(str);
        }
    }

    let finalresult = result.toString().replaceAll('&#39;,&#39;', '&#39;&#39;'); //Convert the array (result) to a string and delete the comma between each element.
    return finalresult; //Returns the final result
}

But it does not work in the following cases:
'2[abc]3[cd]ef'
'3[a2[c]]'

I'm a beginner, and I've been trying for three days to solve this problem, and I haven't found any suitable idea, any help?

I want this result:

Input: &#39;2[abc]3[cd]ef&#39;
Output: &#39;abcabccdcdcdef&#39;

Input: &#39;3[a2[c]]&#39;
Output: &#39;accaccacc&#39;

英文:

I need code that converts a string into the following form:
&#39;3[a]2[bc]&#39;
to the following figure:
&#39;aaabcbc&#39;

I have created the following code:

function string_builder(expression) {
    let string = expression; //I put the text string into a variable called string.
    let splitString = string.replaceAll(&#39;[&#39;, &#39;,&#39;).replaceAll(&#39;]&#39;, &#39;,&#39;) .split(&#39;,&#39;); //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 &lt; 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 &lt;= 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(&#39;,&#39;, &#39;&#39;); //Convert the array (result) to a string and delete the comma between each element.
    return finalresult; //Returns the final result

}

But it does not work in the following cases:
'2[abc]3[cd]ef'
'3[a2[c]]'

I'm a beginner, and I've been trying for three days to solve this problem, and I haven't found any suitable idea, any help?

I want this result:

Input: &#39;2[abc]3[cd]ef&#39;
Output: &#39;abcabccdcdcdef&#39;

Input: &#39;3[a2[c]]&#39;
Output: &#39;accaccacc&#39;

答案1

得分: 0

以下是代码的翻译部分:

function decodeString(s) {
  // 创建一个空栈以跟踪字符
  const stack = [];
  // 遍历输入字符串中的每个字符
  for (let i = 0; i &lt; s.length; i++) {
    // 如果字符是闭合括号,我们需要解码嵌套的模式
    if (s[i] === &#39;]&#39;) {
      // 创建一个空字符串以存储嵌套模式中的解码字符串
      let str = &#39;&#39;;
      // 从栈中弹出字符,直到达到相应的开放括号
      while (stack[stack.length - 1] !== &#39;[&#39;) {
        // 将弹出的字符连接到解码字符串上
        str = stack.pop() + str;
      }
      // 从栈中弹出开放括号
      stack.pop();
      // 创建一个空字符串以存储重复次数
      let count = &#39;&#39;;
      // 从栈中弹出字符,直到达到数字
      while (stack.length &gt; 0 &amp;&amp; !isNaN(stack[stack.length - 1])) {
        // 将弹出的字符连接到重复次数上
        count = stack.pop() + count;
      }
      // 使用parseInt将重复次数转换为整数
      count = parseInt(count);
      // 将重复的解码字符串推回栈中
      stack.push(str.repeat(count));
    } else {
      // 如果字符不是闭合括号,将其推送到栈中
      stack.push(s[i]);
    }
  }
  // 将栈上剩余的字符连接在一起以形成最终的解码字符串
  return stack.join(&#39;&#39;);
}

// 使用输入字符串“2[abc]3[cd]ef”测试函数
const input = &quot;2[abc]3[cd]ef&quot;;
const output = decodeString(input); 
console.log(output)  //abcabccdcdcdef

希望这对您有所帮助。

英文:

Try this approach using the stack data structure to keep track of the nested patterns in the input string. Should work.

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 &lt; s.length; i++) {
// If the character is a closing bracket, we need to decode a nested pattern
if (s[i] === &#39;]&#39;) {
// Create an empty string to store the decoded string inside the nested pattern
let str = &#39;&#39;;
// Pop characters off the stack until we reach the corresponding opening bracket
while (stack[stack.length - 1] !== &#39;[&#39;) {
// 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 = &#39;&#39;;
// Pop characters off the stack until we reach a digit
while (stack.length &gt; 0 &amp;&amp; !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(&#39;&#39;);
}
// Test the function with the input string &quot;2[abc]3[cd]ef&quot;
const input = &quot;2[abc]3[cd]ef&quot;;
const output = decodeString(input); 
console.log(output)  //abcabccdcdcdef

huangapple
  • 本文由 发表于 2023年3月7日 21:20:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75662504.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定