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

Given two strings s1 and s2, we need to find the minimum number of manipulations required to make two strings anagram without deleting any character.
Note:- The anagram strings have same set of characters, sequence of characters can be different.
If deletion of character is allowed and cost is given, refer to Minimum Cost To Make Two Strings Identical

Question Source: Yatra.com Interview Experience | Set 7
Examples:

```Input :
s1 = "aba"
s2 = "baa"
Output : 0
Explanation: Both String contains identical characters

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.
```

Assumption: Length of both the Strings is considered similar

## C++

 `// C++ Program to find minimum number  ` `// of manipulations required to make  ` `// two strings identical  ` `#include   ` `using` `namespace` `std;  ` ` `  `    ``// Counts the no of manipulations  ` `    ``// required  ` `    ``int` `countManipulations(string s1, string s2)  ` `    ``{  ` `         `  `        ``int` `count = 0;  ` ` `  `        ``// store the count of character  ` `        ``int` `char_count;  ` `         `  `        ``for` `(``int` `i = 0; i < 26; i++){ ` `            ``char_count[i] = 0; ` `        ``} ` ` `  `        ``// iterate though the first String  ` `        ``// and update count  ` `        ``for` `(``int` `i = 0; i < s1.length(); i++)  ` `            ``char_count[s1[i] - ``'a'``]++;  ` ` `  `        ``// iterate through the second string  ` `        ``// update char_count.  ` `        ``// if character is not found in  ` `        ``// char_count then increase count  ` `        ``for` `(``int` `i = 0; i < s2.length(); i++){  ` `            ``char_count[s2[i] - ``'a'``]--; ` `            ``if` `(char_count[s2[i] - ``'a'``] < 0)  ` `                ``count++;  ` `        ``} ` `        ``return` `count;  ` `    ``}  ` ` `  `    ``// Driver code  ` `    ``int` `main()  ` `    ``{  ` ` `  `        ``string s1 = ``"ddcf"``;  ` `        ``string s2 = ``"cedk"``;  ` `         `  `        ``cout<

## Java

 `// Java Program to find minimum number of manipulations ` `// required to make two strings identical ` `public` `class` `Similar_strings { ` ` `  `    ``// Counts the no of manipulations required ` `    ``static` `int` `countManipulations(String s1, String s2) ` `    ``{ ` `        ``int` `count = ``0``; ` ` `  `        ``// store the count of character ` `        ``int` `char_count[] = ``new` `int``[``26``]; ` ` `  `        ``// iterate though the first String and update  ` `        ``// count ` `        ``for` `(``int` `i = ``0``; i < s1.length(); i++)  ` `            ``char_count[s1.charAt(i) - ``'a'``]++;         ` ` `  `        ``// iterate through the second string ` `        ``// update char_count. ` `        ``// if character is not found in char_count ` `        ``// then increase count ` `        ``for` `(``int` `i = ``0``; i < s2.length(); i++)  ` `            ``if` `(char_count[s2.charAt(i) - ``'a'``]-- <= ``0``) ` `                ``count++; ` `         `  `        ``return` `count; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` ` `  `        ``String s1 = ``"ddcf"``; ` `        ``String s2 = ``"cedk"``; ` `        ``System.out.println(countManipulations(s1, s2)); ` `    ``} ` `} `

## Python 3

 `# Python3 Program to find minimum number  ` `# of manipulations required to make  ` `# two strings identical  ` ` `  `# Counts the no of manipulations  ` `# required  ` `def` `countManipulations(s1, s2):  ` `     `  `    ``count ``=` `0` ` `  `    ``# store the count of character  ` `    ``char_count ``=` `[``0``] ``*` `26` `     `  `    ``for` `i ``in` `range``(``26``): ` `        ``char_count[i] ``=` `0` ` `  `    ``# iterate though the first String  ` `    ``# and update count  ` `    ``for` `i ``in` `range``(``len``( s1)):  ` `        ``char_count[``ord``(s1[i]) ``-`  `                   ``ord``(``'a'``)] ``+``=` `1` ` `  `    ``# iterate through the second string  ` `    ``# update char_count.  ` `    ``# if character is not found in  ` `    ``# char_count then increase count  ` `    ``for` `i ``in` `range``(``len``(s2)):  ` `        ``char_count[``ord``(s2[i]) ``-` `ord``(``'a'``)] ``-``=` `1` `        ``if` `(char_count[``ord``(s2[i]) ``-`  `                       ``ord``(``'a'``)] < ``0``) : ` `            ``count ``+``=` `1` ` `  `    ``return` `count ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"``:  ` ` `  `    ``s1 ``=` `"ddcf"` `    ``s2 ``=` `"cedk"` `     `  `    ``print``(countManipulations(s1, s2)) ` ` `  `# This code is contributed by ita_c `

## C#

 `// C# Program to find minimum number ` `// of manipulations required to make ` `// two strings identical ` `using` `System; ` ` `  `public` `class` `GFG { ` ` `  `    ``// Counts the no of manipulations ` `    ``// required ` `    ``static` `int` `countManipulations(``string` `s1, ` `                                  ``string` `s2) ` `    ``{ ` `        ``int` `count = 0; ` ` `  `        ``// store the count of character ` `        ``int` `[]char_count = ``new` `int``; ` ` `  `        ``// iterate though the first String ` `        ``// and update count ` `        ``for` `(``int` `i = 0; i < s1.Length; i++)  ` `            ``char_count[s1[i] - ``'a'``]++;  ` ` `  `        ``// iterate through the second string ` `        ``// update char_count. ` `        ``// if character is not found in ` `        ``// char_count then increase count ` `        ``for` `(``int` `i = 0; i < s2.Length; i++)  ` `            ``if` `(char_count[s2[i] - ``'a'``]-- <= 0) ` `                ``count++; ` `         `  `        ``return` `count; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main() ` `    ``{ ` ` `  `        ``string` `s1 = ``"ddcf"``; ` `        ``string` `s2 = ``"cedk"``; ` `         `  `        ``Console.WriteLine( ` `            ``countManipulations(s1, s2)); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m.  `

## PHP

 ` `

Output:

```2
```

Time Complexity : O(n), where n is the length of the string.
This article is contributed by Sumit Ghosh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

5

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.