
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Decode String in C++
Suppose we have an encoded string; we have to return its decoded string. The rule for encoding is: k[encoded_string], this indicates where the encoded_string inside the square brackets is being repeated exactly k times. We can assume that the original data does not contain any numeric characters and that digits are only for those repeat numbers, k. So if the input is like “1[ba]2[na]”, then the output will be “banana”.
To solve this, we will follow these steps −
- create one empty stack, set i := 0
- while i < size of a string
- if s[i] is ‘]’
- res := delete element from the stack and take only the string that is inside the square brackets.
- n := 0
- while stack is not empty, and stack top is one numeric character, then accommodate the numbers and form the actual integer as n
- for j in range 1 to n
- for x in range 0 to size of res
- insert res[x] into the stack
- for x in range 0 to size of res
- otherwise insert s[i] into the stack
- increase i by 1
- if s[i] is ‘]’
- ans := an empty string
- while stack is not empty
- ans := stack top element + ans
- pop from stack
- return ans
Example(C++):
Let us see the following implementation to get a better understanding −
#include <bits/stdc++.h> using namespace std; class Solution { public: string decodeString(string s) { stack <char> st; int i = 0; while(i<s.size()){ if(s[i] == ']'){ string res = ""; while(st.top()!='['){ res = st.top() + res; st.pop(); } st.pop(); int n = 0; int x = 1; while(!st.empty() && st.top()>='0' && st.top()<='9'){ n = n + (st.top()-'0')*x; x*=10; st.pop(); } for(int j = 1; j <= n; j++){ for(int x = 0; x < res.size();x++){ st.push(res[x]); } } } else{ st.push(s[i]); } i++; } string ans =""; while(!st.empty()){ ans = st.top() + ans; st.pop(); } return ans; } }; main(){ Solution ob; cout << ob.decodeString("1[ba]2[na]"); }
Input
"1[ba]2[na]"
Output
"banana"
- Related Articles
- How to decode the string in android?
- Function to decode a string in JavaScript
- How to decode an encoded string in JavaScript?
- Java Program to decode string to integer
- To decode string array values that is already encoded in Numpy
- Decode Ways in Python
- Program to decode a run-length form of string into normal form in Python
- Decode Ways II in C++
- Decode Your Nightmares
- Encode and Decode Strings in C++
- Arduino – base64 encode and decode
- Program to recover decode XORed array in Python
- Program to decode a given message in C++
- How to decode JSON into objects in Golang?
- Program to find decode XORed permutation in Python

Advertisements