7012.Counting Reorders

Time Limit: 1s Memory Limit: 512MB

Calculate the number of ways you can reorder the characters of a string so that no two adjacent characters are the same.

For example, the answer for aabc is $6$ , because the possible orders are abac , abca , acab , acba , baca , and caba .

Input Format(From the terminal/stdin)

The only input line has a string that consists of $n$ characters between a – z .

  • $1 \le n \le 5000$

Output Format(To the terminal/stdout)

Print an integer: the answer modulo $10^9+7$ .

Sample Input

Copy
aabc
    \n

Sample Output

Copy
6
 \n
Source: CSES, Counting Problems, 2421

Submit

请先 登录

© 2025 FAQs