Sort by

recency

|

523 Discussions

|

  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/khDcPEl6to0

    string happyLadybugs(string b) {
        vector<int> occ(26, 0);
        bool happy = true, underscore = false;
        b = "0"+b+"0";
        for(int i = 1; i < b.size()-1; i++){
            if(b[i] != '_' && b[i] != b[i-1] && b[i] != b[i+1]) happy = false;
            if(b[i] == '_') underscore = true;
            else occ[b[i] - 'A']++;
        }
        if(happy) return "YES";
        if(underscore && find(occ.begin(), occ.end(), 1) == occ.end()) return "YES";
        return "NO";
    }
    
  • + 1 comment

    Here is my Python solution! If we have at least one empty square, then we can move any of the ladybugs anywhere. This means that the only cases that work are if there are more than one empty square and an even number of each type, or if there are no empty squares but all the ladybugs are already happy.

    def ishappy(b):
        for bug in range(1, len(b) - 1):
            if b[bug] == b[bug - 1] or b[bug] == b[bug + 1]:
                continue
            else:
                return False
        return True
    
    def happyLadybugs(b):
        ladybugs = set([string for string in b if string != "_"])
        if list(b).count("_") < 1 and not ishappy(b):
            return "NO"
        for ladybug in ladybugs:
            if list(b).count(ladybug) < 2:
                return "NO"
        return "YES"
    
    • + 0 comments

      your code says yes to this, but it should be a no: ZAABBZ

  • + 0 comments

    Python3; After realizing 1 empty field gives you possibility to place any existing color anywhere

    from collections import Counter
    
    def are_happy(b):
        for i in range(len(b)):
            happy = False
            if i > 0:
                if b[i-1] == b[i]:
                    happy = True
            if i < len(b) - 1:
                if b[i+1] == b[i]:
                    happy = True
            if not happy:
                return False
        return True
            
    
    def happyLadybugs(b):
        c = Counter(b)
        if c['_'] < 1:
            if are_happy(b):
                return 'YES'
            else:
                return 'NO'
        
        for key, count in c.most_common()[::-1]:
            if count == 1 and key != '_':
                return 'NO'
            if count > 1:
                return 'YES'
        return 'YES'
    
  • + 0 comments
    unordered_map<char, int> ladybug;
         int flag = 0;
         //counting frequency of character
         for(int i = 0; i< b.size(); i++){
            ladybug[b[i]]++;
         }
         
         //checking the count of the character greater than 1
         if(ladybug.size() > 1){
            for(int i = 0; i< ladybug.size(); i++){
                char c = i+ 65;
                if(ladybug[c]== 1 && c != '_') return "NO";
            }
         }
         
         //checking if any possible chance to switch 
         for(auto i : ladybug){
            if(i.first == '_'){flag = 1; break;}
         }
         
         // checking it is sorted or not if there is no chance to switch
         if(flag == 0){
            for(int i = 0; i< b.size();i++){
                if(b[i] == b[i+1] || b[i] == b[i-1]){
                    flag = 1;
                }else{
                    flag = 0;
                    break;
                }
            }
         }
         
         return  flag == 0 ? "NO" : "YES";
    
  • + 1 comment

    I thought about using a map to count the letters, but even then I would have to know when the letters would be out of order and there would be no space to sort them. I chose to simply count how many consecutive letters are present after sorting the array (if there is free space for that)

    		int count = 0;
    		char[] chars = b.toCharArray();
    
    		if (b.indexOf('_') >= 0)
    			Arrays.sort(chars);
    
    		for (int i = 1; i < chars.length; ++i) {
    			if (chars[i] == '_')
    				break;
    
    			if (chars[i - 1] == chars[i]) {
    				++count;
    			} else if (count == 0) {
    				count = -1;
    				break;
    			} else {
    				count = 0;
    			}
    
    		}
    
    		return count > 0 || chars[0] == '_' ? "YES" : "NO";
    
    • + 1 comment

      It's funny, because applying sorting makes solution already worse than O(nlogn) :D It was not that good idea after all

      • + 0 comments

        Okay, but performance wasn't my goal to resolve this challange.