📌  相关文章
📜  根据给定条件从字母数字字符串数组生成字符串

📅  最后修改于: 2021-04-24 15:02:41             🧑  作者: Mango

给定一个字符串数组arr [] ,其中每个字符串的形式均为“名称:数字”,并以字符T作为输入,任务是根据以下条件生成一个新的字符串:

  • 在每个字符串找到“数字”中的最大数字,该数字小于或等于字符串“名称”的长度。
  • 如果获得任何这样的数字d,则在该字符串名称输出字符串的索引d附加字符。否则,将字符T附加到输出字符串。

例子:

方法:要解决此问题,请执行以下步骤:

  1. 遍历字符串数组,并在“ ”周围拆分每个字符串。第一部分包含名称,第二部分包含数字。
  2. 存储的名称的长度可变,并找到最大位数小于或等于所述数目的长度。
  3. 如果找到任何这样的数字,请提取该名称索引处的字符,并将其追加到结果字符串。否则,将T附加到结果字符串。
  4. 重复上述操作对于阵列中的所有字符串之后打印所得到的字符串。

下面是上述方法的实现:

C++
// C++ program for the
// above approach
#include
using namespace std;
     
// Function to generate required string
string generatePassword(vectorarr,
                        char T)
{
  // To store the result
  string result;
  for (auto s:arr)
  {
    // Split name and number
    int index;
     
    for(int i = 0; i < s.size(); i++)
    {
      if(s[i] == ':')
      {
        index = i;
        break;
      }
    }
     
    string name = s.substr(0, index);
    string number = s.substr(index + 1,
                             s.size() -
                             index - 1);
    int n = name.length();
     
    // Stores the maximum number
    // less than or equal to the
    // length of name
    int max = 0;
 
    for (int i = 0; i < number.length(); i++)
    {
      // Check for number by parsing
      // it to integer if it is greater
      // than max number so far
      int temp = number[i] - '0';
 
      if (temp > max && temp <= n)
        max = temp;
    }
 
    // Check if no such number is
    // found then we append X
    // to the result.
    if (max == 0)
      result.push_back(T);
 
    // Otherwise
    else
 
      // Append max index
      // of the name
      result.push_back(name[max - 1]);
  }
 
  // Return the final string
  return result;
}
 
// Driver Code
int main()
{
  vectorarr = {"Geeks:89167",
                       "gfg:68795"};
  char T = 'X';
 
  // Function Call
  cout << (generatePassword(arr, T));
}
 
// This code is contributed by Stream_Cipher


Java
// Java program for the above approach
 
import java.io.*;
 
class GFG {
 
    // Function to generate required string
    public static String
    generatePassword(String s[], char T)
    {
        // To store the result
        StringBuilder result
            = new StringBuilder();
 
        for (String currentString : s) {
 
            // Split name and number
            String person[]
                = currentString.split(":");
            String name = person[0];
            String number = person[1];
 
            int n = name.length();
 
            // Stores the maximum number
            // less than or equal to the
            // length of name
            int max = 0;
 
            for (int i = 0;
                 i < number.length(); i++) {
 
                // Check for number by parsing
                // it to integer if it is greater
                // than max number so far
                int temp = Integer.parseInt(
                    String.valueOf(number.charAt(i)));
 
                if (temp > max && temp <= n)
                    max = temp;
            }
 
            // Check if no such number is
            // found then we append X
            // to the result.
            if (max == 0)
                result.append(T);
 
            // Otherwise
            else
 
                // Append max index
                // of the name
                result.append(
                    String.valueOf(
                        name.charAt(max - 1)));
        }
 
        // Return the final string
        return result.toString();
    }
 
    // Driver Code
    public static void
        main(String[] args)
    {
        String arr[] = { "Geeks:89167",
                         "gfg:68795" };
        char T = 'X';
 
        // Function Call
        System.out.println(
            generatePassword(arr, T));
    }
}


Python3
# Python3 program for
# the above approach
 
# Function to generate
# required string
def generatePassword(s, T):
 
    # To store the result
    result = []
     
    for currentString in s:
 
        # Split name and number
        person = currentString.split(":")
        name = person[0]
        number = person[1]
        n = len(name)
 
        # Stores the maximum number
        # less than or equal to the
        # length of name
        max = 0
 
        for i in range(len(number)):
 
            # Check for number by parsing
            # it to integer if it is greater
            # than max number so far
            temp = int(number[i])
 
            if(temp > max and temp <= n):
                max = temp
 
        # Check if no such number is
        # found then we append X
        # to the result.
        if max == 0:
            result.append(T)
 
        # Otherwise
        else:
 
            # Append max index
            # of the name
            result.append(name[max - 1])
 
    # Return the
    # final string
    return result
 
# Driver Code
arr = ["Geeks:89167","gfg:68795"]
T = 'X'
 
# Function call
print(*generatePassword(arr, T),
       sep = "")
 
# This code is contributed by avanitrachhadiya2155


C#
// C# program for the above approach
using System;
using System.Text;
class GFG{
 
// Function to generate required string
public static String generatePassword(String []s,
                                      char T)
{
    // To store the result
    StringBuilder result = new StringBuilder();
    foreach (String currentString in s)
    {
        // Split name and number
        String []person = currentString.Split(':');
        String name = person[0];
        String number = person[1];
 
        int n = name.Length;
 
        // Stores the maximum number
        // less than or equal to the
        // length of name
        int max = 0;
 
        for (int i = 0; i < number.Length; i++)
        {
            // Check for number by parsing
            // it to integer if it is greater
            // than max number so far
            int temp = Int32.Parse(String.Join("",
                                   number[i]));
            if (temp > max && temp <= n)
                max = temp;
        }
 
        // Check if no such number is
        // found then we append X
        // to the result.
        if (max == 0)
            result.Append(T);
 
        // Otherwise
        else
 
            // Append max index
            // of the name
            result.Append(String.Join("",
                          name[max - 1]));
    }
 
    // Return the readonly string
    return result.ToString();
}
 
// Driver Code
public static void Main(String[] args)
{
    String []arr = {"Geeks:89167",
                    "gfg:68795"};
    char T = 'X';
 
    // Function Call
    Console.WriteLine(generatePassword(arr, T));
}
}
 
// This code is contributed by shikhasingrajput


输出:
GX



时间复杂度: O(N)
辅助空间: O(1)