Java short recursive solution


  • 0
    W

    public class Solution {
    private int point = 0;

    public String decodeString(String s) {
        StringBuilder sb = new StringBuilder();
        int times = 0;
        while (point < s.length()) {
            char c = s.charAt(point);
            point++;
            if (c >= '0' && c <= '9') {
                times = times * 10 + c - '0';
            } else if (c == '[') {
                String t = decodeString(s);
                for (int i = 0; i < times; i++) sb.append(t);
                times = 0;
            } else if (c == ']') {
                return sb.toString();
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.