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.
Input: str = “3(ab)4(cd)”
Input: str = “2(kl)3(ap)”
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:
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.
- Java program to expand a String if range is given?
- Change the given string according to the given conditions
- Construct the Cypher string based on the given conditions
- Generate a string consisting of characters 'a' and 'b' that satisfy the given conditions
- Find the last remaining Character in the Binary String according to the given conditions
- Minimum number of substrings the given string can be splitted into that satisfy the given conditions
- Count of Binary Strings possible as per given conditions
- Count pairs of strings that satisfy the given conditions
- Calculate weight of parenthesis based on the given conditions
- Number of strings in two array satisfy the given conditions
- Count of ways to traverse a Matrix according to given conditions
- Split the array into equal sum parts according to given conditions
- Create a new string by alternately combining the characters of two halves of the string in reverse
- Longest palindromic string formed by concatenation of prefix and suffix of a string
- Minimal moves to form a string by adding characters or appending string itself
- Find the longest sub-string which is prefix, suffix and also present inside the string | Set 2
- String slicing in Python to check if a string can become empty by recursive deletion
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.