Game of Thrones - I

Sort by

recency

|

1084 Discussions

|

  • + 0 comments

    Here is my c++ solution : explanation here : https://youtu.be/yhSaL48IHds

    solution 1 :

    string gameOfThrones(string s) {
        map<char, int> mp;
        for(int i = 0; i < s.size(); i++){
            if(mp[s[i]] != 0){
                mp[s[i]]--;
                if(mp[s[i]] == 0) mp.erase(s[i]);
            }
            else mp[s[i]]++;
        }
        if(mp.size() > 1) return "NO";
        return "YES";
    }
    

    solution 2 :

    string gameOfThrones(string s) {
        map<char, int> mp;
        for(int i = 0; i < s.size(); i++){
            if(mp[s[i]] == 1)  mp.erase(s[i]);
            else mp[s[i]]++;
        }
        if(mp.size() > 1) return "NO";
        return "YES";
    }
    
  • + 0 comments

    Approach to solve:

    1 Count Character Frequencies Use a hash map (unordered_map) or array (int freq[26]) to store how many times each character appears in the given string.

    2 Count Odd Frequency Characters Traverse through the frequency table and count how many characters have an odd frequency.

    3 Check Conditions for a Palindrome If odd frequency count <= 1, the string can be rearranged into a palindrome --> Return "YES". Otherwise, return "NO".

  • + 0 comments

    Here is my Python solution!

    def gameOfThrones(s):
        s = list(s)
        if len(s) % 2 == 0:
            for string in set(s):
                if s.count(string) % 2 == 1:
                    return "NO"
            return "YES"
        chances = 1
        for string in set(s):
            if s.count(string) % 2 == 1:
                chances -= 1
        return "YES" if chances >= 0 else "NO"
    
  • + 0 comments
    string gameOfThrones(string s) {
     map<char,int> mp;
     for(char i:s){
        mp[i]++;
     }
     int count =0;
     for(int i=0;i<mp.size();i++){
        if(mp[i]%2!=0)count++;
     }
     if(count>1)return "NO";
     else return "YES";
    }
    
  • + 0 comments

    string gameOfThrones(string s) { map mp; for(char i:s){ mp[i]++; } int count =0; for(int i=0;i1)return "NO"; else return "YES"; }