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.
Input: s1 = “geeks”, s2 = “for”
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”
- 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:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Pairs of strings which on concatenating contains each character of "string"
- Total character pairs from two strings, with equal number of set bits in their ascii value
- Pairs of complete strings in two sets of strings
- Sub-strings of length K containing same character
- Sub-strings that start and end with one character and have at least one other
- Count of sub-strings that contain character X at least once
- Count of character pairs at same distance as in English alphabets
- Check if there is any common character in two given strings
- Number of sub-strings that contain the given character exactly k times
- Count of sub-strings that do not consist of the given character
- Count of strings that can be formed from another string using each character at-most once
- Print the most occurring character in an array of strings
- NFA to accept strings that has atleast one character occurring in a multiple of 3
- Number of strings which starts and ends with same character after rotations
- Number of character corrections in the given strings to make them equal
- Check if two strings can be made equal by swapping one character among each other
- Check if two strings after processing backspace character are equal or not
- Program to build a DFA to accept strings that start and end with same character
- Program to build a DFA that accepts strings starting and ending with different character
- Generate two output strings depending upon occurrence of character in input string.
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.