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
- Character pairs from two strings with even sum
- Pairs of strings which on concatenating contains each character of "string"
- Count pairs in an array such that both elements has equal set bits
- Replace every character of string by character whose ASCII value is K times more than it
- Number of sub-strings that contain the given character exactly k times
- Number of Binary Strings of length N with K adjacent Set Bits
- Count number of equal pairs in a string
- Minimum number of pairs required to make two strings same
- Minimize ASCII values sum after removing all occurrences of one character
- Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character
- Minimum number of given operations required to make two strings equal
- Sub-strings having exactly k characters that have ASCII value greater than p
- Find the minimum number of preprocess moves required to make two strings equal
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.