We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Implementation
  4. Happy Ladybugs
  5. Discussions

Happy Ladybugs

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 422 Discussions, By:

recency

Please Login in order to post a comment

  • AndreyGermanov
    2 weeks ago+ 0 comments

    Julia

    function happyLadybugs(b)
        if length(b) == 1 && b[1] !== '_'
            return "NO"
        end
        symbs = Dict()
        for ch in b
            if !haskey(symbs,ch)
                symbs[ch] = 1
            else
                symbs[ch] +=1
            end
        end
        unhappy_num = 0
        for index in eachindex(b)
            if b[index] !== '_'
                if !isHappy(b,index)
                    unhappy_num +=1
                end
            end
        end
        if unhappy_num>0 && !haskey(symbs,'_')
            return "NO"
        end
        for (index,value) in symbs
            if index!=='_' && value == 1
                return "NO"
            end
        end
        return "YES"
    end
    
    function isHappy(b,index)
        if index == 1 && b[index+1] === b[index]
            return true
        elseif index == length(b) && b[index-1] === b[index]
            return true
        elseif index !== 1 && index !== length(b)
            return b[index-1] === b[index] || b[index+1] === b[index]        
        end
        return false
    end
    
    0|
    Permalink
  • dream101221
    1 month ago+ 0 comments

    Happy Ladybugs problem Solution In HackerRank Here is the solution:- Click Here

    0|
    Permalink
  • SmokyDee
    1 month ago+ 0 comments

    Golang:

    func happyLadybugs(b string) string {
    	// Write your code here
    	var Array [26]int
    	var adj []int
    	var space int
    	count := 1
    	for index, char := range b {
    		if char == '_' {
    			space++
    			continue
    		}
    		Array[int(char)-65]++
    		if index > 0 {
    			if rune(b[index-1]) == char {
    				count++
    			} else {
    				adj = append(adj, count)
    				count = 1
    			}
    		}
    	}
    	adj = append(adj, count)
    	if space == 0 {
    		for _, a := range adj {
    			if a == 1 {
    				return "NO"
    			}
    		}
    		return "YES"
    	}
    	for _, count := range Array {
    		if count == 1 {
    			return "NO"
    		}
    	}
    	return "YES"
    }
    
    -1|
    Permalink
  • BELLDINOUSAUR
    2 months ago+ 1 comment

    python3:

    def happyLadybugs(b):
        # Write your code here
        if b.count('_')==0:
            i=1
            while i<=len(b)-2 and (b[i]==b[i-1] or b[i]==b[i+1]):
                i+=1
            if i==len(b)-1 and b[i]==b[i-1]:
                return 'YES'
            else: 
                return 'NO'           
        t=Counter(b)
        check=True
        for i in t.keys():
            if i!='_' and t[i]==1:
                check=False
                break
        return 'YES' if check else 'NO'   
    
    -2|
    Permalink
  • grbz_kerem
    2 months ago+ 1 comment

    JavaScript Solution

    function happyLadybugs(b) {
        const ladybugs = b.split("");
        const ladybugsSet = [...new Set(ladybugs)];
    
        if (!ladybugsSet.includes("_")) {
          for (let i = 0; i < ladybugs.length; i++) {
            if (ladybugs[i - 1] !== ladybugs[i] && ladybugs[i] !== ladybugs[i + 1]) return "NO";
          }
          return "YES";
        } else {
          for (const ladybug of ladybugsSet) {
            if (ladybug !== "_") {
              if (ladybugs.filter((color) => color === ladybug).length === 1) return "NO";
            }
          }
          return "YES";
        }
    }
    
    1|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy