Find k 'th character of decrypted string | Set 1 | Function to find K 'th character in  Encoded String ; expand string variable is used to store final string after decompressing string str ; let temp = " " ; Current substring let freq = 0 ; Count of current substring ; read characters until you find a number or end of string ; push character in temp ; read number for how many times string temp will be repeated in decompressed string ; generating frequency of temp ; now append string temp into expand equal to its frequency ; this condition is to handle the case when string str is ended with alphabets not with numeric value ; Driver program to test the string
function encodedChar ( str , k ) { let expand = " " ; for ( let i = 0 ; i < str . length ; ) { while ( i < str . length && str [ i ] . charCodeAt ( 0 ) >= ' ' . charCodeAt ( 0 ) && str [ i ] . charCodeAt ( 0 ) <= ' ' . charCodeAt ( 0 ) ) { temp += str [ i ] ; i ++ ; } while ( i < str . length && str [ i ] . charCodeAt ( 0 ) >= ' ' . charCodeAt ( 0 ) && str [ i ] . charCodeAt ( 0 ) <= ' ' . charCodeAt ( 0 ) ) { freq = freq * 10 + str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; i ++ ; } for ( let j = 1 ; j <= freq ; j ++ ) expand += temp ; } if ( freq == 0 ) expand += temp ; return expand [ k - 1 ] ; } let str = " " ; let k = 21 ; document . write ( encodedChar ( str , k ) ) ;