Leetcode
  • Leetcode Questions
  • Runtime Screenshots
  • DataStructure
    • Leetcode 394. Decode String
    • Leetcode 225. Implement Stack Using Queues
    • Leetcode 336. Palindrome Pairs
    • Leetcode 316. Remove Duplicate Letters
    • Leetcode 206. Reverse Linked List
    • Leetcode 347. Top K Frequent Elements
    • Leetcode 227. Basic Calculator II
    • Leetcode 224. Basic Calculator
  • Linear
    • Leetcode 23. Merge k Sorted Lists
    • Leetcode 48. Rotate Image
    • Leetcode 6. ZigZag Conversion
    • Leetcode 438. Find All Anagrams in a String
    • Leetcode 189. Rotate Array
    • LeetCode 56. Merge Intervals
    • Leetcode 4. Median of Two Sorted Array
    • Leetcode 3. Longest Substring Without Repeating Characters
    • Leetcode 8. String to Integer (atoi)
    • Leetcode 5. Longest Palindromic Substring
    • Leetcode 11. Container With Most Water
  • Tree
    • Leetcode 103. Binary Tree Zigzag Level Order Traversal
    • Leetcode 508. Most Frequent Subtree Sum
    • Leetcode 226. Invert Binary Tree
    • Leetcode 222. Count Complete Tree Nodes
    • Leetcode 250. Count Univalue Subtrees
    • Leetcode 285. Inorder Successor in BST
    • Leetcode 230. Kth Smallest Element in a BST
    • Leetcode 543. Diameter of Binary Tree
    • Leetcode 199. Binary Tree Right Side View
  • Math
    • Leetcode 50. Power(x, n)
    • Leetcode 166. Fraction to Recurring Decimal
    • Leetcode 7. Reverse Integer
    • Leetcode 360. Sort Transformed Array
    • Leetcode 367. Valid Perfect Square
    • Leetcode 12. Integer to Roman
  • DynamicProgramming
    • Leetcode 10. Regular Expression Matching
    • Leetcode 253. Meeting Rooms II
    • Leetcode 303. Range Sum Query
    • Leetcode 22. Generate Parentheses
  • Graph
    • Leetcode 142. Linked List Cycle II
    • Leetcode 261. Graph Valid Tree
    • Leetcode 339. Nested List Weight Sum
    • Leetcode 207. Course Schedule
  • OODesign
    • Leetcode 295. Find Media From Data Stream
Powered by GitBook
On this page
  • 题目
  • 思路
  • 解答
  • Complexity Analysis
  • 拓展
  • 总结
  • Reference

Was this helpful?

  1. DataStructure

Leetcode 394. Decode String

题目

Given an encoded string, return its decoded string.

The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer.

You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc.

Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there won't be input like 3a or 2[4].

Examples:

s = "3[a]2[bc]", return "aaabcbc".
s = "3[a2[c]]", return "accaccacc".
s = "2[abc]3[cd]ef", return "abcabccdcdcdef".

思路

这道题目与之前两道224和227 Basic Calculator非常类似,我们需要借助两个Stack来完成string evaluation,只不过规则稍有不同。首先,我们还是需要遇见digit char的时候用循环的方式构造multipler k。当遇见'['时,我们知道目前的multiplier已经构造完成,因此我们可以将它放进kstack中保存起来。当遇见']'时,我们知道当前括号中的str已经构造完成,我们可以依据kstack中的multipler k来将str重复k次。

这题比较困难的在于处理两个'['中间的值,比如2[a3[b]]里面的a。为了能让3[b]所展开的string正确地接着a,我们需要将a保存在strstack中(这类似于计算器题目中的保存中间结果的stack)。我们可以将strstack理解为保存括号层级的一种方式,每往下加深一次括号嵌套,我们就会先往strstack中保存当前层级的中间str结果。

解答

class Solution {
    public String decodeString(String s) {

        Stack<Integer> kstack = new Stack<Integer>(); // stores the multiplier
        Stack<String> strstack = new Stack<String>(); // stores all intermediate str
        String str = ""; // result string
        int k = 0; // multipler before each number

        for (char ch : s.toCharArray()) {

            // Construct the multipler k
            if (Character.isDigit(ch)) {
                k = k * 10 + Character.getNumericValue(ch);
                continue;
            }

            switch(ch) {

                case '[': // end of multipler
                    kstack.push(k);
                    strstack.push(str);
                    k = 0;
                    str = "";
                    break;

                case ']': // end of current str
                    int repeatedTimes = kstack.pop();
                    StringBuilder prev_res = new StringBuilder(strstack.pop());
                    for (int i = 0; i < repeatedTimes; i++) {
                        prev_res.append(str);
                    }
                    str = prev_res.toString();
                    break;

                default:  // normal char
                    str += ch;
                    break;
            }
        }

        return str;
    }
}

Complexity Analysis

  • Time Complexity: O(nk). 我们会遍历input string中的所有字母,不过重复sub string的时候平均会重复k次。

  • Space Complexity: O(n). 我们需要用两个stack来分别存储k值和中间字符串结果。

拓展

这道题能通过递归来解答吗?与循环解答相比,时间空间复杂度是多少?

总结

这道题与两道Calculator的题本质上是一样的:我们需要借助stack保存中间结果、调整运算顺序。在解答这些题时,我们需要格外小心处理各种edge case,比如这道题目中重置str字符串、push to stack的位置和条件。

Reference

PreviousDataStructureNextLeetcode 225. Implement Stack Using Queues

Last updated 5 years ago

Was this helpful?

sampsonchan's solution