重复给定字符串的子字符串所需的次数 |第 2 组(递归)
给定字符串str,任务是重复字符串X的每个子字符串的次数,其中X是由原始字符串中的子字符串之后出现的连续数字组成的数字。例如,如果str = “g1e2ks1”,那么结果字符串将是“geeks”。
例子:
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
这里讨论了解决这个问题的迭代方法。本文重点介绍解决给定问题的递归方法。
方法:递归地逐个字符字符遍历字符串。遍历时,将字符保留在单独的字符串中,将表示重复次数的数字保留在整数中。对于找到的每一组数字,根据找到的整数将存储的字符串的出现附加到最终结果字符串中,并递归调用剩余的字符串。
下面是上述方法的实现:
C++
// C++ program of the above approach
#include
using namespace std;
// Stores the final string
string res = "";
// Recursive function to generate
// the required string
void decode(string s, int i, string t, int x)
{
// If complete string has
// been traversed
if (i == s.length()) {
// Append string t, s times
for (int i = 0; i < x; i++)
res = res + t;
return;
}
// If current character
// is an integer
if (isdigit(s[i]) && !t.empty()) {
x = x * 10 + (s[i] - '0');
}
// If current character
// in an alphabet
if (!isdigit(s[i])) {
if (!t.empty() && x > 0) {
// Append t, x times
for (int i = 0; i < x; i++)
res = res + t;
// Update the value
// of t and x
t = "";
x = 0;
}
t = t + s[i];
}
// Recursive call for the
// remaining string
decode(s, i + 1, t, x);
}
// Function to convert the given
// string into desired form
string decodeString(string s)
{
// Recursive Call
decode(s, 0, "", 0);
// Return Answer
return res;
}
// Driven Program
int main()
{
string str = "g1e2k1s1for1g1e2ks1";
cout << decodeString(str);
return 0;
}
Java
// Java program of the above approach
import java.util.*;
class GFG
{
// Stores the final String
static String res = "";
// Recursive function to generate
// the required String
static void decode(String s, int i, String t, int x)
{
// If complete String has
// been traversed
if (i == s.length()) {
// Append String t, s times
for (int j = 0; j < x; j++)
res = res + t;
return;
}
// If current character
// is an integer
if (Character.isDigit(s.charAt(i)) && !t.isEmpty()) {
x = x * 10 + (s.charAt(i) - '0');
}
// If current character
// in an alphabet
if (!Character.isDigit(s.charAt(i))) {
if (!t.isEmpty() && x > 0) {
// Append t, x times
for (int j = 0; j < x; j++)
res = res + t;
// Update the value
// of t and x
t = "";
x = 0;
}
t = t + s.charAt(i);
}
// Recursive call for the
// remaining String
decode(s, i + 1, t, x);
}
// Function to convert the given
// String into desired form
static String decodeString(String s)
{
// Recursive Call
decode(s, 0, "", 0);
// Return Answer
return res;
}
// Driven Program
public static void main(String[] args)
{
String str = "g1e2k1s1for1g1e2ks1";
System.out.print(decodeString(str));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python program of the above approach
# Stores the final String
res = "";
# Recursive function to generate
# the required String
def decode(s, i, t, x):
global res;
# If complete String has
# been traversed
if (i == len(s)):
# Append String t, s times
for j in range(x):
res = res + t;
return;
# If current character
# is an integer
if (s[i].isdigit() and len(t)!=0):
x = x * 10 + (ord(s[i]) - ord('0'));
# If current character
# in an alphabet
if (s[i].isdigit()==False):
if (len(t)!=0 and x > 0):
# Append t, x times
for j in range(x):
res = res + t;
# Update the value
# of t and x
t = "";
x = 0;
t = t + s[i];
# Recursive call for the
# remaining String
decode(s, i + 1, t, x);
# Function to convert the given
# String into desired form
def decodeString(s):
# Recursive Call
decode(s, 0, "", 0);
# Return Answer
return res;
# Driven Program
if __name__ == '__main__':
str = "g1e2k1s1for1g1e2ks1";
print(decodeString(str));
# This code is contributed by 29AjayKumar
C#
// C# program of the above approach
using System;
public class GFG
{
// Stores the readonly String
static String res = "";
// Recursive function to generate
// the required String
static void decode(String s, int i, String t, int x)
{
// If complete String has
// been traversed
if (i == s.Length) {
// Append String t, s times
for (int j = 0; j < x; j++)
res = res + t;
return;
}
// If current character
// is an integer
if (char.IsDigit(s[i]) && t.Length!=0) {
x = x * 10 + (s[i] - '0');
}
// If current character
// in an alphabet
if (!char.IsDigit(s[i])) {
if (t.Length!=0 && x > 0) {
// Append t, x times
for (int j = 0; j < x; j++)
res = res + t;
// Update the value
// of t and x
t = "";
x = 0;
}
t = t + s[i];
}
// Recursive call for the
// remaining String
decode(s, i + 1, t, x);
}
// Function to convert the given
// String into desired form
static String decodeString(String s)
{
// Recursive Call
decode(s, 0, "", 0);
// Return Answer
return res;
}
// Driven Program
public static void Main(String[] args)
{
String str = "g1e2k1s1for1g1e2ks1";
Console.Write(decodeString(str));
}
}
// This code is contributed by 29AjayKumar
Javascript
输出
geeksforgeeks
时间复杂度: O(M),其中 M 是给定字符串中存在的所有整数的总和
辅助空间: O(N)