In this HackerRank Happy Ladybugs problem, you have Given the values of n and b for g games of Happy Ladybugs, determine if it’s possible to make all the ladybugs happy. For each game, return YES if all the ladybugs can be made happy through some number of moves. Otherwise, return NO.
Problem solution in Python programming.
games = int(input()) for g in range(games): n = int(input()) b = input() bugs = {} underscore = False for bi in b: if bi == '_': underscore = True else: if bi in bugs: bugs[bi] += 1 else: bugs[bi] = 1 if underscore: for bi in bugs: if bugs[bi] == 1: print('NO') break else: print('YES') else: if n == 1: print('NO') else: if b[0] != b[1]: print('NO') else: for i in range(1, n - 1): if b[i - 1] != b[i] and b[i + 1] != b[i]: print('NO') break else: if b[-1] != b[-2]: print('NO') else: print('YES')
Problem solution in Java Programming.
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { private static boolean isHappyAlready(char c[]){ int l=0,r=0; for(int i=0;i<c.length;i++){ l=i-1; r=i+1; if(l>=0){ if(c[l]==c[i]) continue; } if(r<c.length){ if(c[r]==c[i]) continue; } return false; } return true; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int Q = in.nextInt(); String result="NO"; for(int a0 = 0; a0 < Q; a0++){ int n = in.nextInt(); char[] b = in.next().toCharArray(); char[] cs=new char[27]; char c=0; for(int i=0;i<b.length;i++){ c=b[i]; if(c != '_')cs[c-'A']++; else cs[26]++; } result="YES"; for(int i=0;i<26;i++){ if(cs[i]==1) {result="NO";break;} } if(result=="YES"){ if(cs[26]==0 && !isHappyAlready(b)){ result="NO"; } } System.out.println(result); } //System.out.println(isHappyAlready("RRR".toCharArray())); } }
Problem solution in C++ programming.
#include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <queue> #include <stack> #include <string> #include <bitset> #include <cstdio> #include <limits> #include <vector> #include <climits> #include <cstring> #include <cstdlib> #include <fstream> #include <numeric> #include <sstream> #include <iostream> #include <algorithm> #include <unordered_map> using namespace std; int cnt[1 << 8]; int main(){ int Q; cin >> Q; for(int a0 = 0; a0 < Q; a0++){ int n; cin >> n; string b; cin >> b; memset(cnt, 0, sizeof(cnt)); for(int i = 0; i < n; i++) cnt[b[i]]++; bool OK = true; for(int i = 'A'; i <= 'Z'; i++) if(cnt[i] == 1) OK = false; if(OK == false) { puts("NO"); continue; } if(OK == true) { if(cnt['_'] != 0) { puts("YES"); continue; } for(int i = 0; i < n; i++) { bool tmp = false; if(i - 1 >= 0 && b[i] == b[i - 1]) tmp = true; if(i + 1 < n && b[i] == b[i + 1]) tmp = true; if(tmp == false) OK = false; } puts(OK ? "YES" : "NO"); } } return 0; }
Problem solution in C programming.
#include <math.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <assert.h> #include <limits.h> #include <stdbool.h> #define arr_size 104 int main(){ int * arr = (int *)malloc(arr_size); int size; int length; char c; scanf("%d", &size); bool happy; bool openspace; bool continues; char prev; for(int i = 0; i < size; i++){ scanf("%d",&length); getchar(); memset(arr, 0, arr_size); continues = true; openspace = false; for(int j = 0; j < length; j++){ c = getchar(); if(j == 0){ prev = c; } //printf("this char is:%cn",c); if(c >= 'A' && c <= 'Z'){ if(prev != c && arr[c-'A'] != 0){ continues = false; } arr[c-'A']++; } else{ openspace = true; } prev = c; } happy = true; for(int j = 0; j < 26; j++){ //printf("%dt",arr[j]); if(arr[j] != 0 && arr[j] < 2){ happy = false; break; } } //printf("n"); if(happy && (continues || openspace))printf("YESn"); else printf("NOn"); } return 0; }
Problem solution in JavaScript programming.
process.stdin.resume(); process.stdin.setEncoding('ascii'); var input_stdin = ""; var input_stdin_array = ""; var input_currentline = 0; process.stdin.on('data', function (data) { input_stdin += data; }); process.stdin.on('end', function () { input_stdin_array = input_stdin.split("n"); main(); }); function readLine() { return input_stdin_array[input_currentline++]; } /////////////// ignore above this line //////////////////// function getFrequency(string) { var freq = {}; for (var i = 0; i < string.length; i++) { var character = string.charAt(i); if (freq[character]) { freq[character]++; } else { freq[character] = 1; } } return freq; }; function main() { var Q = parseInt(readLine()); for(var a0 = 0; a0 < Q; a0++){ var n = parseInt(readLine()); var b = readLine(); var re = /_/; if (b.search(re) != -1) { var f = getFrequency(b); var sum = 0; for (key in f) { if (key != "_" && f[key] === 1) { sum++; } } if (sum > 0) { console.log("NO"); } else { console.log("YES"); } } else { var re2 = /[A-Z]/; if (b.search(re2) != -1) { var arr = b.split(''); var sum2 = 0; for (var i = 0; i < arr.length; i++) { if (arr[i] !== arr[i + 1] && arr[i] !== arr[i - 1]) { sum2++; } } if (sum2 > 0) { console.log("NO"); } else { console.log("YES"); } } else { console.log("NO"); } } } }