Sort by

recency

|

960 Discussions

|

  • + 0 comments

    My c++ solution using map, here is the explanation : https://youtu.be/0-xHzWDVAME

    int anagram(string s) {
        if(s.size() % 2 == 1) return -1;
        map<char, int> mp;
        int ans = 0;
        for(int i = 0; i < s.size() / 2; i++) mp[s[i]]++;
        for(int i = s.size() / 2; i < s.size(); i++){
            if(mp[s[i]] != 0) mp[s[i]]--;
            else ans++;
        }
        return ans;
    }
    
  • + 0 comments

    include

    using namespace std;

    int test(string s) { if (s.length() % 2 == 1) return -1; int n = s.length(); map mp; for (int i = 0; i < n / 2; i++) mp[s[i]]++; for (int i = n / 2; i < n; i++) if (mp[s[i]] > 0) mp[s[i]]--; int dem = 0; for (auto it : mp) dem += it.second; return dem; }

    int main() { int t; cin >> t; while (t--) { string s; cin >> s; cout << test(s) << endl; } return 0; }

  • + 0 comments

    Java 8 This is one of the easiest way to solve this problem.

     public static int anagram(String s) {
            int n = s.length();
            
            if(n % 2 != 0){
                return -1;
            }
            
            //Split the main string
            String left = s.substring(0, n/2);
            String right = s.substring(n/2, n);
            
            //Iterate the left string to verify if the letters are contained into the 
            // right string
            for(int i = 0; i < n/2; i++){
                if(right.contains(left.substring(i, i + 1))){
                    right = right.replaceFirst(left.substring(i, i + 1), "");
                }
            }
            return right.length();
            
            
    
        }
    
  • + 0 comments

    for Python3 Platform

    def anagram(s):
        if (len(s) % 2 != 0):
            return -1
        else:
            lst = list(s[len(s)//2:])
            
            for i in s[:len(s)//2]:
                if (i in lst):
                    lst.remove(i)
            
            return len(lst)
    
    q = int(input())
    for j in range(q):
        s = input()
        
        result = anagram(s)
        
        print(result)
    
  • + 0 comments
    def anagram(s):
        n = len(s)
        if n % 2 != 0:
            return -1
        A = s[:n//2]
        B = list(s[n//2:])
        for a in A:
            if a in B:
                B.remove(a)
        return len(B)