codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1605C.md (2105B)


      1 Source: https://codeforces.com/problemset/problem/1605/C
      2 
      3 C. Dominant Character
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 Ashish has a string $s$ of length $n$ containing only characters 'a', 'b' and 'c'.
     22 
     23 He wants to find the length of the smallest substring, which satisfies the following conditions:
     24 
     25 * Length of the substring is at least $2$
     26 * 'a' occurs strictly more times in this substring than 'b'
     27 * 'a' occurs strictly more times in this substring than 'c'
     28 
     29 Ashish is busy planning his next Codeforces round. Help him solve the problem.
     30 
     31 A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
     32 
     33 Input
     34 
     35 The first line contains a single integer $t$ $(1 \le t \le 10^{5})$  — the number of test cases. The description of test cases follows.
     36 
     37 The first line of each test case contains a single integer $n$ $(2 \le n \le 10^{6})$  — the length of the string $s$.
     38 
     39 The second line of each test case contains a string $s$ consisting only of characters 'a', 'b' and 'c'.
     40 
     41 It is guaranteed that the sum of $n$ over all test cases does not exceed $10^{6}$.
     42 
     43 Output
     44 
     45 For each test case, output the length of the smallest substring which satisfies the given conditions or print $-1$ if there is no such substring.
     46 
     47 Example
     48 
     49 Input
     50 
     51 ```
     52 3
     53 2
     54 aa
     55 5
     56 cbabb
     57 8
     58 cacabccc
     59 ```
     60 
     61 Output
     62 
     63 ```
     64 2
     65 -1
     66 3
     67 ```
     68 
     69 Note
     70 
     71 Consider the first test case. In the substring "aa", 'a' occurs twice, while 'b' and 'c' occur zero times. Since 'a' occurs strictly more times than 'b' and 'c', the substring "aa" satisfies the condition and the answer is $2$. The substring "a" also satisfies this condition, however its length is not at least $2$.
     72 
     73 In the second test case, it can be shown that in none of the substrings of "cbabb" does 'a' occur strictly more times than 'b' and 'c' each.
     74 
     75 In the third test case, "cacabccc", the length of the smallest substring that satisfies the conditions is $3$.