310266: CF1807C. Find and Replace

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

C. Find and Replacetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

You are given a string $s$ consisting of lowercase Latin characters. In an operation, you can take a character and replace all occurrences of this character with $\texttt{0}$ or replace all occurrences of this character with $\texttt{1}$.

Is it possible to perform some number of moves so that the resulting string is an alternating binary string$^{\dagger}$?

For example, consider the string $\texttt{abacaba}$. You can perform the following moves:

  • Replace $\texttt{a}$ with $\texttt{0}$. Now the string is $\color{red}{\texttt{0}}\texttt{b}\color{red}{\texttt{0}}\texttt{c}\color{red}{\texttt{0}}\texttt{b}\color{red}{\texttt{0}}$.
  • Replace $\texttt{b}$ with $\texttt{1}$. Now the string is ${\texttt{0}}\color{red}{\texttt{1}}{\texttt{0}}\texttt{c}{\texttt{0}}\color{red}{\texttt{1}}{\texttt{0}}$.
  • Replace $\texttt{c}$ with $\texttt{1}$. Now the string is ${\texttt{0}}{\texttt{1}}{\texttt{0}}\color{red}{\texttt{1}}{\texttt{0}}{\texttt{1}}{\texttt{0}}$. This is an alternating binary string.

$^{\dagger}$An alternating binary string is a string of $\texttt{0}$s and $\texttt{1}$s such that no two adjacent bits are equal. For example, $\texttt{01010101}$, $\texttt{101}$, $\texttt{1}$ are alternating binary strings, but $\texttt{0110}$, $\texttt{0a0a0}$, $\texttt{10100}$ are not.

Input

The input consists of multiple test cases. The first line contains an integer $t$ ($1 \leq t \leq 100$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains an integer $n$ ($1 \leq n \leq 2000$) — the length of the string $s$.

The second line of each test case contains a string consisting of $n$ lowercase Latin characters — the string $s$.

Output

For each test case, output "YES" (without quotes) if you can make the string into an alternating binary string, and "NO" (without quotes) otherwise.

You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).

ExampleInput
8
7
abacaba
2
aa
1
y
4
bkpt
6
ninfia
6
banana
10
codeforces
8
testcase
Output
YES
NO
YES
YES
NO
YES
NO
NO
Note

The first test case is explained in the statement.

In the second test case, the only possible binary strings you can make are $\texttt{00}$ and $\texttt{11}$, neither of which are alternating.

In the third test case, you can make $\texttt{1}$, which is an alternating binary string.

Input

题意翻译

给定一串长度为 $n$ 的字符串 $s$,你可以将每种字母转为 $0$ 或 $1$,将所有字母转换完后,问能否得到一串由 $0$ 和 $1$ 交替而成的字符串(即相邻两个字符不同)。 by @[Larryyu](https://www.luogu.com.cn/user/475329)

Output

题目大意:
这个题目是关于字符串的操作。给定一个由小写拉丁字母组成的字符串 s。每次操作可以将一个字符的所有出现替换为 0 或者将一个字符的所有出现替换为 1。需要判断是否可以通过一系列操作使得最终得到的字符串是一个交替二进制字符串。

输入输出数据格式:
输入:
- 第一行包含一个整数 t (1 ≤ t ≤ 100) 表示测试用例的数量。
- 每个测试用例包含两行:
- 第一行是一个整数 n (1 ≤ n ≤ 2000) 表示字符串 s 的长度。
- 第二行是一个由 n 个小写拉丁字母组成的字符串 s。

输出:
- 对于每个测试用例,如果能通过操作得到一个交替二进制字符串,则输出 "YES",否则输出 "NO"。

示例:
输入:
```
8
7
abacaba
2
aa
1
y
4
bkpt
6
ninfia
6
banana
10
codeforces
8
testcase
```
输出:
```
YES
NO
YES
YES
NO
YES
NO
NO
```

注意:
- 交替二进制字符串是指只包含 0 和 1,并且没有两个相邻位相同的字符串。例如,01010101、101、1 是交替二进制字符串,而 0110、0a0a0、10100 不是。题目大意: 这个题目是关于字符串的操作。给定一个由小写拉丁字母组成的字符串 s。每次操作可以将一个字符的所有出现替换为 0 或者将一个字符的所有出现替换为 1。需要判断是否可以通过一系列操作使得最终得到的字符串是一个交替二进制字符串。 输入输出数据格式: 输入: - 第一行包含一个整数 t (1 ≤ t ≤ 100) 表示测试用例的数量。 - 每个测试用例包含两行: - 第一行是一个整数 n (1 ≤ n ≤ 2000) 表示字符串 s 的长度。 - 第二行是一个由 n 个小写拉丁字母组成的字符串 s。 输出: - 对于每个测试用例,如果能通过操作得到一个交替二进制字符串,则输出 "YES",否则输出 "NO"。 示例: 输入: ``` 8 7 abacaba 2 aa 1 y 4 bkpt 6 ninfia 6 banana 10 codeforces 8 testcase ``` 输出: ``` YES NO YES YES NO YES NO NO ``` 注意: - 交替二进制字符串是指只包含 0 和 1,并且没有两个相邻位相同的字符串。例如,01010101、101、1 是交替二进制字符串,而 0110、0a0a0、10100 不是。

加入题单

算法标签: