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 ASCII values of both the character have same number of set bits. Print the total number of such pairs.
Input: s1 = “xcd”, s2 = “swa”
Only valid pair is (d, a) with ASCII values as 100 and 97 respectively.
Both of which contains 3 set bits.
Input: s1 = “geeks”, s2 = “forgeeks”
- Make two arrays arr1 and arr2 of size 6 with all values initialized to 0 to store the frequency of number of set bits. Since maximum number of set bits in lower case alphabets is 6.
- Traverse the string s1, and find the ascii value of each character. Store the frequency of number of set bits of each ascii value in a array arr1. (For example, if there are 3 characters with 4 set bits, then store 3 at arr)
- Do the similar operation for string s2 and store its value in another array arr2.
- Initialize a count variable with 0.
- For total number of pairs, keep on adding (arr1[i] * arr2[i]) in count variable for all valid values of i.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Replace every character of string by character whose ASCII value is K times more than it
- Total distinct pairs from two arrays such that second number can be obtained by inverting bits of first
- Count pairs of elements such that number of set bits in their AND is B[i]
- Count pairs of elements such that number of set bits in their OR is B[i]
- Count pairs of characters in a string whose ASCII value difference is K
- Sub-strings having exactly k characters that have ASCII value greater than p
- Count pairs from 1 to N such that their Sum is divisible by their XOR
- Check whether two strings can be made equal by reversing substring of equal length from both strings
- Convert Hexadecimal value String to ASCII value String
- Print each word in a sentence with their corresponding average of ASCII values
- Check whether two strings can be made equal by copying their characters with the adjacent ones
- Count of strings possible by replacing two consecutive same character with new character
- Count total set bits in all numbers from 1 to n | Set 2
- Count total set bits in all numbers from 1 to N | Set 3
- Minimize ASCII values sum after removing all occurrences of one character
- Sort the character array based on ASCII % N
- Character pairs from two strings with even sum
- Count pairs with set bits sum equal to K
- Count pairs in an array such that both elements has equal set bits
- Check if array can be sorted by swapping pairs with GCD of set bits count equal to that of the smallest array element
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.