Repeat substrings of the given String required number of times | Set 2 (Recursion)
Last Updated :
18 Jan, 2022
Given string str, the task is to repeat every substring of the string X number of times where X is the number composed of the consecutive digits present just after the substring in the original string. For example, if str = “g1e2ks1” then the resultant string will be “geeks”.
Examples:
Input: str = “2a10bd3”
Output: aaaaaaaaaabdbdbd
Explanation: First digit “2” is unnecessary as there is no valid substring before it. Character “a” will be repeated 10 times and then “bd” will be repeated thrice.
Input: str = “g1e2ks1for1g1e2ks1”
Output: geeksforgeeks
An iterative approach to this problem has been discussed here. This article focuses on a recursive approach to solve the given problem.
Approach: Traverse the string character by character recursively. While traversing, maintain the characters in a separate string and the digits representing the number of repetitions in an integer. For every set of digits found, append the occurrences of the stored string according to the integer found into a final resultant string and recursively call for the remaining string.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string res = "" ;
void decode(string s, int i, string t, int x)
{
if (i == s.length()) {
for ( int i = 0; i < x; i++)
res = res + t;
return ;
}
if ( isdigit (s[i]) && !t.empty()) {
x = x * 10 + (s[i] - '0' );
}
if (! isdigit (s[i])) {
if (!t.empty() && x > 0) {
for ( int i = 0; i < x; i++)
res = res + t;
t = "" ;
x = 0;
}
t = t + s[i];
}
decode(s, i + 1, t, x);
}
string decodeString(string s)
{
decode(s, 0, "" , 0);
return res;
}
int main()
{
string str = "g1e2k1s1for1g1e2ks1" ;
cout << decodeString(str);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static String res = "" ;
static void decode(String s, int i, String t, int x)
{
if (i == s.length()) {
for ( int j = 0 ; j < x; j++)
res = res + t;
return ;
}
if (Character.isDigit(s.charAt(i)) && !t.isEmpty()) {
x = x * 10 + (s.charAt(i) - '0' );
}
if (!Character.isDigit(s.charAt(i))) {
if (!t.isEmpty() && x > 0 ) {
for ( int j = 0 ; j < x; j++)
res = res + t;
t = "" ;
x = 0 ;
}
t = t + s.charAt(i);
}
decode(s, i + 1 , t, x);
}
static String decodeString(String s)
{
decode(s, 0 , "" , 0 );
return res;
}
public static void main(String[] args)
{
String str = "g1e2k1s1for1g1e2ks1" ;
System.out.print(decodeString(str));
}
}
|
Python3
res = "";
def decode(s, i, t, x):
global res;
if (i = = len (s)):
for j in range (x):
res = res + t;
return ;
if (s[i].isdigit() and len (t)! = 0 ):
x = x * 10 + ( ord (s[i]) - ord ( '0' ));
if (s[i].isdigit() = = False ):
if ( len (t)! = 0 and x > 0 ):
for j in range (x):
res = res + t;
t = "";
x = 0 ;
t = t + s[i];
decode(s, i + 1 , t, x);
def decodeString(s):
decode(s, 0 , "", 0 );
return res;
if __name__ = = '__main__' :
str = "g1e2k1s1for1g1e2ks1" ;
print (decodeString( str ));
|
C#
using System;
public class GFG
{
static String res = "" ;
static void decode(String s, int i, String t, int x)
{
if (i == s.Length) {
for ( int j = 0; j < x; j++)
res = res + t;
return ;
}
if ( char .IsDigit(s[i]) && t.Length!=0) {
x = x * 10 + (s[i] - '0' );
}
if (! char .IsDigit(s[i])) {
if (t.Length!=0 && x > 0) {
for ( int j = 0; j < x; j++)
res = res + t;
t = "" ;
x = 0;
}
t = t + s[i];
}
decode(s, i + 1, t, x);
}
static String decodeString(String s)
{
decode(s, 0, "" , 0);
return res;
}
public static void Main(String[] args)
{
String str = "g1e2k1s1for1g1e2ks1" ;
Console.Write(decodeString(str));
}
}
|
Javascript
<script>
var res = "" ;
function decode(s, i, t, x)
{
if (i == s.length) {
for (let i = 0; i < x; i++)
res = res + t;
return ;
}
if (Number.isInteger(parseInt(s[i])) && t.length != 0) {
x = x * 10 + (s[i].charCodeAt(0) - '0' .charCodeAt(0));
}
if (!Number.isInteger(parseInt(s[i]))) {
if (!t.length == 0 && x > 0) {
for (let i = 0; i < x; i++)
res = res + t;
t = "" ;
x = 0;
}
t = t + s[i];
}
decode(s, i + 1, t, x);
}
function decodeString(s)
{
decode(s, 0, "" , 0);
return res;
}
let str = "g1e2k1s1for1g1e2ks1" ;
document.write(decodeString(str));
</script>
|
Time Complexity: O(M), where M is the sum of all integers present in the given string
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...