Given string str of the type “3(ab)4(cd)”, the task is to expand it to “abababcdcdcdcd” where integers are from the range [1, 9].
This problem was asked in ThoughtWorks interview held in October 2018.
Examples:
Input: str = “3(ab)4(cd)”
Output: abababcdcdcdcd
Input: str = “2(kl)3(ap)”
Output: klklapapap
Approach: We traverse through the string and wait for a numeric value, num to turn up at position i. As soon as it arrives, we check i + 1 for a ‘(‘. If it’s present, then the program enters into a loop to extract whatever is within ‘(‘ and ‘)’ and concatenate it to an empty string, temp. Later, another loop prints the generated string num number of times. Repeat these steps until the string finishes.
Below is the implementation of the approach:
C++
#include<bits/stdc++.h>
using namespace std;
void expandString(string strin)
{
string temp = "" ;
int j;
for ( int i = 0; i < strin.length(); i++)
{
if (strin[i] >= 0)
{
int num = strin[i] - '0' ;
if (strin[i + 1] == '(' )
{
for (j = i + 1; strin[j] != ')' ; j++)
{
if ((strin[j] >= 'a' && strin[j] <= 'z' ) ||
(strin[j] >= 'A' && strin[j] <= 'Z' ))
{
temp += strin[j];
}
}
for ( int k = 1; k <= num; k++)
{
cout << (temp);
}
num = 0;
temp = "" ;
if (j < strin.length())
{
i = j;
}
}
}
}
}
int main()
{
string strin = "3(ab)4(cd)" ;
expandString(strin);
}
|
Java
public class GFG {
static void expandString(String strin)
{
String temp = "" ;
int j;
for ( int i = 0 ; i < strin.length(); i++) {
if (strin.charAt(i) >= 0 ) {
int num = strin.charAt(i) - '0' ;
if (strin.charAt(i + 1 ) == '(' ) {
for (j = i + 1 ; strin.charAt(j) != ')' ; j++) {
if ((strin.charAt(j) >= 'a'
&& strin.charAt(j) <= 'z' )
|| (strin.charAt(j) >= 'A'
&& strin.charAt(j) <= 'Z' )) {
temp += strin.charAt(j);
}
}
for ( int k = 1 ; k <= num; k++) {
System.out.print(temp);
}
num = 0 ;
temp = "" ;
if (j < strin.length()) {
i = j;
}
}
}
}
}
public static void main(String args[])
{
String strin = "3(ab)4(cd)" ;
expandString(strin);
}
}
|
Python3
def expandString(strin):
temp = ""
j = 0
i = 0
while (i < len (strin)):
if (strin[i] > = "0" ):
num = ord (strin[i]) - ord ( "0" )
if (strin[i + 1 ] = = '(' ):
j = i + 1
while (strin[j] ! = ')' ):
if ((strin[j] > = 'a' and strin[j] < = 'z' ) or \
(strin[j] > = 'A' and strin[j] < = 'Z' )):
temp + = strin[j]
j + = 1
for k in range ( 1 , num + 1 ):
print (temp,end = "")
num = 0
temp = ""
if (j < len (strin)):
i = j
i + = 1
strin = "3(ab)4(cd)"
expandString(strin)
|
C#
using System;
class GFG{
static void expandString( string strin)
{
string temp = "" ;
int j;
for ( int i = 0;
i < strin.Length; i++)
{
if (strin[i] >= 0)
{
int num = strin[i] - '0' ;
if (strin[i + 1] == '(' )
{
for (j = i + 1;
strin[j] != ')' ; j++)
{
if ((strin[j] >= 'a' &&
strin[j] <= 'z' ) ||
(strin[j] >= 'A' &&
strin[j] <= 'Z' ))
{
temp += strin[j];
}
}
for ( int k = 1; k <= num; k++)
{
Console.Write(temp);
}
num = 0;
temp = "" ;
if (j < strin.Length)
{
i = j;
}
}
}
}
}
public static void Main(String [] args)
{
string strin = "3(ab)4(cd)" ;
expandString(strin);
}
}
|
Javascript
<script>
function expandString(strin)
{
let temp = "" ;
let j;
for (let i = 0; i < strin.length; i++) {
if (strin[i].charCodeAt(0) >= 0) {
let num = strin[i].charCodeAt(0) - '0' .charCodeAt(0);
if (strin[i+1] == '(' ) {
for (j = i + 1; strin[j] != ')' ; j++) {
if ((strin[j] >= 'a'
&& strin[j] <= 'z' )
|| (strin[j] >= 'A'
&& strin[j] <= 'Z' )) {
temp += strin[j];
}
}
for (let k = 1; k <= num; k++) {
document.write(temp);
}
num = 0;
temp = "" ;
if (j < strin.length) {
i = j;
}
}
}
}
}
let strin = "3(ab)4(cd)" ;
expandString(strin);
</script>
|
Time Complexity: O(N*N)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
18 Aug, 2021
Like Article
Save Article