Given two strings s1 and s2 of equal length and containing lowercase characters, find the minimum number of manipulations required to make two strings anagram without deleting any character. In each modification, we can change any one character from either string.
Examples:
Input : s1 = "aba", s2 = "baa"
Output : 0
Explanation: Strings are already anagrams.Input : s1 = "ddcf", s2 = "cedk"
Output : 2
Explanation : Here, we need to change two characters in either of the strings to make them identical. We
can change 'd' and 'f' in s1 or 'e' and 'k' in s2.
Table of Content
[Naive Approach] Using Sorting
The idea is to sort both strings and then compare them character by character to count the number of mismatches. Since each mismatch represents a manipulation needed to make the strings anagrams, the total count of mismatches is divided by 2 because each manipulation fixes two mismatches (one in each string).
#include <iostream>
using namespace std;
int minManipulation(string s1, string s2) {
// Sort the characters of both strings
sort(s1.begin(), s1.end());
sort(s2.begin(), s2.end());
int i = 0, j = 0, count = 0;
// Compare characters in sorted strings
while (i < s1.size() && j < s2.size()) {
if (s1[i] == s2[j]) {
i++;
j++;
}
else if (s1[i]