[LC] 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".

Solution 1: Use 2 stacks
class Solution:
    def decodeString(self, s: str) -> str:
        char_stack = []
        num_stack = []
        res, num, index = ‘‘, 0, 0
        while index < len(s):
            char = s[index]
            if char.isdigit():
                num = 0
                while index < len(s) and s[index].isdigit():
                    num = 10 * num + int(s[index])
                    index += 1
                num_stack.append(num)
            elif char == ‘[‘:
                char_stack.append(res)
                res = ‘‘
                index += 1
            elif char == ‘]‘:
                cur_char_lst = list(char_stack.pop())
                times = num_stack.pop()
                for _ in range(times):
                    cur_char_lst.append(res)
                res = ‘‘.join(cur_char_lst)
                index += 1
            else:
                res += char
                index += 1
        return res

solution 2: Use 1 stack

class Solution:
    def decodeString(self, s: str) -> str:
        stack = []
        cur_string, cur_num = ‘‘, 0
        for c in s:
            if c.isdigit():
                cur_num = 10 * cur_num + int(c)
            elif c == ‘[‘:
                stack.append(cur_string)
                stack.append(cur_num)
                cur_string = ‘‘
                cur_num = 0
            elif c == ‘]‘:
                prev_num = stack.pop()
                prev_string = stack.pop()
                cur_string = prev_string + prev_num * cur_string
            else:
                cur_string += c
        return cur_string

原文地址:https://www.cnblogs.com/xuanlu/p/11797621.html

时间: 2024-10-11 06:22:48

[LC] 394. Decode String的相关文章

394. Decode String 解码icc字符串3[i2[c]]

[抄题]: Given an encoded string, return it's 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 ass

394. Decode String

Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note thatk is guaranteed to be a positive integer. You may assume tha

Leetcode 394: Decode String

Given an encoded string, return it's 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 th

Python 解LeetCode:394 Decode String

题目描述:按照规定,把字符串解码,具体示例见题目链接 思路:使用两个栈分别存储数字和字母 注意1: 数字是多位的话,要处理后入数字栈 注意2: 出栈时过程中产生的组合后的字符串要继续入字母栈 注意3: 记得字母出栈的时候字符要逆序组合成字符串 注意4: 不用字符串而用字母栈的原因是字符串的 join 效率会比字符串加法高一些 结果: 30 ms, beat 98.02% 缺点:判断是数字那里有点代码不简洁,可以把 j 挪到循环外面的 class Solution(object): def dec

[Swift]LeetCode394. 字符串解码 | Decode String

Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where the encoded_stringinside the square brackets is being repeated exactly k times. Note that kis guaranteed to be a positive integer. You may assume that

[LC] 91. Decode Ways

A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given a non-empty string containing only digits, determine the total number of ways to decode it. Example 1: Input: &qu

LeetCode-394. Decode String(DFS)

Given an encoded string, return it's 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 th

[LeetCode] Decode String 解码字符串

Given an encoded string, return it's 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 th

LC 988. Smallest String Starting From Leaf

Given the root of a binary tree, each node has a value from 0 to 25 representing the letters 'a' to 'z': a value of 0 represents 'a', a value of 1represents 'b', and so on. Find the lexicographically smallest string that starts at a leaf of this tree