Check if characters of a given string can be used to form any N equal strings | Java program for the above approach ; Function to check if the freq of any character is divisible by N ; Stores the frequency of characters ; If frequency of a character is not divisible by n ; If no character has frequency at least N ; Driver Code ; Function Call
import java . util . * ; class GFG { static boolean isSame ( String str , int n ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( mp . containsKey ( str . charAt ( i ) - ' a ' ) ) { mp . put ( str . charAt ( i ) - ' a ' , mp . get ( str . charAt ( i ) - ' a ' ) + 1 ) ; } else { mp . put ( str . charAt ( i ) - ' a ' , 1 ) ; } } for ( Map . Entry < Integer , Integer > it : mp . entrySet ( ) ) { if ( ( it . getValue ( ) ) >= n ) { return true ; } } return false ; } public static void main ( String [ ] args ) { String str = " ccabcba " ; int n = 4 ; if ( isSame ( str , n ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }