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.
/*
* 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();
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Happy Ladybugs
You are viewing a single comment's thread. Return to all comments →
import java.io.; import java.util.; import java.util.stream.*;
class Result {
}
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")));
}