Character pairs from two strings with even sum

Given two strings s1 and s2. The task is to take one character from first string, and one character from second string, and check if the sum of ascii values of both the character is an even number. Print the total number of such pairs. Note that both the strings consist of lowercase English alphabets.

Examples:

Input: s1 = “geeks”, s2 = “for”
Output: 5
All valid pairs are:
(g, o) -> 103 + 111 = 214
(e, o) -> 101 + 111 = 212
(e, o) -> 101 + 111 = 212
(k, o) -> 107 + 111 = 218
(s, o) -> 115 + 111 = 226

Input: s1 = “abcd”, s2 = “swed”
Output: 8



Approach:

  • It is clear, that for the sum to be even, either both the ascii values must be even or both must be odd.
  • Calculate the total number of odd and even ascii values from first string. Let it be a1 and b1 respectively.
  • Calculate the total number of odd and even ascii values from second string. Let it be a2 and b2 respectively.
  • Then the total number of valid pairs will be ((a1 * a2) + (b1 * b2)).

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GfG
{
  
    // Function to return the total number of valid pairs 
    static int totalPairs(String s1, String s2) 
    
        int a1 = 0, b1 = 0
      
        // Count total number of even and odd 
        // ascii values for string s1 
        for (int i = 0; i < s1.length(); i++) 
        
            if ((int)s1.charAt(i) % 2 != 0
                a1++; 
            else
                b1++; 
        
      
        int a2 = 0, b2 = 0
      
        // Count total number of even and odd 
        // ascii values for string s2 
        for (int i = 0; i < s2.length(); i++)
        
            if ((int)s2.charAt(i) % 2 != 0
                a2++; 
            else
                b2++; 
        
      
        // Return total valid pairs 
        return ((a1 * a2) + (b1 * b2)); 
    
  
    // Driver code
    public static void main(String []args)
    {
          
        String s1 = "geeks", s2 = "for"
        System.out.println(totalPairs(s1, s2));
    }
}
  
// This code is contributed by Rituraj Jain

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# Function to return the total 
# number of valid pairs 
def totalPairs(s1, s2) : 
  
    a1 = 0; b1 = 0
  
    # Count total number of even and  
    # odd ascii values for string s1 
    for i in range(len(s1)) :
        if (ord(s1[i]) % 2 != 0) :
            a1 += 1
        else :
            b1 += 1
      
    a2 = 0 ; b2 = 0
  
    # Count total number of even and odd 
    # ascii values for string s2 
    for i in range(len(s2)) :
        if (ord(s2[i]) % 2 != 0) :
            a2 += 1
        else :
            b2 += 1
      
    # Return total valid pairs 
    return ((a1 * a2) + (b1 * b2)); 
  
# Driver code 
if __name__ == "__main__"
  
    s1 = "geeks";
    s2 = "for"
      
    print(totalPairs(s1, s2));
      
# This code is contributed by Ryuga

chevron_right


Output:

5


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01, rituraj_jain



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.