• + 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();
    }
    

    }