Table of Contents
Let's look at various input and output scenarios for this -
The method used in the following program is as follows
Example
Output
Home Backend Development C++ Rearrange characters to form a palindrome (if possible) in C++

Rearrange characters to form a palindrome (if possible) in C++

Sep 09, 2023 pm 03:57 PM
character rearrange palindrome

Rearrange characters to form a palindrome (if possible) in C++

We are given a string 'str' of any given length. The task is to rearrange the characters so that the output becomes a palindrome string without adding or removing characters from the given input string. A palindrome string is when the characters are arranged in such a way that they sound the same from start to end.

Let's look at various input and output scenarios for this -

Input - String str = "itnin"

Output - If possible, the rearrangement of characters to form a palindrome string is: nitin

Explanation - We are given a variable of type string, assumed to be str. Now we will rearrange the characters of the input string to make it a palindrome string, if not

It will return 'NOT POSSIBLE' if possible. Therefore, the output given the input string is 'nitin'.

Input - String str = "baaaba"

Output - The result of possible character rearrangement to form a palindrome is: aabbaa

Explanation - We are given a variable of string type, assuming it is str. Now we will rearrange the characters of the input string to make it a palindrome string and return 'NOT POSSIBLE' if this is not possible. Therefore, the output given the input string is 'aabbaa'.

The method used in the following program is as follows

  • Input a string type variable, assuming it is str, and calculate the size of the string and store it in a name is the variable of length.

  • Pass the data to the function Rearrangement(str, length).

  • Inside the function Rearrangement(arr, length)

    • Create an unordered_map type variable named 'um', which stores char and integer Type of key-value pairs.

    • Declare an integer type variable total and set it to 0.

    • Create a character type variable 'ch' and string type variables str_1 and str_2.

    • Starts a loop from i to 0 until i is less than length. Inside the loop, set um[str[i]] by incrementing the value 1.

    • Start looping FOR to iterate map 'um'. Inside the loop, check IF it.second % 2 is not equal to 0, then increase total by 1 and set ch to it.first.

    • Check if total is greater than 1 or total = 1 and length % 2 = 0, then 0 will be returned.

    • Start looping FOR to iterate map 'um'. Inside the loop, set str(it.second / 2, it.first) to str, str_1 to str_1 str, and str_2 to str str_2.

    • Check IF total = 1, then return str_1 ch str_2. Otherwise, str_1 str_2 is returned.

  • Print the result.

Example

#include <bits/stdc++.h>
using namespace std;
string Rearrangement(string str, int length){
   unordered_map<char, int> um;
   int total = 0;
   char ch;
   string str_1 = "";
   string str_2 = "";

   for (int i = 0; i < length; i++){
      um[str[i]]++;
   }
   for(auto it : um){
      if(it.second % 2 != 0){
         total++;
         ch = it.first;
      }
   }
   if(total > 1 || total == 1 && length % 2 == 0){
      return 0;
   }
   for(auto it : um){
      string str(it.second / 2, it.first);
      str_1 = str_1 + str;
      str_2 = str + str_2;
   }
   if(total == 1){
      return str_1 + ch + str_2;
   }
   else{
      return str_1 + str_2;
   }
}
int main(){
   string str = "itnin";
   int length = str.size();
   cout<<"Rearrangement of characters to form palindrome if possible is: "<<Rearrangement(str, length);
   return 0;
}
Copy after login

Output

If we run the above code, the following output will be generated

Rearrangement of characters to form palindrome if possible is: nitin
Copy after login

The above is the detailed content of Rearrange characters to form a palindrome (if possible) in C++. 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

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

How to type arrows in Word How to type arrows in Word Apr 16, 2023 pm 11:37 PM

How to use AutoCorrect to type arrows in Word One of the fastest ways to type arrows in Word is to use the predefined AutoCorrect shortcuts. If you type a specific sequence of characters, Word automatically converts those characters into arrow symbols. You can draw many different arrow styles using this method. To type an arrow in Word using AutoCorrect: Move your cursor to the location in the document where you want the arrow to appear. Type one of the following character combinations: If you don't want what you type to be corrected to an arrow symbol, press the backspace key on your keyboard to

How to apply superscript and subscript formatting options in Microsoft Excel How to apply superscript and subscript formatting options in Microsoft Excel Apr 14, 2023 pm 12:07 PM

A superscript is a character or characters, either letters or numbers, that you need to set slightly above the normal line of text. For example, if you need to write 1st, the letter st needs to be slightly higher than the character 1. Likewise, a subscript is a group of characters or a single character and needs to be set slightly lower than normal text level. For example, when you write a chemical formula, you need to place the numbers below the normal line of characters. The following screenshots show some examples of superscript and subscript formatting. Although it may seem like a daunting task, applying superscript and subscript formatting to your text is actually quite simple. In this article, we will explain in some simple steps how to easily format text using superscript or subscript. Hope you enjoyed reading this article. How to apply superscript in Excel

How do you enter extended characters, such as the degree symbol, on iPhone and Mac? How do you enter extended characters, such as the degree symbol, on iPhone and Mac? Apr 22, 2023 pm 02:01 PM

Your physical or numeric keyboard provides a limited number of character options on the surface. However, there are several ways to access accented letters, special characters, and more on iPhone, iPad, and Mac. The standard iOS keyboard gives you quick access to uppercase and lowercase letters, standard numbers, punctuation, and characters. Of course, there are many other characters. You can choose from letters with diacritics to upside-down question marks. You may have stumbled upon a hidden special character. If not, here's how to access them on iPhone, iPad, and Mac. How to Access Extended Characters on iPhone and iPad Getting extended characters on your iPhone or iPad is very simple. In "Information", "

Use java's Character.isDigit() function to determine whether a character is a number Use java's Character.isDigit() function to determine whether a character is a number Jul 27, 2023 am 09:32 AM

Use Java's Character.isDigit() function to determine whether a character is a numeric character. Characters are represented in the form of ASCII codes internally in the computer. Each character has a corresponding ASCII code. Among them, the ASCII code values ​​corresponding to the numeric characters 0 to 9 are 48 to 57 respectively. To determine whether a character is a number, you can use the isDigit() method provided by the Character class in Java. The isDigit() method is of the Character class

Correct way to display Chinese characters in matplotlib Correct way to display Chinese characters in matplotlib Jan 13, 2024 am 11:03 AM

Correctly displaying Chinese characters in matplotlib is a problem often encountered by many Chinese users. By default, matplotlib uses English fonts and cannot display Chinese characters correctly. To solve this problem, we need to set the correct Chinese font and apply it to matplotlib. Below are some specific code examples to help you display Chinese characters correctly in matplotlib. First, we need to import the required libraries: importmatplot

Modify a string by rearranging vowels according to their index position in the string Modify a string by rearranging vowels according to their index position in the string Sep 06, 2023 pm 06:53 PM

In this article, we will discuss how to modify a given string in C++ by rearranging the vowels alphabetically at their respective indices. We will also explain the methods used to solve this problem and provide examples with test cases. Problem Statement Given a string, rearrange the vowels at their respective indices in alphabetical order. Consonants in the string should maintain their original order. For example, given the string "tutorialspoint", the output should be "tatiriolspount". Method This problem can be solved using a simple algorithm. We can first create a separate string containing all the vowels in the given string in their respective order. We can then sort that string alphabetically. at last,

How to use Golang to determine whether a character is a letter How to use Golang to determine whether a character is a letter Dec 23, 2023 am 11:57 AM

How to use Golang to determine whether a character is a letter. In Golang, determining whether a character is a letter can be achieved by using the IsLetter function in the Unicode package. The IsLetter function checks whether the given character is a letter. Next, we will introduce in detail how to use Golang to write code to determine whether a character is a letter. First, you need to create a new Go file in which to write the code. You can name the file "main.go". code

Regarding the character representation of the Enter key in Java, which one is it? Regarding the character representation of the Enter key in Java, which one is it? Mar 29, 2024 am 11:48 AM

The character representation of the Enter key in Java is `. In Java, ` represents a newline character, and when this character is encountered, the text output will wrap. Here is a simple code example that demonstrates how to use `` to represent the Enter key: publicclassMain{publicstaticvoidmain(String[]args){System.out.println("This is the first line of this

See all articles