- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Parity of Count of Letters whose Position and Frequency have Same Parity
In this problem, we will count the number of characters whose frequency and position have the same parity and print the count of the number as odd or even.
To solve the problem, we can find the frequency of each character in the string, and count total characters whose freqeuncy and position have the same parity. After that, we can print the odd or even answer based on the counts.
Problem statement − We have given a string alpha containing only lowercase english alphabetical characters. We need to check whether the number of characters having equal parity of their alphabetical position and frequency is odd or even.
Any character has the same frequency and alphabetical position parity if the character follows any of the conditions below.
If character frequency in the string is odd, and alphabetical position is also odd.
If the character frequency in the string is even, and the alphabetical position is also even.
Sample examples
Input
alpha = "dbbabcdc"
Output
Even
Explanation
The frequency of ‘a’ is 1, and the position is also 1. So, both have the same parity, and the count becomes 1.
The frequency of ‘d’ is 2, and the position is 4. So, the count becomes 2 due to the same parity bit.
The value of the count is 2, which is even.
Input
alpha = "ppqqr"
Output
Odd
Explanation – The parity of only ‘p’ is same. So, the count is 1, and the answer is odd.
Input
alpha = "pqqqqrrr";
Output
Even
Explanation − The parity of any character is not same. So, it prints ‘Even’ due to the zero value of the count.
Approach 1
In this approach, we will use the map data structure to store the frequency of each string character. After that, we will count the number of characters having the same parity in the alphabetical position and frequency.
Algorithm
Step 1 − Define the count[] array of length 27 and initialize with 0. Also, initialize the ‘parity’ with 0.
Step 2 − Store the character frequency in the count[] array.
Step 3 − Make 26 iterations to traverse each lowercase alphabetical character.
Step 4 − If count[p] is greater than 0, check whether the character frequency and position have the same parity. If yes, increment the ‘parity’ value by 1.
Step 5 − At last, if parity is divisible by 2, return ‘Even’. Otherwise, return ‘Odd’.
Example
#include <bits/stdc++.h> using namespace std; string getParity(string alpha) { // To store the count of characters int count[27] = {0}; int parity = 0; // Count frequency of each character for (int p = 0; p < alpha.size(); p++) { count[alpha[p] - 'a' + 1]++; } for (int p = 1; p <= 26; p++) { if (count[p] != 0) { // Increment parity for valid odd and even parity if (p % 2 == 0 && count[p] % 2 == 0 || p % 2 == 1 && count[p] % 2 == 1) parity++; } } // Return value based on final parity count if (parity % 2 == 1) return "ODD"; else return "EVEN"; } int main() { string alpha = "dbbabcdc"; cout << "The parity of given string's character's is " << getParity(alpha); return 0; }
Output
The parity of given string's character's is EVEN
Time complexity − O(N) for calculating the frequency of characters.
Space complexity − O(26) ~ O(1) to store freqeucny of alphabetical characters.
Approach 2
In this approach, we will sort the given string. After that, whenever we get the different adjacent characters, we will check the parity of the frequency and position of the previous character.
Algorithm
Step 1 − Initialize the ‘parity’ with 0.
Step 2 − The sort() method is used to sort the given string.
Step 3 − Start traversing the string, and initialize the ‘charCnt’ with 0 to store the frequency of the current character.
Step 4 − If the current character is not the same as the next characer, check whether the parity of the ‘charCnt’ and position of the character matches. If yes, increment ‘parity’ by 1.
Step 5 − If the current character is the same as the previous character, increment the ‘charCnt’ by 1.
Step 6 − At the end, if the ‘parity’ value is even, return ‘Even’. Otherwise, return ‘Odd’.
Example
#include <bits/stdc++.h> using namespace std; string getParity(string alpha) { int parity = 0; // Sort the string sort(alpha.begin(), alpha.end()); // Traverse the string for (int p = 0; p < alpha.size(); p++) { int charCnt = 0; // When we get different adjacent characters if (alpha[p] != alpha[p + 1]) { // Validating the odd and even parties if (charCnt % 2 == 1 && (alpha[p] - 'a' + 1) % 2 == 1 || charCnt % 2 == 0 && (alpha[p] - 'a' + 1) % 2 == 0) parity++; } else { charCnt++; } } if (parity % 2 == 1) return "ODD"; else return "EVEN"; } int main() { string alpha = "abbbccdd"; cout << "The parity of given string's character's is " << getParity(alpha); return 0; }
Output
The parity of given string's character's is EVEN
Time complexity − O(NlogN) for sorting the string.
Space complexity − O(N) to sort the string.
The first approach takes the constant space, whereas the second approach takes the dynamic space to sort the given string. Also, the second approach is more time expensive, so it is recommended to use the first approach for better performance.