@@ -32,3 +32,53 @@ Output: "abcabccdcdcdef"
3232- Stack
3333- Recursion
3434
35+ ## Solution
36+
37+ The essence of this approach lies in recognizing that the problem is naturally suited for a stack-based solution. When
38+ decoding a string, we need to handle nested and sequential patterns, and a stack allows us to manage these layers
39+ effectively. By pushing and popping elements on the stack as we encounter brackets, we can keep track of the substrings
40+ and their repeat counts. This method ensures we decode the string from the innermost nested structure outward,
41+ maintaining the correct order and repetition. To achieve this, we process each character in the string in the following
42+ way:
43+
44+ - ** Digits** : Represent how many times a substring is repeated.
45+ - ** Open bracket ([ )** : Marks the start of a new substring and saves the current state by pushing the current substring
46+ and repeat count onto stacks and then resetting them for a new segment.
47+ - ** Close bracket (] )** : Ends the current substring, repeats it as specified, and appends it to the previous substring.
48+ - ** Letters** : Build the current substring being processed.
49+
50+ This solution handles nested and consecutive encoding strings by utilizing the stack to track previously processed
51+ substrings and their associated repeat counts. The final decoded string is constructed by combining all parts accumulated
52+ in the stacks.
53+
54+ Here’s the step-by-step implementation of the solution:
55+
56+ - Initialize two empty stacks: count_stack for storing repeat counts and string_stack for storing intermediate strings.
57+ - Initialize current as an empty string. This will hold the currently decoded string segment.
58+ - Initialize k as 0: This will store multi-digit numbers representing repeat counts.
59+ - Iterate through each character char in the input string s:
60+ - If char is a digit:
61+ - Update k to accumulate the digit into a multi-digit number by performing k = 10 * k + int(char).
62+ - If char is a '[ ':
63+ - Push k onto count_stack to save the current repeat count.
64+ - Push current onto string_stack to save the current string segment.
65+ - Reset k to 0 for the next repeat count.
66+ - Reset current to an empty string to start decoding the new segment.
67+ - If char is a’] ’:
68+ - Pop the last string segment from string_stack and store it in decoded_string.
69+ - Pop the last repeat count from count_stack and store it in num.
70+ - Update current by appending current * num to decoded_string, repeating the decoded segment, and combining it with
71+ the previous string.
72+ - If char is a regular character:
73+ - Append char to current, adding it to the currently decoded string segment.
74+ - After processing all characters, current will contain the fully decoded string.
75+
76+ ### Time Complexity
77+
78+ The time complexity of this solution is O(n∗m∗k), where n is the length of the input string, m is the length of current,
79+ and k is the maximum multiplier encountered during the decoding.
80+
81+ ### Space Complexity
82+
83+ The space complexity of this solution is O(c+d), where c is the number of alphabets and d is the number of digits in the
84+ input string.
0 commit comments