0% found this document useful (0 votes)
6 views2 pages

Java String Decoder Implementation

The provided Java code defines a class 'Decodificador' that decodes a given encoded string based on a specific format using stacks. It processes characters to handle digits, opening brackets, and closing brackets, constructing the decoded string accordingly. The main method includes test cases demonstrating the functionality of the decoding method.

Uploaded by

202206345
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Java String Decoder Implementation

The provided Java code defines a class 'Decodificador' that decodes a given encoded string based on a specific format using stacks. It processes characters to handle digits, opening brackets, and closing brackets, constructing the decoded string accordingly. The main method includes test cases demonstrating the functionality of the decoding method.

Uploaded by

202206345
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

import [Link].

Stack;

public class Decodificador {

public String decodificar(String codificado) {

Stack<Integer> numStack = new Stack<>();

Stack<String> strStack = new Stack<>();

StringBuilder currentStr = new StringBuilder();

int num = 0;

for (char c : [Link]()) {

if ([Link](c)) {

num = num * 10 + (c - '0');

} else if (c == '[') {

[Link](num);

[Link]([Link]());

currentStr = new StringBuilder();

num = 0;

} else if (c == ']') {

int repeatTimes = [Link]();

StringBuilder temp = new StringBuilder([Link]());

for (int i = 0; i < repeatTimes; i++) {

[Link](currentStr);

currentStr = temp;

} else {

[Link](c);

return [Link]();

}
// Ejemplos de prueba

public static void main(String[] args) {

Decodificador dec = new Decodificador();

[Link]([Link]("3[a]3[b]3[c]")); // aaabbbccc

[Link]([Link]("1[4]2[]1[2+]1[(]2[-]1[5]1[]")); // 4*2+(--5)

[Link]([Link]("2[java]2[g]2[>]1[>]")); // javajavagg>>>

[Link]([Link]("2[lorem ]2[ipsum ]1[dolor]")); // lorem lorem ipsum ipsum


dolor

You might also like