Table of Contents
method 1
algorithm
Example
Output
Method 2
Home Backend Development C++ Parity in the number of letters with the same letter position and frequency parity

Parity in the number of letters with the same letter position and frequency parity

Sep 14, 2023 pm 03:41 PM
Location frequency Parity

Parity in the number of letters with the same letter position and frequency parity

In this problem we will count the number of characters with same parity in frequency and position and print the count of that number as odd or even.

To solve this problem, we can find the frequency of each character in the string and count the total number of characters with the same parity in frequency and position. After that we can print odd or even answers based on the count.

Problem Statement - We are given a string alpha containing only lowercase English alphabetic characters. We need to check if the number of characters with the same letter position and frequency is odd or even.

If any character satisfies any of the following conditions, then the character has the same frequency and letter position parity.

  • If the character frequency in the string is an odd number, and the letter position is also an odd number.

  • If the character frequency in the string is an even number, and the letter position is also an even number.

Example

enter

alpha = "dbbabcdc"
Copy after login

Output

Even
Copy after login
Copy after login

illustrate

  • a has a frequency of 1 and a position of 1, so the parity is the same and the count becomes 1.

  • d has a frequency of 2 and a position of 4. Therefore, since the parity bits are the same, the count becomes 2.

The count value is 2, which is an even number.

enter

alpha = "ppqqr"
Copy after login

Output

Odd
Copy after login

Explanation – Only the parity of ‘p’ is the same. Therefore, the count is 1 and the answer is an odd number.

enter

alpha = "pqqqqrrr";
Copy after login

Output

Even
Copy after login
Copy after login

Description - Parity is not the same for any character. So since the count value is zero, it prints "Even".

method 1

In this approach, we will use a map data structure to store the frequency of each string character. After that, we count the number of characters with the same parity in letter position and frequency.

algorithm

Step 1 - Define a count[] array of length 27 and initialize it with 0. Additionally, initialize "parity" with 0.

Step 2 - Store character frequencies in count[] array.

Step 3 - Make 26 iterations to go through each lowercase alphabetic character.

Step 4 - If count[p] is greater than 0, check if the character frequency and position have the same parity. If so, increase the Parity value by 1.

Step 5 - Finally, if the 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;
}
Copy after login

Output

The parity of given string's character's is EVEN
Copy after login
Copy after login

Time complexity - O(N) for calculating the frequency of characters.

Space complexity - O(26) ~ O(1) to store the frequency of alphabetic characters.

Method 2

In this method, we will sort the given string. After that, whenever we get different adjacent characters, we check the frequency and position parity of the previous character.

algorithm

Step 1 - Initialize "Parity" to 0.

Step 2 - The sort() method is used to sort the given string.

Step 3 - Start traversing the string and initialize 'charCnt' to 0 to store the frequency of the current character.

Step 4 - If the current character is different from the next character, check if the parity and character position of "charCnt" match. If so, increase Parity by 1.

Step 5 - If the current character is the same as the previous character, increase "charCnt" by 1.

Step 6 - Finally, 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;
}
Copy after login

Output

The parity of given string's character's is EVEN
Copy after login
Copy after login

Time complexity - O(NlogN) for sorting strings.

Space complexity - O(N) to sort strings.

The first method uses constant space, while the second method uses dynamic space to sort the given string. In addition, the second method has a higher time cost, so it is recommended to use the first method for better performance.

The above is the detailed content of Parity in the number of letters with the same letter position and frequency parity. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
Details on how to turn on environment variable settings on Windows 11 Details on how to turn on environment variable settings on Windows 11 Dec 30, 2023 pm 06:07 PM

The environment variable function is an essential tool for running the configuration program in the system. However, in the latest win11 system, there are still many users who do not know how to set it up. Here is a detailed introduction to the location of the win11 environment variable opening. Come and join us. Learn to operate it. Where are the win11 environment variables: 1. First enter "win+R" to open the run box. 2. Then enter the command: controlsystem. 3. In the system information interface that opens, select "Advanced System Settings" from the left menu. 4. Then select the "Environment Variables" option at the bottom of the "System Properties" window that opens. 5. Finally, in the opened environment variables, you can make relevant settings according to your needs.

How to change game download location in Steam How to change game download location in Steam May 10, 2023 pm 11:22 PM

Steam is one of the most popular applications among PC gamers as you can find any major game on the Steam store. It simplifies the downloading, installation and management of users' favorite games through its user interface. Whenever a Steam user wants to download a game, Steam uses the application's default installation directory to download and install the game. This location defaults to C:\ProgramFiles(x86)\Steam. The problem arises because most users do not have enough space on the C drive, especially for games that take up a lot of storage space, such as 50–100GB. To overcome this problem, Steam allows users to use an app to change the download and

Win11 startup path and how to open it Win11 startup path and how to open it Jan 03, 2024 pm 11:13 PM

Every Windows system has a startup path. If you add files or software to it, it will be opened at boot time. However, many friends don’t know where the win11 startup path is. In fact, we only need to enter the corresponding folder on the C drive. Win11 startup path: 1. Double-click to open "This PC" 2. Directly paste the path "C:\ProgramData\Microsoft\Windows\StartMenu\Programs\Startup" into the path box. 3. Here is the win11 startup path. If we want to open the file after booting, we can put the file in. 4. If you cannot enter according to this path, it may be hidden.

where is windows10 credential manager where is windows10 credential manager Jul 09, 2023 am 10:09 AM

The Credential Manager is a function used by users to manage web credentials and Windows credentials, but many users still don’t know where the Windows 10 Credential Manager is. In fact, the credential manager is on the operation panel. After opening the control panel, remember to change the viewing method to a small icon, so that you can see the credential manager. Click View to view all kinds of information. If you want to view a large amount of , you need to enter the account password. Where is the Windows 10 Credential Manager: 1. Open the Control Panel in the system, click the View method in the upper right corner, and convert the type to a small icon. 2. After viewing it as a small icon, click "Certificate Manager". 3. After entering the credential manager, you can see an introduction to the relevant functions, which are mainly used for

Location of Origami Bird at Stardome Railway Crocker Film and Television Park Location of Origami Bird at Stardome Railway Crocker Film and Television Park Mar 27, 2024 pm 11:51 PM

There are a total of 20 origami birds in Croaker Film and Television Park on Star Dome Railway. Many players don’t know where the origami birds are in Crocker Film and Television Park. The editor has summarized the locations of each origami bird to help everyone. Search for it, and take a look at this latest summary of the locations of the origami birds in Croaker Film and Television Park for specific content. Guide to the Honkai Star Dome Railway: Origami Bird in Crook Movie Park Location 1, Crook Movie Park 1st Floor 2, and Crook Movie Park 2nd Floor Star Dome Railway

Understand the location and structure of pip installation package storage Understand the location and structure of pip installation package storage Jan 18, 2024 am 08:23 AM

To learn more about the storage location of packages installed by pip, you need specific code examples. Pip is a commonly used package management tool in the Python language. It is used to easily install, upgrade and manage Python packages. When using pip to install a package, it will automatically download the corresponding package file from PyPI (Python Package Index) and install it to the specified location. So, where are the packages installed by pip stored? This is a problem that many Python developers will encounter. This article will delve into the location of the packages installed by pip and provide

Which has a greater impact on performance, memory frequency or timing? Which has a greater impact on performance, memory frequency or timing? Feb 19, 2024 am 08:58 AM

Memory is one of the most important components in the computer, and it has a significant impact on the performance and stability of the computer. When choosing memory, people tend to focus on two important parameters, namely timing and frequency. So, for memory performance, which is more important, timing or frequency? First, let's understand the concepts of timing and frequency. Timing refers to the time interval required for a memory chip to receive and process data. It is usually represented by a CL value (CASLatency). The smaller the CL value, the faster the memory processing speed. The frequency is within

Using Apple's Check-In Feature: A Guide to the Messages App in iOS 17 Using Apple's Check-In Feature: A Guide to the Messages App in iOS 17 Sep 14, 2023 pm 09:13 PM

Apple in iOS 17 has added a new feature in Messages to let loved ones know when you're home safely. It's called check-in, and here's how you use it. Whether you're walking home after dark or going for an early morning run, you can start checking in with family or friends in Apple's Messages app to let them know when you're home safely. Upon your arrival, CheckIn automatically detects when you are home and notifies your friends. When they are alerted and the check-in has ended, you will also be notified. If something unexpected happens and you're delayed en route, CheckTab will even recognize that you're not making progress and check in with you, asking if you want to increase your ETA. if you don't have

See all articles