Least number of manipulations needed to ensure two strings have identical characters

Given two strings return the value of least number of manipulations needed to ensure both strings have identical characters, i.e., both string become anagram of each other.
Examples:Â
Input : s1 = "aab"
s2 = "aba"
Output : 2
Explanation : string 1 contains 2 a's and 1 b,
also string 2 contains same characters
Input : s1 = "abc"
s2 = "cdd"
Output : 2
Explanation : string 1 contains 1 a, 1 b, 1 c
while string 2 contains 1 c and 2 d's
so there are 2 different characters
Question Source : Yatra.com Interview Experience | Set 7
The idea is to create a extra count array for both the strings separately and then count the difference in characters.Â
Implementation:
C++
// C++ program to count least number// of manipulations to have two strings// set of same characters#include <bits/stdc++.h>using namespace std;Â
const int MAX_CHAR = 26;Â
// return the count of manipulations// requiredint leastCount(string s1, string s2, int n){Â Â Â Â int count1[MAX_CHAR] = { 0 };Â Â Â Â int count2[MAX_CHAR] = { 0 };Â
    // count the number of different    // characters in both strings    for (int i = 0; i < n; i++) {        count1[s1[i] - 'a'] += 1;        count2[s2[i] - 'a'] += 1;    }Â
    // check the difference in characters    // by comparing count arrays    int res = 0;    for (int i = 0; i < MAX_CHAR; i++) {        if (count1[i] != 0) {            res += abs(count1[i] - count2[i]);        }    }    return res;}Â
// driver programint main(){Â Â Â Â string s1 = "abc";Â Â Â Â string s2 = "cdd";Â Â Â Â int len = s1.length();Â Â Â Â int res = leastCount(s1, s2, len);Â Â Â Â cout << res << endl;Â Â Â Â return 0;} |
Java
// Java program to count least number// of manipulations to have two// strings set of same charactersimport java.io.*;Â
public class GFG {Â
    static int MAX_CHAR = 26;Â
    // return the count of manipulations    // required    static int leastCount(String s1,                        String s2, int n)    {                 int[] count1 = new int[MAX_CHAR];        int[] count2 = new int[MAX_CHAR];Â
        // count the number of different        // characters in both strings        for (int i = 0; i < n; i++)         {            count1[s1.charAt(i) - 'a'] += 1;            count2[s2.charAt(i) - 'a'] += 1;        }Â
        // check the difference in characters        // by comparing count arrays        int res = 0;                 for (int i = 0; i < MAX_CHAR; i++)        {            if (count1[i] != 0) {                res += Math.abs(count1[i]                                  - count2[i]);            }        }                 return res;    }Â
    // driver program    static public void main(String[] args)    {        String s1 = "abc";        String s2 = "cdd";        int len = s1.length();        int res = leastCount(s1, s2, len);                 System.out.println(res);    }}Â
// This code is contributed by vt_m. |
Python3
# Python3 program to count least number# of manipulations to have two strings# set of same charactersMAX_CHAR = 26Â
# return the count of manipulations# requireddef leastCount(s1, s2, n):Â
    count1 = [0] * MAX_CHAR    count2 = [0] * MAX_CHARÂ
    # count the number of different    # characters in both strings    for i in range ( n):        count1[ord(s1[i]) - ord('a')] += 1        count2[ord(s2[i]) - ord('a')] += 1Â
    # check the difference in characters    # by comparing count arrays    res = 0    for i in range (MAX_CHAR):        if (count1[i] != 0):            res += abs(count1[i] - count2[i])         return resÂ
# Driver Codeif __name__ == "__main__":Â
    s1 = "abc"    s2 = "cdd"    l = len(s1)    res = leastCount(s1, s2, l)    print (res)Â
# This code is contributed by ita_c |
C#
// C# program to count least number// of manipulations to have two strings// set of same charactersusing System;Â
public class GFG {Â
    static int MAX_CHAR = 26;Â
    // return the count of manipulations    // required    static int leastCount(string s1,                         string s2, int n)    {                 int[] count1 = new int[MAX_CHAR];        int[] count2 = new int[MAX_CHAR];Â
        // count the number of different        // characters in both strings        for (int i = 0; i < n; i++)        {            count1[s1[i] - 'a'] += 1;            count2[s2[i] - 'a'] += 1;        }Â
        // check the difference in characters        // by comparing count arrays        int res = 0;        for (int i = 0; i < MAX_CHAR; i++)        {            if (count1[i] != 0) {                res += Math.Abs(count1[i]                                 - count2[i]);            }        }                 return res;    }Â
    // driver program    static public void Main()    {        string s1 = "abc";        string s2 = "cdd";        int len = s1.Length;        int res = leastCount(s1, s2, len);        Console.WriteLine(res);    }}Â
// This code is contributed by vt_m. |
Javascript
<script>Â
// Javascript program to count least number// of manipulations to have two// strings set of same characterslet MAX_CHAR = 26;Â
// Return the count of manipulations// requiredfunction leastCount(s1, s2, n){    let count1 = new Array(MAX_CHAR);    let count2 = new Array(MAX_CHAR);         for(let i = 0; i < MAX_CHAR; i++)    {        count1[i] = 0;        count2[i] = 0;    }         // Count the number of different    // characters in both strings    for(let i = 0; i < n; i++)     {        count1[s1[i].charCodeAt(0) -                  'a'.charCodeAt(0)] += 1;        count2[s2[i].charCodeAt(0) -                  'a'.charCodeAt(0)] += 1;    }Â
    // Check the difference in characters    // by comparing count arrays    let res = 0;           for(let i = 0; i < MAX_CHAR; i++)    {        if (count1[i] != 0)         {            res += Math.abs(count1[i] - count2[i]);        }    }    return res;}Â
// Driver Codelet s1 = "abc";let s2 = "cdd";let len = s1.length;let res = leastCount(s1, s2, len);Â
document.write(res);Â
// This code is contributed by avanitrachhadiya2155Â Â Â Â Â </script> |
Output
2
If you like zambiatek and would like to contribute, you can also write an article using write.zambiatek.co.uk or mail your article to review-team@zambiatek.co.uk. See your article appearing on the zambiatek main page and help other Geeks.Â
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!


