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.
Happy Ladybugs
Happy Ladybugs
+ 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 comments Happy Ladybugs problem Solution In HackerRank Here is the solution:- Click Here
+ 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 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'
+ 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"; } }
Load more conversations
Sort 422 Discussions, By:
Please Login in order to post a comment