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:
- Total distinct pairs from two arrays such that second number can be obtained by inverting bits of first
- Number of character corrections in the given strings to make them equal
- Count pairs with set bits sum equal to K
- Character pairs from two strings with even sum
- Count pairs in an array such that both elements has equal set bits
- Pairs of strings which on concatenating contains each character of "string"
- Check if two strings can be made equal by swapping one character among each other
- Replace every character of string by character whose ASCII value is K times more than it
- 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]
- Number of Binary Strings of length N with K adjacent Set Bits
- Number of sub-strings that contain the given character exactly k times
- Find a number X such that (X XOR A) is minimum and the count of set bits in X and B are equal
- Count number of equal pairs in a string
- Number of pairs in an array having sum equal to product
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.