Get K | Java implementation of above approach ; Function to return the K - th letter from new String . ; finding size = length of new string S ' ; get the K - th letter ; Driver program
class GFG { static String K_thletter ( String S , int K ) { int N = S . length ( ) ; long size = 0 ; for ( int i = 0 ; i < N ; ++ i ) { if ( Character . isDigit ( S . charAt ( i ) ) ) { size = size * ( S . charAt ( i ) - '0' ) ; } else { size += 1 ; } } for ( int i = N - 1 ; i >= 0 ; -- i ) { K %= size ; if ( K == 0 && Character . isAlphabetic ( S . charAt ( i ) ) ) { return ( String ) " " + S . charAt ( i ) ; } if ( Character . isDigit ( S . charAt ( i ) ) ) { size = size / ( S . charAt ( i ) - '0' ) ; } else { size -= 1 ; } } return null ; } public static void main ( String [ ] args ) { String S = " geeks2for2" ; int K = 15 ; System . out . println ( K_thletter ( S , K ) ) ; } }