Sum of frequencies of characters of a string present in another string | Java program for the above approach ; Function to find sum of frequencies of characters of S1 present in S2 ; Insert all characters of string S1 in the set ; Traverse the string S2 ; Check if X is present in bset or not ; Increment count by 1 ; Finally , print the count ; Driver code ; Given strings
import java . util . HashSet ; class GFG { static void countTotalFrequencies ( String S1 , String S2 ) { HashSet < Character > bset = new HashSet < Character > ( ) ; char [ ] S1arr = S1 . toCharArray ( ) ; char [ ] S2arr = S2 . toCharArray ( ) ; for ( char x : S1arr ) bset . add ( x ) ; int count = 0 ; for ( char x : S2arr ) { if ( bset . contains ( x ) ) count += 1 ; } System . out . print ( count ) ; } public static void main ( String [ ] args ) { String S1 = " geEksFOR " ; String S2 = " GeEksforgeEKS " ; countTotalFrequencies ( S1 , S2 ) ; } }