Count of Substrings that can be formed without using the given list of Characters | Java implementation of the above approach ; Function to find the Number of sub - Strings without using given character ; Mark the given characters in the freq array ; Count variable to store the count of the characters until a character from given L is encountered ; If a character from L is encountered , then the answer variable is incremented by the value obtained by using the mentioned formula and count is set to 0 ; For last remaining characters ; Driver code
import java . util . * ; class GFG { static int countSubString ( char [ ] S , char L [ ] , int n ) { int [ ] freq = new int [ 26 ] ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { freq [ ( int ) ( L [ i ] - ' a ' ) ] = 1 ; } int count = 0 ; for ( int x : S ) { if ( freq [ ( int ) ( x - ' a ' ) ] > 0 ) { ans += ( count * count + count ) / 2 ; count = 0 ; } else count ++ ; } ans += ( count * count + count ) / 2 ; return ans ; } public static void main ( String [ ] args ) { String S = " abcpxyz " ; char L [ ] = { ' a ' , ' p ' , ' q ' } ; int n = L . length ; System . out . print ( countSubString ( S . toCharArray ( ) , L , n ) ) ; } }