Modify a string by circularly shifting each character to the right by respective frequencies | java program for the above approach ; Function to replace the characters by its frequency of character in it ; Stores frequencies of characters in the string S ; Traverse the string S ; Increment the frequency of each character by 1 ; Traverse the string S ; Find the frequency of the current character ; Update the character ; Print the resultant string ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static void addFrequencyToCharacter ( String Str ) { int frequency [ ] = new int [ 26 ] ; int N = Str . length ( ) ; char S [ ] = Str . toCharArray ( ) ; for ( int i = 0 ; i < N ; i ++ ) { frequency [ S [ i ] - ' a ' ] += 1 ; } for ( int i = 0 ; i < N ; i ++ ) { int add = frequency [ S [ i ] - ' a ' ] % 26 ; if ( ( int ) ( S [ i ] ) + add <= ( int ) ( ' z ' ) ) S [ i ] = ( char ) ( ( int ) ( S [ i ] ) + add ) ; else { add = ( ( int ) ( S [ i ] ) + add ) - ( ( int ) ( ' z ' ) ) ; S [ i ] = ( char ) ( ( int ) ( ' a ' ) + add - 1 ) ; } } System . out . println ( new String ( S ) ) ; } public static void main ( String [ ] args ) { String S = " geeksforgeeks " ; addFrequencyToCharacter ( S ) ; } }