- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Minimum Number of Steps to Make Two Strings Anagram in C++
Suppose we have two equal-size strings s and t. In one step we can choose any character of t and replace it with another character. We have to find the minimum number of steps required to make t an anagram of s. Note: An Anagram of a string is a string that contains the same characters with a different (or the same) ordering.
So if the input is like - “yxy” and “xyx”, then the output will be 1, as only one character is needed to be replaced.
To solve this, we will follow these steps −
n := size of characters in s
make a map m, and fill this with the frequency of each character present in s, make another map m2, and fill this with the frequency of each character present in t
ret := n
for each key-value pair it in m
x := minimum of value of it, and value of m2[value of it]
decrease ret by 1
return ret
Example (C++)
Let us see the following implementation to get a better understanding −
#include <bits/stdc++.h> using namespace std; class Solution { public: int minSteps(string s, string t) { int n = s.size(); map <char, int> m1; for(int i = 0; i < s.size(); i++){ m1[s[i]]++; } int ret = n; map <char, int> m2; for(int i = 0; i < t.size(); i++){ m2[t[i]]++; } map <char, int> :: iterator it = m1.begin(); while(it != m1.end()){ //cout << it->first << " " << it->second << " " << m2[it->first] << endl; int x = min(it->second, m2[it->first]); ret -= x; it++; } return ret; } }; main(){ Solution ob; cout << (ob.minSteps("yxy", "xyx")); }
Input
"yxy" "xyx"
Output
1
- Related Articles
- Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character 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++.
- Minimum Cost To Make Two Strings Identical in C++
- Minimum Cost to make two Numeric Strings Identical in C++
- Find the minimum number of preprocess moves required to make two strings equal in Python
- Minimum Swaps to Make Strings Equal in C++
- Minimum Insertion Steps to Make a String Palindrome in C++
- Check if two strings are anagram of each other using C++
- Program to find minimum swaps required to make given anagram in python
- Java Program to Check if two strings are anagram
- Golang Program to Check if two Strings are Anagram
- Program to find minimum deletions to make strings strings in Python
- Finding minimum steps to make array elements equal in JavaScript
- Minimum steps to make all the elements of the array divisible by 4 in C++
