Sort by

recency

|

525 Discussions

|

  • + 0 comments
    public static String happyLadybugs(String str) {
    
        if(Arrays.stream(str.split("")).distinct().allMatch(s -> s.equals("_"))) return "YES";
    
        Map<String, Long> map = Arrays.
                stream(str.split("")).
                collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
    
        if(map.entrySet().stream().filter(en -> !en.getKey().equals("_")).anyMatch(en -> en.getValue()==1L)) 
            return "NO";
        if(str.indexOf("_") == -1) {
            for(int i=0;i<str.length();i++) {
                if(i==0) {
                    if(str.charAt(i) != str.charAt(i+1)) return "NO";
                } else if(i==str.length()-1) {
                    if(str.charAt(i) != str.charAt(i-1)) return "NO";
                } else {
                    if(str.charAt(i) != str.charAt(i+1) && 
                            str.charAt(i) != str.charAt(i-1)) return "NO";
                }
            }
        }
    
        return "YES";
    }
    
  • + 0 comments

    Here is problem solution in Python, java, c++, c and javascript -https://programmingoneonone.com/hackerrank-happy-ladybugs-problem-solution.html

  • + 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

    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'