Making Anagrams

Sort by

recency

|

758 Discussions

|

  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/B4pZbX0VzzU

    #include <bits/stdc++.h>
    
    using namespace std;
    
    int main()
    {
        string fstr, sstr;
        cin >> fstr; cin >> sstr;
        vector<int>fsc(26,0), ssc(26,0);
        int i, result = 0;
        for(i = 0; i < fstr.size(); i++) fsc[fstr[i] - 'a']++;
        for(i = 0; i < sstr.size(); i++) ssc[sstr[i] - 'a']++;
        for(i = 0; i < 26; i++) result += abs(fsc[i] - ssc[i]);
        cout << result << endl;
        return 0;
    }
    
  • + 0 comments

    Java solution based on calc the number of common charcters of each string

    public static int makingAnagrams(String s1, String s2) {
        int commonCount = 0;
        HashMap<Character, Integer> map = new HashMap<>();
        int n = s1.length();
        int m = s2.length();
        
        for(int i = 0; i < n; i++){
          char c = s1.charAt(i);
          map.put(c, map.getOrDefault(c, 0) + 1 );
          }
        
        for(int i = 0; i < m; i++){
          char c = s2.charAt(i);
          if( map.containsKey(c) && map.get(c) > 0){
            commonCount++;
            map.put(c, map.get(c) - 1);
            }
          }
        
        return n + m - 2 * commonCount;
    
        }
    
  • + 0 comments

    I made an efficient code .Instead of using multiple list comprehensions and sum calls, I streamlined it by reading the lines, processing them, and calculating the differences in one go. This approach minimizes intermediate lists and leverages efficient operations.

    import sys from collections import Counter

    Read lines from stdin

    lines = [sys.stdin.readline().strip() for _ in range(2)]

    Count characters in each line

    counts = [Counter(line) for line in lines]

    Calculate the total difference in counts for each letter

    diff = 0 for letter in "abcdefghijklmnopqrstuvwxyz": diff += abs(counts[0].get(letter, 0) - counts[1].get(letter, 0))

    print(diff)

  • + 0 comments

    for Python3 Platform

    def makingAnagrams(s1, s2):
        samechar = 0
        lst = list(s2)
        
        for i in s1:
            if (i in lst):
                lst.remove(i)
                samechar += 1
        
        return len(lst) + len(s1) - samechar
    
    s1 = input()
    s2 = input()
    
    result = makingAnagrams(s1, s2)
    
    print(result)
    
  • + 0 comments

    I increment the freq for each char from first array and decrement for each char for second array. The common chars therefore will cancel out. The ones that aren't common will either have a positive or negative value. When we add the absolute values we get the total number of different chars. My solution in c / c++. Using either arrays or unordered_map to store the freqeuncy:

    //std::unordered_map freq; const int range = 'z' - 'a' + 1; int freq[range] = {0};

    for(auto elem : s1)
    {
        //freq[elem]++;
        freq[elem - 'a']++;
    }
    for(auto elem : s2)
    {
        //freq[elem]--;
        freq[elem - 'a']--;
    }
    
    int sum = 0;
    //for(auto itr = freq.begin(); itr != freq.end(); itr++)
    for(auto idx = 0; idx < range; idx++)
    {
        //sum += abs(itr->second);
        sum += abs(freq[idx]);   
    }
    return sum;