Making Anagrams

Sort by

recency

|

757 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

    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;
    
  • + 0 comments

    Javascript (Typescript)

    function makingAnagrams(s1: string, s2: string): number {
        let str1: string = s1;
        let str2: string = s2;
    
        for(let i = 0; i < str1.length; i++) {
            let index = str2.indexOf(str1[i]);
            if(index !== -1) {
                str1 = str1.substring(0, i) + str1.substring(i + 1);
                str2 = str2.substring(0, index) + str2.substring(index + 1);
                i--;
            }
        }
        return str1.length + str2.length;
    

    }