Character pairs from two strings with even sum
Last Updated :
16 Oct, 2023
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
Brute Force Approach:
A brute force approach to solve this problem would be to iterate through every possible pair of characters from both the strings, calculate the sum of their ASCII values and check if the sum is even or not. If it is even, then increment a counter. Finally, return the counter as the total number of valid pairs.
Here are the steps to implement above approach:
- Initialize a counter to 0 to keep track of the total number of valid pairs.
- Iterate over the first string s1 and for each character, iterate over the second string s2 and calculate the sum of the ASCII values of both characters.
- Check if the sum of the ASCII values is even or not using the modulo operator (%).
- Return the counter as the total number of valid pairs.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int totalPairs(string s1, string s2)
{
int cnt = 0;
for ( int i = 0; i < s1.length(); i++) {
for ( int j = 0; j < s2.length(); j++) {
int sum = s1[i] + s2[j];
if (sum % 2 == 0)
cnt++;
}
}
return cnt;
}
int main()
{
string s1 = "geeks" , s2 = "for" ;
cout << totalPairs(s1, s2);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int totalPairs(String s1, String s2) {
int cnt = 0 ;
for ( int i = 0 ; i < s1.length(); i++) {
for ( int j = 0 ; j < s2.length(); j++) {
int sum = s1.charAt(i) + s2.charAt(j);
if (sum % 2 == 0 )
cnt++;
}
}
return cnt;
}
public static void main(String[] args) {
String s1 = "geeks" ;
String s2 = "for" ;
System.out.println(totalPairs(s1, s2));
}
}
|
Python3
def totalPairs(s1, s2):
cnt = 0
for i in range ( len (s1)):
for j in range ( len (s2)):
_sum = ord (s1[i]) + ord (s2[j])
if _sum % 2 = = 0 :
cnt + = 1
return cnt
if __name__ = = "__main__" :
s1 = "geeks"
s2 = "for"
print (totalPairs(s1, s2))
|
C#
using System;
public class Program
{
static int TotalPairs( string s1, string s2)
{
int cnt = 0;
for ( int i = 0; i < s1.Length; i++)
{
for ( int j = 0; j < s2.Length; j++)
{
int sum = s1[i] + s2[j];
if (sum % 2 == 0)
cnt++;
}
}
return cnt;
}
public static void Main()
{
string s1 = "geeks" , s2 = "for" ;
Console.WriteLine(TotalPairs(s1, s2));
}
}
|
Javascript
function totalPairs(s1, s2) {
let cnt = 0;
for (let i = 0; i < s1.length; i++) {
for (let j = 0; j < s2.length; j++) {
let sum = s1.charCodeAt(i) + s2.charCodeAt(j);
if (sum % 2 === 0) {
cnt++;
}
}
}
return cnt;
}
const s1 = "geeks" ;
const s2 = "for" ;
console.log(totalPairs(s1, s2));
|
Time Complexity: O(m * n), where m and n are the lengths of the strings s1 and s2, respectively.
Auxiliary Space: O(1)
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++
#include <bits/stdc++.h>
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;
}
|
Java
class GfG
{
static int totalPairs(String s1, String s2)
{
int a1 = 0 , b1 = 0 ;
for ( int i = 0 ; i < s1.length(); i++)
{
if (( int )s1.charAt(i) % 2 != 0 )
a1++;
else
b1++;
}
int a2 = 0 , b2 = 0 ;
for ( int i = 0 ; i < s2.length(); i++)
{
if (( int )s2.charAt(i) % 2 != 0 )
a2++;
else
b2++;
}
return ((a1 * a2) + (b1 * b2));
}
public static void main(String []args)
{
String s1 = "geeks" , s2 = "for" ;
System.out.println(totalPairs(s1, s2));
}
}
|
Python3
def totalPairs(s1, s2) :
a1 = 0 ; b1 = 0 ;
for i in range ( len (s1)) :
if ( ord (s1[i]) % 2 ! = 0 ) :
a1 + = 1 ;
else :
b1 + = 1 ;
a2 = 0 ; b2 = 0 ;
for i in range ( len (s2)) :
if ( ord (s2[i]) % 2 ! = 0 ) :
a2 + = 1 ;
else :
b2 + = 1 ;
return ((a1 * a2) + (b1 * b2));
if __name__ = = "__main__" :
s1 = "geeks" ;
s2 = "for" ;
print (totalPairs(s1, s2));
|
C#
using System;
class GfG
{
static 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));
}
public static void Main(String []args)
{
String s1 = "geeks" , s2 = "for" ;
Console.WriteLine(totalPairs(s1, s2));
}
}
|
Javascript
<script>
function totalPairs(s1, s2)
{
var a1 = 0, b1 = 0;
for ( var i = 0; i < s1.length; i++) {
if ((s1[i].charCodeAt(0)) % 2 != 0)
a1++;
else
b1++;
}
var a2 = 0, b2 = 0;
for ( var i = 0; i < s2.length; i++) {
if ((s2[i].charCodeAt(0)) % 2 != 0)
a2++;
else
b2++;
}
return ((a1 * a2) + (b1 * b2));
}
var s1 = "geeks" , s2 = "for" ;
document.write( totalPairs(s1, s2));
</script>
|
Time Complexity: O(m + n), where m and n are the lengths of the two strings.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Share your thoughts in the comments
Please Login to comment...