- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

# Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character in C++

Suppose we have two strings of equal length, we have to find a minimum number of alterations required to make two strings anagram, without deleting any character. The Anagram is two strings that have the same set of characters. Suppose two strings are “HELLO”, and “WORLD” here number of required changes is 3, as three characters are different in this case.

The idea is simple, we have to find the frequency of each character in the first string, then go through the second string, if characters in the second string are present, in the frequency array, then decrease the frequency value. If the frequency value is less than 0, then increase the final count by 1.

## Example

#include <iostream> using namespace std; int countAlteration(string str1, string str2) { int count = 0; int frequency[26]; for (int i = 0; i < 26; i++){ frequency[i] = 0; } for (int i = 0; i < str1.length(); i++) frequency[str1[i] - 'A']++; for (int i = 0; i < str2.length(); i++){ frequency[str2[i] - 'A']--; if (frequency[str2[i] - 'A'] < 0) count++; } return count; } int main() { string s1 = "HELLO", s2 = "WORLD"; cout << "Number of required alteration: " << countAlteration(s1, s2); }

## Output

Number of required alteration: 3

- Related Articles
- Minimum Number of Steps to Make Two Strings Anagram in C++
- Using Counter() in Python 3.x. to find minimum character removal to make two strings anagram
- Minimum number of given operations required to make two strings equal using C++.
- Find the minimum number of preprocess moves required to make two strings equal in Python
- Program to find minimum swaps required to make given anagram in python
- Program to find minimum number of deletions required from two ends to make list balanced in Python
- Minimum Cost To Make Two Strings Identical in C++
- Program to find minimum number of operations required to make one number to another in Python
- Program to count number of minimum swaps required to make it palindrome in Python
- Minimum Cost to make two Numeric Strings Identical in C++
- Program to count minimum number of operations required to make numbers non coprime in Python?
- Program to find minimum number of operations required to make lists strictly Increasing in python
- Program to find minimum number of operations required to make one string substring of other in Python
- Program to find minimum number of bricks required to make k towers of same height in Python
- Minimum number of given moves required to make N divisible by 25 using C++.

Advertisements