Game of Thrones - I

Sort by

recency

|

1091 Discussions

|

  • + 0 comments
    public static String gameOfThrones(String str) {
        Map<String, Long> charcount = Arrays.
        stream(str.split("")).
        collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
    
        long count = charcount.entrySet().stream().filter(en -> en.getValue() % 2 != 0).count();
    
        return count==0 || count==1 ? "YES" : "NO";
    
    }
    
  • + 0 comments

    Python solution O(n) time and O(k) space.

    k represent best case scenario worse case will be O(n) where every chars is added to the dictionary.

    def gameOfThrones(s):
        chars = {}
    
        for c in s:
            if c not in chars:
                chars[c] = 0
    
            if chars[c] > 0:
                chars[c] -= 1
            else:
                chars[c] += 1
                
        if sum(chars.values()) <= 1:
            return "YES"
        return "NO"
    
  • + 0 comments

    My python3

    def gameOfThrones(s):
        # Write your code here
        odd_count = {i for i in dict(Counter(s)).items() if i[1]%2!=0}
        return 'YES' if len(odd_count)<2 else 'NO'
    
  • + 0 comments

    My C++ Solution:

    string gameOfThrones(string s) {
        map<char,int> mp;
        for(int i=0;i<s.size();i++){
            mp[s[i]]++;
        }
        int count=0;
        for (const auto& pair : mp) {
            if(pair.second % 2 ==1){
                count++;
            }
            if(count>=2){
                return "NO";
            }
        }
        return "YES";
    }
    
  • + 0 comments

    My C++ Solution: (gist is: To make a string into palindrome - there should not be multiple characters with odd freq)

    string gameOfThrones(string s) {
        char arr[26] ={0};
        bool odd = false; 
        
        for(int i=0; i<s.size(); i++){
            if(arr[s[i]-'a']){
                arr[s[i]-'a']--;
            }else{
                arr[s[i]-'a']++;
            }
        }
        for(int i=0; i<26; i++){
            if(arr[i]%2 != 0 ){
                if( odd == true)
                    return "NO";
                else odd = true;
            }
        }
        return "YES";
    }