Generate a string from an array of alphanumeric strings based on given conditions | Java program for the above approach ; Function to generate required string ; To store the result ; Split name and number ; Stores the maximum number less than or equal to the length of name ; Check for number by parsing it to integer if it is greater than max number so far ; Check if no such number is found then we append X to the result . ; Otherwise ; Append max index of the name ; Return the final string ; Driver Code ; Function Call
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 ) ) ; } }