📌  相关文章
📜  根据给定条件扩展字符串

📅  最后修改于: 2021-05-06 23:40:00             🧑  作者: Mango

给定类型为“ 3(ab)4(cd)”的字符串str ,任务是将其扩展为“ abababcdcdcdcd”,其中整数在[1,9]范围内。
在2018年10月举行的ThoughtWorks采访中提出了这个问题。
例子:

方法:我们遍历字符串,然后等待数值num在位置i上出现。一旦到达,我们就在i + 1中检查‘(’ 。如果存在,则程序进入循环以提取‘(’‘)’中的所有内容并将其连接为空字符串temp 。另一个循环将打印生成的字符串num次。重复这些步骤,直到字符串完成为止。
下面是该方法的实现:

C++
// C++ implementation of the approach
#include
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


Java
// 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);
    }
}


Python3
# 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


C#
// C# implementation of
// the above approach
using System;
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[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++)
        {
          Console.Write(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);
}
}
 
// This code is contributed by Chitranayal


输出:
abababcdcdcdcd