Sort by

recency

|

973 Discussions

|

  • + 0 comments
    fun anagram(s: String): Int {
        // Write your code here
        if(s.length % 2 !=0)
            return -1
        var res = 0
        val middle = s.length/2
        var sub1 = s.subSequence(0, middle)
        var sub2 = s.subSequence(middle,s.length)
        var s1 = StringBuilder(sub1)
        var s2 = StringBuilder(sub2)
        for(c in s1){
            if(s2.contains(c)){
                val index = s2.indexOf(c)
                s2.deleteCharAt(index)
            }else
                res++
        }
        return res
    }
    
  • + 0 comments

    rust answer

    fn anagram(s: &str) -> i32 {
        let mut counter = vec![0;26];
        let chs: Vec<char> = s.chars().collect();
        let mut res = 0;
        let mut idx:usize = 0;
        
        if chs.len() % 2 == 1 {
            return -1;
        }
        
        for i in 0..chs.len()/2 {
            idx = (chs[i] as u32 - 'a' as u32) as usize;
            counter[idx] = counter[idx] + 1;
        }
        for i in chs.len()/2..chs.len() {
            idx = (chs[i] as u32 - 'a' as u32) as usize;
            if counter[idx] == 0 {
                res += 1;
            } else {
                counter[idx] = counter[idx] - 1;
            }
        }
        
        res
    
    }
    
  • + 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;
    }