Given an array of strings arr[] where each string is of the form “name:number” and a character T as input, the task is to generate a new string based on the following conditions:
- In each string find the maximum digit in “number” which is less than or equal to the length of the string “name”.
- If any such digit d is obtained, then append character at index d of the string name to the output string. Otherwise, append character T to the output string.
Examples:
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”.
Approach: To solve the problem follow the steps given below:
- Traverse through the array of strings and split each string around “:“. The first part contains the name and the second part contains the number.
- Store the length of the name in a variable and find the maximum digit less than or equal to the length of the number.
- If any such digit found is found, extract the character at that index of name and append to the resultant string. Otherwise, append T to the resultant string.
- Print the resultant string after repeating the above operations for all the strings in the array.
Below is the implementation of the above approach:
C++
// C++ program for the // above approach#include<bits/stdc++.h>using namespace std; // Function to generate required stringstring generatePassword(vector<string>arr, 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 Codeint main(){ vector<string>arr = {"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 approachimport 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 callprint(*generatePassword(arr, T), sep = "")# This code is contributed by avanitrachhadiya2155 |
C#
// C# program for the above approachusing System;using System.Text;class GFG{// Function to generate required stringpublic 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 Codepublic 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
Time Complexity: O(N)
Auxiliary Space: O(1)
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.
Recommended Posts:
- Generate a String from given Strings P and Q based on the given conditions
- Generate an array B[] from the given array A[] which satisfies the given conditions
- Generate an array using given conditions from a given array
- Rearrange Array to maximize number having Array elements as digits based on given conditions
- Queries to search for an element in an array and modify the array based on given conditions
- Compare two strings considering only alphanumeric characters
- Construct the Cypher string based on the given conditions
- Sum of indices of Characters removed to obtain an Empty String based on given conditions
- Generate a string consisting of characters 'a' and 'b' that satisfy the given conditions
- Maximum items that can be bought from the cost Array based on given conditions
- Maximize the Sum of a Subsequence from an Array based on given conditions
- Generate elements of the array following given conditions
- Generate an array of size K which satisfies the given conditions
- Calculate weight of parenthesis based on the given conditions
- Maximum Sum possible by selecting X elements from a Matrix based on given conditions
- Maximum Count of pairs having equal Sum based on the given conditions
- Calculate the Square of Euclidean Distance Traveled based on given conditions
- Range sum queries based on given conditions
- Maximum possible score that can be obtained by constructing a Binary Tree based on given conditions
- Alphanumeric Abbreviations of a String
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

