Sort by

recency

|

971 Discussions

|

  • + 0 comments

    c++ answer

    include

    using namespace std;

    string ltrim(const string &); string rtrim(const string &);

    /* * Complete the 'anagram' function below. * * The function is expected to return an INTEGER. * The function accepts STRING s as parameter. */

    int anagram(string s) { int n = s.length();

    // If the length is odd, it's impossible to split into two equal halves.
    if (n % 2 != 0) {
        return -1;
    }
    
    int half_len = n / 2;
    string s1 = s.substr(0, half_len);
    string s2 = s.substr(half_len);
    
    // Frequency arrays for characters 'a' through 'z'
    vector<int> freq1(26, 0);
    vector<int> freq2(26, 0);
    
    // Populate frequency for s1
    for (char c : s1) {
        freq1[c - 'a']++;
    }
    
    // Populate frequency for s2
    for (char c : s2) {
        freq2[c - 'a']++;
    }
    
    int changes = 0;
    // Compare frequencies to find characters that need to be changed in s1
    // We only need to count characters that are in s1 but not (or less) in s2
    for (int i = 0; i < 26; ++i) {
        if (freq1[i] > freq2[i]) {
            changes += (freq1[i] - freq2[i]);
        }
    }
    
    return changes;
    

    }

    int main() { ofstream fout(getenv("OUTPUT_PATH"));

    string q_temp;
    getline(cin, q_temp);
    
    int q = stoi(ltrim(rtrim(q_temp)));
    
    for (int q_itr = 0; q_itr < q; q_itr++) {
        string s;
        getline(cin, s);
    
        int result = anagram(s);
    
        fout << result << "\n";
    }
    
    fout.close();
    
    return 0;
    

    }

    string ltrim(const string &str) { string s(str);

    s.erase(
        s.begin(),
        find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
    );
    
    return s;
    

    }

    string rtrim(const string &str) { string s(str);

    s.erase(
        find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
        s.end()
    );
    
    return s;
    

    }

  • + 0 comments

    My Intuition based easy C++ solution:

    int anagram(string s) {
        if(s.length()%2 != 0)return -1;
        unordered_map<char,int> mp1;
        unordered_map<char,int> mp2;
        int changes = 0;
        string s1 = s.substr(0,s.size()/2);
        string s2 = s.substr(s.size()/2);
        for(int i=0; i<s1.size();i++){
            mp1[s1[i]]++;
            mp2[s2[i]]++;
        }
        for(auto key : mp1){
            if(mp2.find(key.first) == mp2.end()){
                changes += key.second;
            }else if(key.second > mp2[key.first]){
                changes += key.second - mp2[key.first];
            }
        }
        return changes;
    }
    
  • + 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;
    }
    
  • + 1 comment

    My Java solution:

    public static int anagram(String s) {
            if (s.length() % 2 != 0) return -1; // If length is odd, return -1
    
            int[] frequency = new int[26]; // Array for character frequencies
    
            int mid = s.length() / 2;
            for (int i = 0; i < mid; i++) {
                frequency[s.charAt(i) - 'a']++;  // Count chars in first half
                frequency[s.charAt(i + mid) - 'a']--; // Subtract chars in second half
            }
    
            int changes = 0;
            for (int count : frequency) {
                if (count > 0) changes += count; // Count excess chars that need replacement
            }
    
            return changes;
        }
    
  • + 0 comments

    Here is my Python solution!

    def anagram(s):
        if len(s) % 2 == 1:
            return -1
        s1 = list(s[:len(s) // 2])
        s2 = list(s[len(s) // 2:])
        letters = set(s1 + s2)
        same = 0
        for letter in letters:
            same += min(s1.count(letter), s2.count(letter))
        return (len(s1) + len(s2) - 2 * same) // 2