Sort by

recency

|

519 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";
    }
    
  • + 0 comments

    C++ Solution:

    struct p {
      int cnt = 0;
      int lp = 0;
    };
    
    string happyLadybugs(string b) {
      
      map<char, p> m;
    
      bool right = true;
      int pos = 0;
      for(auto& a : b) {
        m[a].cnt += 1;
        if(a != '_' && m[a].lp > 0 &&  m[a].lp != pos-1)
          right = false;
        m[a].lp = pos;
        pos++;
      }
      
      for(auto& a : m){
        if(a.first != '_' && a.second.cnt < 2) {
          return "NO";
        }
      }
      
      if(right)
        return "YES ";
    
      if(m.count('_') == 0)
        if(m.size() == 1 && (*m.begin()).second.cnt > 1)
          return "YES ";
        else
          return "NO";
      else {
        if(m.size() == 2 && 
           (*m.begin()).second.cnt + (*m.begin()++).second.cnt == b.size())
          return "YES ";
      }
    
      return "YES";
    }
    
  • + 1 comment
    /* All the ladybugs are possible to be made happy if there is at least one empty
     * cell (underscore) and there is no color with only one ladybug. First we check
     * if there is any color with only one ladybug. If so we print "NO". Then we
     * check if there is at least one empty cell. If not, the ladybugs must already
     * be happy without moving. If there isn't any empty cell to move the ladybugs
     * around we check if all the initial positions are happy. If not we print "NO".
     * If it passes the tests we print "YES". Either the ladybugs are happy or are
     * possible to be made. In order to determine if there is any color with only
     * one ladybug we need a frequency table to hold how many times each color
     * occurs in the string. You can use an array with a size of 26 each index
     * corresponding to one letter A-Z or a hashmap to store the frequencies of each
     * color.
     */
    
    // WITH ARRAY
    class Result {
      public static String happyLadybugs(String b) {
        String happyString = "YES";
        String notHappyString = "NO";
        boolean hasUnderScore = false;
        int[] freqTable =
            new int[26]; // To store frequency of ladybugs (A-Z). A' frequency is
                         // stored at int[0] and Z's at int[25]
    
        // Step 1: Count frequencies of each ladybug and check for underscores
        for (char c : b.toCharArray()) {
          if (c == '_') {
            hasUnderScore = true; // Flag to track if there's at least one
                                  // underscore
          } else {
            freqTable[c - 'A']++;
          }
        }
    
        // Step 2: Check if there's any ladybug with only 1 occurrence
        if (hasSingleOccurence(freqTable)) {
          return notHappyString;
        }
    
        // Step 3: If there are no underscores, check if the arrangement is already
        // happy
        if (!hasUnderScore && !isAlreadyHappy(b)) {
          return notHappyString;
        }
    
        // Otherwise happy
        return happyString;
      }
    
      // Method to check if any ladybug has only a single occurrence
      private static boolean hasSingleOccurence(int[] freq) {
        for (int i = 0; i < 26; i++) {
          if (freq[i] == 1) {
            return true;
          }
        }
        return false;
      }
    
      // Method to check if the string is already in a "happy" state (no
      // underscores)
      private static boolean isAlreadyHappy(String b) {
        for (int i = 0; i < b.length(); i++) {
          char current = b.charAt(i);
          if ((i > 0 && b.charAt(i - 1) == current)
              || (i < b.length() - 1 && b.charAt(i + 1) == current)) {
            continue;
          }
          return false; // found an unhappy ladybug
        }
            return true;
      }
    }
    
        // WITH HASHMAP
    
        class Result {
          /*
           * Complete the 'happyLadybugs' function below.
           *
           * The function is expected to return a STRING.
           * The function accepts STRING b as parameter.
           */
    
          public static String happyLadybugs(String b) {
            String happyString = "YES";
            String notHappyString = "NO";
            boolean hasUnderScore = false;
            Map<Character, Integer> freqTable = new HashMap<>();
    
            // Step 1: Count frequencies of each ladybug and check for underscores
            for (char c : b.toCharArray()) {
              if (c == '_') {
                hasUnderScore =
                    true; // Flag to track if there's at least one underscore
              } else {
                freqTable.put(c, freqTable.getOrDefault(c, 0) + 1);
              }
            }
    
            // Step 2: Check if there's any ladybug with only 1 occurrence
            if (hasSingleOccurence(freqTable)) {
              return notHappyString;
            }
    
            // Step 3: If there are no underscores, check if the arrangement is
            // already happy
            if (!hasUnderScore && !isAlreadyHappy(b)) {
              return notHappyString;
            }
    
            // Otherwise happy
            return happyString;
          }
    
          // Method to check if any ladybug has only a single occurrence
          private static boolean hasSingleOccurence(Map<Character, Integer> freq) {
            for (char key : freq.keySet()) {
              // If a character is not '_' and occurs only once, it's impossible to
              // make them happy
              if (freq.get(key) == 1) {
                return true;
              }
            }
            return false;
          }
    
          // Method to check if the string is already in a "happy" state (no
          // underscores)
          private static boolean isAlreadyHappy(String b) {
            for (int i = 0; i < b.length(); i++) {
              char current = b.charAt(i);
              if ((i > 0 && b.charAt(i - 1) == current)
                  || (i < b.length() - 1 && b.charAt(i + 1) == current)) {
                continue;
              }
              return false; // found an unhappy ladybug
            }
            return true;
          }
        }
    
  • + 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

    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"; 
            
            }