Kth character after replacing each character of String by its frequency exactly X times | Java program for the above approach ; Function to find the Kth character after X days ; Variable to store the KthChar ; Traverse the string ; Convert char into int ; Calculate characters ; If K is less than sum than ans = str [ i ] ; Return answer ; Driver code ; Given Input ; Function Call
class GFG { static char FindKthChar ( String str , int K , int X ) { char ans = ' ▁ ' ; int sum = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { int digit = ( int ) str . charAt ( i ) - 48 ; int range = ( int ) Math . pow ( digit , X ) ; sum += range ; if ( K <= sum ) { ans = str . charAt ( i ) ; break ; } } return ans ; } public static void main ( String [ ] args ) { String str = "123" ; int K = 9 ; int X = 3 ; char ans = FindKthChar ( str , K , X ) ; System . out . println ( ans ) ; } }