Number of substrings with each character occurring even times | Java program for the above approach ; Function to count substrings having even frequency of each character ; Stores the total count of substrings ; Traverse the range [ 0 , N ] : ; Traverse the range [ i + 1 , N ] ; Stores the substring over the range of indices [ i , len ] ; Stores the frequency of characters ; Count frequency of each character ; Traverse the dictionary ; If any of the keys have odd count ; Otherwise ; Return count ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static int subString ( String s , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int len = i + 1 ; len <= n ; len ++ ) { String test_str = s . substring ( i , len ) ; HashMap < Character , Integer > res = new HashMap < > ( ) ; for ( char keys : test_str . toCharArray ( ) ) { res . put ( keys , res . getOrDefault ( keys , 0 ) + 1 ) ; } int flag = 0 ; for ( char keys : res . keySet ( ) ) { if ( res . get ( keys ) % 2 != 0 ) { flag = 1 ; break ; } } if ( flag == 0 ) count += 1 ; } } return count ; } public static void main ( String [ ] args ) { String S = " abbaa " ; int N = S . length ( ) ; System . out . println ( subString ( S , N ) ) ; } }