Expand the string according to the given conditions

Given a 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 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include<bits/stdc++.h>
using namespace std;
  
// Function to expand and print the given string
void expandString(string strin)
{
    string temp = "";
    int j;
  
    for (int i = 0; i < strin.length(); i++) 
    {
        if (strin[i] >= 0) 
        {
  
            // Subtract '0' to convert char to int
            int num = strin[i] - '0';
            if (strin[i + 1] == '('
            {
  
                // Characters within brackets
                for (j = i + 1; strin[j] != ')'; j++)
                {
                    if ((strin[j] >= 'a' && strin[j] <= 'z') || 
                        (strin[j] >= 'A' && strin[j] <= 'Z'))
                    {
                        temp += strin[j];
                    }
                }
  
                // Expanding
                for (int k = 1; k <= num; k++) 
                {
                    cout << (temp);
                }
  
                // Reset the variables
                num = 0;
                temp = "";
  
                if (j < strin.length()) 
                {
                    i = j;
                }
            }
        }
    }
}
  
// Driver code
int main()
{
    string strin = "3(ab)4(cd)";
    expandString(strin);
}
  
// This code is contributed by Surendra_Gangwar

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
public class GFG {
  
    // Function to expand and print the given string
    static void expandString(String strin)
    {
        String temp = "";
        int j;
  
        for (int i = 0; i < strin.length(); i++) {
            if (strin.charAt(i) >= 0) {
  
                // Subtract '0' to convert char to int
                int num = strin.charAt(i) - '0';
                if (strin.charAt(i + 1) == '(') {
  
                    // Characters within brackets
                    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);
                        }
                    }
  
                    // Expanding
                    for (int k = 1; k <= num; k++) {
                        System.out.print(temp);
                    }
  
                    // Reset the variables
                    num = 0;
                    temp = "";
  
                    if (j < strin.length()) {
                        i = j;
                    }
                }
            }
        }
    }
  
    // Driver code
    public static void main(String args[])
    {
        String strin = "3(ab)4(cd)";
        expandString(strin);
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function to expand and print the given string
def expandString(strin):
      
    temp = ""
    j = 0
    i = 0
    while(i < len(strin)):
        if (strin[i] >= "0"):
              
            # Subtract '0' to convert char to int
            num = ord(strin[i])-ord("0")
            if (strin[i + 1] == '('):
                  
                # Characters within brackets
                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
                      
                # Expanding
                for k in range(1, num + 1):
                    print(temp,end="")
                      
                # Reset the variables
                num = 0
                temp = ""
                if (j < len(strin)):
                    i = j
        i += 1
  
# Driver code
strin = "3(ab)4(cd)"
expandString(strin)
  
# This code is contributed by shubhamsingh10

chevron_right


Output:

abababcdcdcdcd

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.