给定一个字符串数组arr [] ,其中每个字符串的形式均为“名称:数字”,并以字符T作为输入,任务是根据以下条件生成一个新的字符串:
- 在每个字符串找到“数字”中的最大数字,该数字小于或等于字符串“名称”的长度。
- 如果获得任何这样的数字d,则在该字符串名称输出字符串的索引d附加字符。否则,将字符T附加到输出字符串。
例子:
Input: arr[] = {“Robert:36787”, “Tina:68721”, “Jo:56389”}, T = ‘X’
Output: tiX
Explanation:
For the first string “Robert:36787”: Length of “Robert” is 6. Since 6 is present in the string “36787”, 6th character of “Robert”, i.e. t is appended to the answer.
For the second string “Tina:68721”: Length of “Tina” is 4. The highest number less than equal to 4, which is present in “68721” is 2. Therefore, 2nd character of “Tina”, i.e. i is appended to the answer.
For the third string “Jo:56389”: Length of “Jo” is 2. Since no number less than equal to 2 is present in “56389”, T( = ‘X’) is appended to the answer.
Therefore, the final string after the above operations is “tiX”.
Input: arr[] = {“Geeks:89167”, “gfg:68795”}, T = ‘X’
Output: GX
Explanation:
For the first string “Geeks:89167”, length of “Geeks” = 5 and the “89167” number has digit 1 which is less than 5.
So, the resultant string will have the character at the 1st position of the name, which is ‘G’.
For the second string “gfg:68795”, the length of “gfg” = 3, and the “68795” doesn’t have a digit less than or equals to 3.
So, the resultant string will have the character T.
Therefore, the final string after the above operations is “GX”.
方法:要解决此问题,请执行以下步骤:
- 遍历字符串数组,并在“ : ”周围拆分每个字符串。第一部分包含名称,第二部分包含数字。
- 存储的名称的长度可变,并找到最大位数小于或等于所述数目的长度。
- 如果找到任何这样的数字,请提取该名称索引处的字符,并将其追加到结果字符串。否则,将T附加到结果字符串。
- 重复上述操作对于阵列中的所有字符串之后打印所得到的字符串。
下面是上述方法的实现:
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)