text: Character pairs from two strings with even sum | C ++ implementation of the approach ; Function to return the total number of valid pairs ; Count total number of even and odd ascii values for string s1 ; Count total number of even and odd ascii values for string s2 ; Return total valid pairs ; Driver code
code: #include <bits/stdc++.h> NEW_LINE using namespace std ; int totalPairs ( string s1 , string s2 ) { int a1 = 0 , b1 = 0 ; for ( int i = 0 ; i < s1 . length ( ) ; i ++ ) { if ( int ( s1 [ i ] ) % 2 != 0 ) a1 ++ ; else b1 ++ ; } int a2 = 0 , b2 = 0 ; for ( int i = 0 ; i < s2 . length ( ) ; i ++ ) { if ( int ( s2 [ i ] ) % 2 != 0 ) a2 ++ ; else b2 ++ ; } return ( ( a1 * a2 ) + ( b1 * b2 ) ) ; } int main ( ) { string s1 = " geeks " , s2 = " for " ; cout << totalPairs ( s1 , s2 ) ; return 0 ; }