Generate a string from an array of alphanumeric strings based on given conditions
Last Updated :
09 Jun, 2021
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++
#include<bits/stdc++.h>
using namespace std;
string generatePassword(vector<string>arr,
char T)
{
string result;
for ( auto s:arr)
{
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();
int max = 0;
for ( int i = 0; i < number.length(); i++)
{
int temp = number[i] - '0' ;
if (temp > max && temp <= n)
max = temp;
}
if (max == 0)
result.push_back(T);
else
result.push_back(name[max - 1]);
}
return result;
}
int main()
{
vector<string>arr = { "Geeks:89167" ,
"gfg:68795" };
char T = 'X' ;
cout << (generatePassword(arr, T));
}
|
Java
import java.io.*;
class GFG {
public static String
generatePassword(String s[], char T)
{
StringBuilder result
= new StringBuilder();
for (String currentString : s) {
String person[]
= currentString.split( ":" );
String name = person[ 0 ];
String number = person[ 1 ];
int n = name.length();
int max = 0 ;
for ( int i = 0 ;
i < number.length(); i++) {
int temp = Integer.parseInt(
String.valueOf(number.charAt(i)));
if (temp > max && temp <= n)
max = temp;
}
if (max == 0 )
result.append(T);
else
result.append(
String.valueOf(
name.charAt(max - 1 )));
}
return result.toString();
}
public static void
main(String[] args)
{
String arr[] = { "Geeks:89167" ,
"gfg:68795" };
char T = 'X' ;
System.out.println(
generatePassword(arr, T));
}
}
|
Python3
def generatePassword(s, T):
result = []
for currentString in s:
person = currentString.split( ":" )
name = person[ 0 ]
number = person[ 1 ]
n = len (name)
max = 0
for i in range ( len (number)):
temp = int (number[i])
if (temp > max and temp < = n):
max = temp
if max = = 0 :
result.append(T)
else :
result.append(name[ max - 1 ])
return result
arr = [ "Geeks:89167" , "gfg:68795" ]
T = 'X'
print ( * generatePassword(arr, T),
sep = "")
|
C#
using System;
using System.Text;
class GFG{
public static String generatePassword(String []s,
char T)
{
StringBuilder result = new StringBuilder();
foreach (String currentString in s)
{
String []person = currentString.Split( ':' );
String name = person[0];
String number = person[1];
int n = name.Length;
int max = 0;
for ( int i = 0; i < number.Length; i++)
{
int temp = Int32.Parse(String.Join( "" ,
number[i]));
if (temp > max && temp <= n)
max = temp;
}
if (max == 0)
result.Append(T);
else
result.Append(String.Join( "" ,
name[max - 1]));
}
return result.ToString();
}
public static void Main(String[] args)
{
String []arr = { "Geeks:89167" ,
"gfg:68795" };
char T = 'X' ;
Console.WriteLine(generatePassword(arr, T));
}
}
|
Javascript
<script>
function generatePassword(s, T) {
var result = [];
for (const currentString of s) {
var person = currentString.split( ":" );
var name = person[0];
var number = person[1];
var n = name.length;
var max = 0;
for ( var i = 0; i < number.length; i++) {
var temp = parseInt(number[i]);
if (temp > max && temp <= n) max = temp;
}
if (max === 0) result.push(T);
else result.push(name[max - 1]);
}
return result.join( "" );
}
var arr = [ "Geeks:89167" , "gfg:68795" ];
var T = "X" ;
document.write(generatePassword(arr, T) + "<br>" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...