Sort by

recency

|

517 Discussions

|

  • + 0 comments

    My Java Solution

    public static String happyLadybugs(String b) {
            String[] bugArr = b.split("");
            if(b.contains("_")){
                Arrays.sort(bugArr);
            }
            bugArr = Arrays.stream(bugArr)
                    .filter(s -> !s.equals("_"))
                    .toArray(String[]::new);
            boolean isPresent = false;
    
            if(bugArr.length == 0){
                return "YES";
            }
    
            if(bugArr.length > 1){
                if(!bugArr[0].equalsIgnoreCase(bugArr[1])
                        || !bugArr[bugArr.length-1].equalsIgnoreCase(bugArr[bugArr.length-2])){
                    return "NO";
                }
                for(int i=1; i<bugArr.length-1; i++){
                    String currentElement = bugArr[i];
                    if(currentElement.equals(bugArr[i-1]) || currentElement.equals(bugArr[i+1])){
                        isPresent = true;
                    }else{
                        isPresent = false;
                        break;
                    }
                }
            }
    
            return isPresent ? "YES" : "NO";
        }
    
  • + 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";
    }
    
  • + 0 comments

    Java soluation using HashMap:

     public static String happyLadybugs(String b) {
            HashMap<Character, Integer> colors = new HashMap<>();
            int n = b.length();
            boolean contain_underscore = false;
            boolean same_color_adjacent = true;
            char prev = ' ';
            
            for(int i = 0; i < n; i++){
                char c = b.charAt(i);
                if(c == '_') contain_underscore = true;
             
                else{
                  if (i > 0 && !contain_underscore && c != prev && colors.get(prev) <= 1)
                       same_color_adjacent = false; 
                  prev = c;
                    
                    
                  if(!colors.containsKey(c))
                        colors.put(c, 1);
                  else
                        colors.replace(c, colors.get(c) + 1);  
                  }
                 
                }
            
            if(!contain_underscore && !same_color_adjacent)
                return "NO";
            
            for(Integer val:colors.values())
                if(val <= 1)
                    return "NO";
            
            return "YES"; 
            
            }
    
  • + 0 comments

    from collections import Counter

    def happyLadybugs(b):

    count_b = Counter(b)
    mc = count_b.most_common()
    if len(mc)!=1 and ((mc[-1][1]== 1 and mc[-1][0]!= '_') or mc[-2][1]== 1):
        return 'NO'
    else:
        if '_' in b:
            return 'YES'
        for i in mc:
            if count_b[i[0]]==1 or b.rfind(i[0])-b.find(i[0])+1 != count_b[i[0]]:
                return 'NO'
            if i == mc[-1]:
                return 'YES'
    
  • + 0 comments

    The solution is a bit tedious due to the number of cases to account for. Not sure I would call this one "easy".