Get K | CPP 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; string K_thletter ( string S , int K ) { int N = S . size ( ) ; long size = 0 ; for ( int i = 0 ; i < N ; ++ i ) { if ( isdigit ( S [ i ] ) ) size = size * ( S [ i ] - '0' ) ; else size += 1 ; } for ( int i = N - 1 ; i >= 0 ; -- i ) { K %= size ; if ( K == 0 && isalpha ( S [ i ] ) ) return ( string ) " " + S [ i ] ; if ( isdigit ( S [ i ] ) ) size = size / ( S [ i ] - '0' ) ; else size -= 1 ; } } int main ( ) { string S = " geeks2for2" ; int K = 15 ; cout << K_thletter ( S , K ) ; return 0 ; }