search
HomeBackend DevelopmentC++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

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"

Output

Even

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"

Output

Odd

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

enter

alpha = "pqqqqrrr";

Output

Even

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;
}

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 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;
}

Output

The parity of given string's character's is EVEN

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
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
详细介绍如何在Windows 11上打开环境变量设置详细介绍如何在Windows 11上打开环境变量设置Dec 30, 2023 pm 06:07 PM

环境变量功能是系统中的配置程序运行必备工具,但是在最新的win11系统中还有许多的用户不知道怎么设置打开,下面就给你们带来了win11环境变量打开位置详细介绍,快来一起学习操作一下吧。win11环境变量在哪:1、首先输入“win+R”,打开运行框。2、然后在里面输入命令:controlsystem。3、在打开的系统信息界面中,选择左侧菜单的“高级系统设置”。4、随后在打开的“系统属性”窗口选择下方的“环境变量”选项。5、最后在打开的环境变量中,即可根据需求进行相关的设置。

Win11的启动路径以及如何打开它Win11的启动路径以及如何打开它Jan 03, 2024 pm 11:13 PM

每一个Windows系统都有一个启动路径,如果你在其中添加了文件或软件,就会在开机的时候打开它。不过不少朋友不知道win11启动路径在哪里,其实我们只需要进入C盘的对应文件夹就可以了。win11启动路径:1、双击打开“此电脑”2、直接将该路径“C:\ProgramData\Microsoft\Windows\StartMenu\Programs\Startup”粘贴进路径框。3、这里就是win11启动路径了,如果我们要开机打开文件就可以将文件放进来。4、如果你根据这个路径进不来,可能是被隐藏了。

如何在 Steam 中更改游戏下载位置如何在 Steam 中更改游戏下载位置May 10, 2023 pm 11:22 PM

Steam是PC游戏玩家中最受欢迎的应用程序之一,因为您可以在Steam商店中找到任何主要游戏。它通过其用户界面简化了用户喜爱的游戏的下载、安装和管理。每当Steam用户想要下载游戏时,Steam都会使用应用程序的默认安装目录来下载和安装游戏。此位置默认为C:\ProgramFiles(x86)\Steam。问题来了,因为大多数用户在C盘上没有足够的空间,特别是对于占用大量存储空间的游戏,例如50–100GB。为了克服这个问题,Steam允许用户使用应用程序更改游戏的下载和

windows10凭证管理器在哪里windows10凭证管理器在哪里Jul 09, 2023 am 10:09 AM

凭证管理器是用户用于管理web凭证和Windows凭据的一个作用,可是很多用户还不清楚windows10凭证管理器在哪里。其实凭证管理器就在操作面板上,大家在打开控制面板以后记得将查看方法改成小图标,那样就能见到凭证管理器了,点击查看就能查看各类信息了,如果想要查看大量,就需要输入账户密码。windows10凭证管理器在哪里:1、在系统中打开控制面板,点击右上角的查看方法,将类型转换成小图标。2、以小图标的方式查看以后,点击“凭证管理器”。3、进来凭证管理器以后,能够看见有关作用的介绍,主要用于

了解pip安装包存储的位置和结构了解pip安装包存储的位置和结构Jan 18, 2024 am 08:23 AM

深入了解pip安装的包存放位置,需要具体代码示例pip是Python语言常用的包管理工具,用于方便地安装、升级和管理Python包。在使用pip安装包时,它会自动从PyPI(Python包索引)下载对应的包文件,并将其安装到指定的位置。那么,pip安装的包究竟存放在哪里呢?这是很多Python开发者都会遇到的问题。本文将深入探讨pip安装的包存放位置,并提供

win11关机位置win11关机位置Jan 10, 2024 am 09:14 AM

如果我们要长时间离开电脑,那么最好将电脑关机保护它,那么win11关机在哪里呢,其实一般来说只要打开开始菜单,在其中就可以找到关机按钮了。win11关机在哪里:答:在开始菜单的电源按钮里。1、首先我们点击底部任务栏的“windows徽标”打开“开始菜单”2、打开后,可以在右下角找到“电源”按钮,如图所示。3、点击电源按钮后,就能看到“关机”了,点击它就能关机。4、如果因为死机等特殊情况无法关机,那么可以直接用电脑上的“电源键”长按强制关机。

使用Apple的签入功能:iOS 17中的消息应用指南使用Apple的签入功能:iOS 17中的消息应用指南Sep 14, 2023 pm 09:13 PM

iOS17中的Apple在“信息”中添加了一项新功能,可让您在安全回家时让亲人知道。它被称为签入,这是你如何使用它。无论你是在天黑后步行回家,还是在清晨跑步,你都可以在Apple的“信息”应用中与家人或朋友一起开始签到,让他们知道你何时安全回家。在您到达后,CheckIn会自动检测您何时在家,并通知您的朋友。当他们收到警报并且签入已结束时,您也会收到通知。如果发生意外情况并且您在途中被延误,CheckTab甚至会识别出您没有取得进展并与您一起办理登机手续,询问您是否要增加预计到达时间。如果您没有

详细介绍电脑中的打印机驱动程序位置详细介绍电脑中的打印机驱动程序位置Jan 08, 2024 pm 03:29 PM

很多用户在电脑上安装了打印机驱动程序,但却不知道如何找到它们。因此,今天我为大家带来了详细介绍打印机驱动程序在电脑中的位置,对于还不了解的用户,快来看看吧打印机驱动在电脑哪里找重新撰写内容而不改变原义时,需要将语言改写为中文,不需要出现原句首先,建议使用第三方软件进行搜索2、在右上角找到"工具箱"3、在下方找到并点击“设备管理器”。改写后的句子:3、在底部找到并点击“设备管理器”4、然后打开“打印队列”,然后找到你的打印机设备。此次是你的打印机名称型号。5、右键打印机设备,就能够去更新或者卸载我

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function