Repeat substrings of the given String required number of times
Given a 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”.
Input: str = “2a10bd3”
First digit “2” is unnecessary as there is no valid substring before it.
“a” will be repeated 10 times and then “bd” will be repeated thrice.
Input: str = “g1ee1ks1for1g1e2ks1”
Approach: Find the first valid substring i.e. the substring which doesn’t contain any digit then parse the integer present just after the found substring using parseInt() and then repeat the found substring the required number of times. Repeat these steps for all valid substrings and then print the resultant string.
Below is the implementation of the above 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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.