Sort by

recency

|

527 Discussions

|

  • + 0 comments

    import java.io.; import java.util.; import java.util.stream.*;

    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) {
        Map<Character, Integer> freq = new HashMap<>();
        boolean hasEmpty = false;
    
        for (char c : b.toCharArray()) {
            if (c == '_') {
                hasEmpty = true;
            } else {
                freq.put(c, freq.getOrDefault(c, 0) + 1);
            }
        }
    
        if (!hasEmpty) {
            for (int i = 0; i < b.length(); i++) {
                if ((i > 0 && b.charAt(i) == b.charAt(i - 1)) ||
                    (i < b.length() - 1 && b.charAt(i) == b.charAt(i + 1))) {
                    continue;
                }
                return "NO";
            }
            return "YES";
        }
    
        for (int count : freq.values()) {
            if (count == 1) {
                return "NO";
            }
        }
    
        return "YES";
    }
    

    }

    public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int g = Integer.parseInt(bufferedReader.readLine().trim());
    
        IntStream.range(0, g).forEach(gItr -> {
            try {
                int n = Integer.parseInt(bufferedReader.readLine().trim());
                String b = bufferedReader.readLine();
                String result = Result.happyLadybugs(b);
                bufferedWriter.write(result);
                bufferedWriter.newLine();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        });
    
        bufferedReader.close();
        bufferedWriter.close();
    }
    

    }

  • + 0 comments
    def happyLadybugs(b):
        # Write your code here
        temp = {}
        under_score_present = False
        already_happy = True
        n = len(b)
        for i in range(n):
            if b[i] == "_":
                under_score_present = True
                continue
            if already_happy:
                happy_left = True if i > 0 and b[i] == b[i-1] else False
                happy_right = True if i < (n-1) and b[i] == b[i+1] else False
                if not (happy_left or happy_right):
                    already_happy = False
            if temp.get(b[i]):
                temp[b[i]] += 1
            else:
                temp[b[i]] = 1
       
        for i in temp:
            if temp[i] == 1:
                return "NO"
                
        return "YES" if under_score_present or already_happy  else "NO"
    
  • + 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";
    }