简体   繁体   English

Java - 如何递归解码字符串

[英]Java - How to decode a string recursively

I need to decode a string recursively encoded as count followed by substring我需要解码一个递归编码为计数的字符串,后跟子字符串

An encoded string (s) is given, the task is to decode it.给定一个编码字符串(s),任务是对其进行解码。 The pattern in which the strings are encoded is as follows.字符串编码的模式如下。

Examples:例子:

Input : str[] = "1[b]" Output : b输入:str[] = "1[b]" 输出:b

Input : str[] = "2[ab] Output : abab输入:str[] = "2[ab] 输出:abab

Input : str[] = "2[a2[b]]" Output : abbabb输入:str[] = "2[a2[b]]" 输出:abbabb

Input : str[] = "3[b2[ca]]" Output : bcacabcacabcaca输入:str[] = "3[b2[ca]]" 输出:bcacabcacabcaca

Below is the code I tried to achieve the same.下面是我试图实现相同的代码。 All I know is it can be solved using two stacks.我所知道的是它可以使用两个堆栈来解决。

public class Main {
    public static void main(String[] args) {
        Stack<Interger> s1 = new Stack();
        Stack<String> s2 = new Stack();
        String result = "";
        for(int i = 0; i < args.length; i++){
            if(Interger.parseInt(args[i]) == 0){
                s1.push(args[i]);
            }
            if(args[i] == 0){
                if(args[i] == ']'){
                   result = s2.pop();
                }
                if(args[i] == '['){
                    continue;
                }
                s2.push(args[i])
            }
        }
    }
}

Can anyone help me what is the efficient way to write code in order to get the expected output?谁能帮助我编写代码以获得预期输出的有效方法是什么?

How to decode a string recursively如何递归解码字符串

You need to define a base case and recursive case :您需要定义基本案例递归案例

  • Base case - the given string doesn't contain square brackets [] , therefore no need to process it.基本情况- 给定的字符串不包含方括号[] ,因此无需处理它。 The return value is the given string itself.返回值是给定的字符串本身。

  • Recursive case - we need to determine the indices of opening [ and closing brackets ] and based on that contract a new string.递归案例- 我们需要确定开始[和结束括号]的索引,并基于该合同一个新的字符串。

That's how it could be implemented:这就是它的实现方式:

public static String decode(String str) {
    int startOfBrackets = str.indexOf('[');
    
    if (startOfBrackets == -1) { // base case there's no square brackets in the string
        return str;
    }

    int startOfDigits = getFirstDigitsPosition(str);
    int repeatNum = Integer.parseInt(str.substring(startOfDigits, startOfBrackets));
    
    return str.substring(0, startOfDigits) +
        decode(str.substring(startOfBrackets + 1, str.length() - 1)).repeat(repeatNum);
}

public static final Pattern SINGLE_DIGIT = Pattern.compile("\\d");

public static int getFirstDigitsPosition(String str) {
    Matcher matcher = SINGLE_DIGIT.matcher(str);
    if (matcher.find()) {
        return matcher.start();
    }
    return -1;
}

main()

public static void main(String[] args) {
    System.out.println(decode("1[b]"));
    System.out.println(decode("2[ab]"));
    System.out.println(decode("2[a2[b]]"));
    System.out.println(decode("3[b2[ca]]"));
}

Output:输出:

abab
abbabb
bcacabcacabcaca

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM