Making Anagrams

Sort by

recency

|

761 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

    Perl:

    sub makingAnagrams {
        my $s1 = shift;
        my $s2 = shift;
        my (%h1, %h2);
        my $cnt = 0;
        my $sum = 0;
        
        foreach (split("", $s1)) {
            $h1{$_} += 1;
        }
        foreach (split("", $s2)) {
            $h2{$_} += 1;
        }
        foreach my $k (keys %h1) {
            if (exists($h2{$k})) {
                $cnt += abs($h1{$k} - $h2{$k});
                delete $h1{$k};
                delete $h2{$k};
            }
            else {
                $cnt += $h1{$k};
                delete $h1{$k};
            }        
        }
        foreach my $k2 (keys %h2) {
            $sum += $h2{$k2};
        }
        return $cnt + $sum;
    }
    
  • + 0 comments

    My answer with Typescript, simple

    function makingAnagrams(s1: string, s2: string): number {
        // 0. define hash [m]
        let m = new Map<string, number>()
    
        // 1. counting [c]+ in [s1]
        // 2. counting [c]- in [s2]
        for (let c of s1) m.set(c, (m.get(c) || 0) + 1)
        for (let c of s2) m.set(c, (m.get(c) || 0) - 1)
    
        // 3. return sum of diff of each character between [s1]&[s2] that counted in [m]
        return Array.from(m.values()).reduce((p, c) => p + Math.abs(c), 0)
    }
    
  • + 0 comments
    from collections import Counter
    
    def makingAnagrams(s1, s2):
        count_s1 = Counter(s1)
        count_s2 = Counter(s2)
        dels = 0
        all_chars = set(count_s1.keys()).union(set(count_s2.keys()))
        for char in all_chars:
            dels += abs(count_s1[char] - count_s2[char])
        return dels
    
  • + 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;
    
        }