We are given n up to 200, string s of lowercase and uppercase letters. A set of positions A (subset of indices, presumably 1-indexed as per examples) is pretty if:
- letters on positions from A are all distinct and lowercase (so each selected position must have a lowercase letter, and all those letters are distinct; i.e., no two selected positions have the same lowercase character).
- There are no uppercase letters in the string which are between positions from A. i.e., for any two positions a1 and a2 in A (with a1 < a2), there is no uppercase letter in the open interval (a1, a2). More formally, for any j such that s[j] is uppercase, it cannot be between any two selected positions. Equivalent: The set of selected positions must be such that the whole segment from the minimum to maximum selected index contains no uppercase letters? Wait careful: Condition says: there are no uppercase letters situated between positions from A. That is: there is no such j that s[j] is uppercase, and a1 < j < a2 for some a1 and a2 from A. If A has more than two positions, this condition implies that between any two selected positions (consecutive in sorted order) there must be no uppercase letters. But is it sufficient to require that there is no uppercase letter between any consecutive selected positions? Because if there is an uppercase letter between a1 and a3, then there exist a1 and a3 such that the uppercase letter lies between them, so it's forbidden. So indeed, the condition is equivalent to: the entire interval from min(A) to max(A) must contain no uppercase letters? Actually not exactly. If A = {2,5,7} and there's an uppercase at position 3, then there is a1=2, a2=5 with 3 in between, so violation. So any uppercase that lies between the smallest and largest selected index (but not necessarily covering all indices) would be between some pair of selected positions? If the uppercase is at position 4 and A = {2,5}, it's between them. If uppercase at position 1 (before the first selected) or at position n (after the last selected), then it's not between any two selected positions because there is no a1 < j < a2. However, the condition does not forbid uppercase letters outside the convex hull of A. So the allowed region of uppercase is only strictly before the first selected position or strictly after the last selected position. Also, uppercase letters could be between selected positions if there is no two selected positions that have it between them? But if there is an uppercase letter at index j, and A contains at least one index less than j and at least one index greater than j, then there exist a1 < j < a2. So the condition is violated if there exists any uppercase letter that has selected positions on both sides (some smaller and some larger). If uppercase appears but all selected positions are on one side (i.e., either all selected positions are ≤ j, or all selected positions are ≥ j), then it's okay. But note that if there are multiple selected positions, they themselves could be on both sides if there is an uppercase letter inside the range. So essentially, the condition is: The set of selected positions must lie in a contiguous segment of the string that contains no uppercase letters. Because if the segment from the smallest selected to the largest selected contains an uppercase letter, then that uppercase letter is between them, violating the condition. Conversely, if there is no uppercase letter in the closed interval [min(A), max(A)]? Wait, condition only forbids uppercase letters between positions from A. If uppercase letter is exactly at a selected position? But selected positions must have lowercase letters, so uppercase cannot be at a selected position. So uppercase letters inside the interval are not allowed if they are between any two selected positions. If uppercase letter is exactly at the leftmost selected position? That cannot happen because it's lowercase. So the interval (min(A), max(A)) cannot contain any uppercase. However, what about uppercase at the exact endpoints? They are lowercase by selection, so not uppercase. So it's effectively: The substring from min(A) to max(A) must contain no uppercase letters. However, is it necessary that the whole substring is uppercase-free? Suppose we have A = {2,4} and there's uppercase at position 3, then violation because 2<3<4. So yes. Suppose A = {2,5} and uppercase at position 5? can't because 5 is selected so lowercase. So the only way to have no uppercase between any two selected positions is that the interval (min(A), max(A)) contains no uppercase letters. But also consider that uppercase letters could be at indices less than min(A) or greater than max(A), that's fine.
Thus, a pretty set A must satisfy:
- All positions in A are lowercase letters.
- All letters at these positions are distinct.
- The substring from the minimum index in A to the maximum index in A (the entire segment covering A) contains no uppercase letters. (Equivalently, the selected positions lie within a contiguous block that has no uppercase letters, and they are themselves lowercase letters; the block may also contain other lowercase letters but no uppercase.)
Now, we need maximum size of such set.
n is small (200), so we could brute force all subsets? 2^200 is impossible. But we can think about dynamic programming or greedy.
Observation: The condition that all letters are distinct and lowercase, and they must all be within a contiguous substring that contains no uppercase letters. However, note that the substring can contain other lowercase letters that are not selected, but if we select a subset of positions from a contiguous uppercase-free segment, then the condition about no uppercase between any two selected positions automatically holds because there is no uppercase in the segment. Conversely, if the condition holds, the selected positions must be inside some uppercase-free segment (the segment from min to max is uppercase-free). So we can break the string into "segments" separated by uppercase letters. Actually, consider that uppercase letters act as barriers: if we have an uppercase, we cannot have selected positions on both sides of it because that would put the uppercase between them. So all selected positions must be contained within a segment that does not contain any uppercase. But also note that we could have selected positions from two different uppercase-free segments if we only pick positions from one segment? No, if we pick positions from two different segments that are separated by an uppercase letter, then that uppercase would be between them, violating the condition. So indeed, all selected positions must come from a single contiguous block of the string that contains no uppercase letters. That block may be maximal segments between uppercase letters, but also we could consider a subsegment of such a block, as long as the entire block from min to max is within the block and contains no uppercase.
Thus, the problem reduces to: For each contiguous substring that contains no uppercase letters (i.e., each maximal segment consisting only of lowercase letters), we can select a subset of positions from it, with distinct lowercase letters, and we want to maximize the size of such a subset. Since selecting from two different maximal segments is not allowed (due to the uppercase separating them), the overall maximum is just the maximum over all such segments of the maximum number of distinct lowercase letters we can pick from that segment (since letters must be distinct and each position is a single letter). But wait: Within a segment, there may be repeated letters. The condition "letters on positions from A are all distinct" means we cannot pick two positions with the same letter. So the maximum number of positions we can pick from a segment is limited by the number of distinct lowercase letters present in that segment. But is it always possible to pick all distinct letters present in the segment? We need to pick a set of positions, each with a distinct letter, such that the letters are all distinct. To maximize the count, we would like to pick as many distinct letters as possible. Since the segment contains only lowercase letters, we can simply choose one position for each distinct letter that appears in the segment. This yields a set where all letters are distinct. Does it satisfy the condition about uppercase? Since the entire segment has no uppercase, the condition holds. So the maximum size for that segment is exactly the number of distinct lowercase letters in that segment.
But is there any other constraint? Could there be an issue if the positions we choose are not within the segment? No, we pick within the segment. Also, there is no requirement that the set is contiguous; it can be any positions within the segment. So yes, the maximum is the number of distinct letters in that segment.
However, need to verify with examples:
Example 1: n=11, s="aaaaBaabAbA". Let's index from 1: positions:1:a,2:a,3:a,4:a,5:B,6:a,7:a,8:b,9:A,10:b,11:A. Actually the string: "aaaaBaabAbA" -> Let's write: 1 a,2 a,3 a,4 a,5 B,6 a,7 a,8 b,9 A,10 b,11 A. The uppercase letters: B at 5, A at 9, A at 11. So the segments without uppercase: segment1 from 1 to 4 (only a's), segment2 from 6 to 8 (a,a,b), segment3 position10? Actually after 9 is A, but 10 is b, then 11 A, so there is segment from 10 to 10? because 10 is b, but between 9 and 11: positions 9 is A uppercase, 10 b lowercase, 11 A uppercase. So there is a segment of length 1 at 10. But segment2 is from 6 to 8 (positions 6,7,8) because positions 5 uppercase, 6-8 lowercase, 9 uppercase. So segment2: letters: a, a, b -> distinct letters: a and b => 2. Segment1: all a -> distinct: a => 1. Segment3: b -> distinct: 1. So max is 2, matches output.
Example 2: "zACaAbbaazzC". n=12. String: Let's index: 1:z,2:A,3:C,4:a,5:A,6:b,7:b,8:a,9:a,10:z,11:z,12:C. Actually need to confirm: "zACaAbbaazzC". Let's parse: z A C a A b b a a z z C. So uppercase: A(2), C(3), A(5), C(12). So segments: between uppercase? Let's list lowercase positions: 1(z),4(a),6(b),7(b),8(a),9(a),10(z),11(z). The maximal contiguous segments without uppercase: Starting at 1: position1 is z, but position2 is A, so segment1: [1,1] (z). Next after position2: position3 is C (uppercase), so skip. After position3: position4 a, position5 is A (uppercase), so segment2: [4,4] (a). After position5: position6 b,7 b,8 a,9 a,10 z,11 z; position12 is C uppercase; so segment3: [6,11] (b,b,a,a,z,z) -> letters: b, a, z => distinct 3. Also check: position? Actually also there might be segment after position12 none. So max distinct = max(1,1,3)=3, matches output.
Example3: "ABC" all uppercase => no lowercase segments, so max =0.
So indeed, the answer seems to be: Split the string by uppercase letters, for each segment consisting of only lowercase letters, count distinct letters in that segment, take maximum over segments.
But wait, is it always that simple? Let's read the problem statement carefully: "there are no uppercase letters in the string which are situated between positions from A (i.e. there is no such j that s[j] is an uppercase letter, and a1 < j < a2 for some a1 and a2 from A)." This condition does NOT require that the entire interval from min(A) to max(A) is uppercase-free; it only forbids uppercase letters that lie between some pair of selected positions. If A = {2, 5, 7}, and there is an uppercase letter at position 4, then indeed there exist a1=2 and a2=5 such that 4 lies between them, so violation. But what if A = {2, 5} and there's uppercase at position 3, violation. So as argued, any uppercase inside (min(A), max(A)) will be between min and max, so violation. However, what about uppercase letters at positions that are between selected positions but maybe not between the min and max? If we have A = {3,5,8}, and uppercase at position 6: between 5 and 8, violation. So indeed, any uppercase within the convex hull of A (the interval from min to max) is forbidden. However, what about uppercase letters that are between some selected positions but not necessarily all? That's captured. So condition is equivalent to: There is no uppercase letter whose index lies strictly between the minimum and maximum index of A.
But also, could there be an uppercase letter that lies between two selected positions that are not the extreme ones? Still it's inside the overall interval, so the condition is same as: interval (min(A), max(A)) contains no uppercase.
Now, could it be that A is not necessarily contained within a single maximal lowercase segment? Suppose we have string: a A b (lowercase, uppercase, lowercase). Consider A = {1,3}. min=1 max=3. The interval (1,3) includes position2 which is uppercase, so this violates the condition. So not allowed.
What about A = {1} only? Then there's no pair, so condition vacuously holds (since need a1 and a2). Actually condition: "there are no uppercase letters in the string which are situated between positions from A (i.e. there is no such j that s[j] is an uppercase letter, and a1 < j < a2 for some a1 and a2 from A)." If A has only one element, there are no pairs a1,a2, so the condition is vacuously true. So singleton sets are allowed as long as it's lowercase. So a single lowercase anywhere is fine, even if there are uppercase around. So A can be a single position from any part, even within uppercase segments? Actually if it's a single position, it's automatically okay. So the answer might be at least 1 if there is any lowercase. But the problem asks for maximum size. So we could always pick a single lowercase letter.
Now, consider scenario: string: a B c (positions:1:a,2:B,3:c). The maximal lowercase segments are {1} and {3}. The rule says answer = max(distinct in seg1=1, seg2=1)=1. But could we pick A = {1,3}? No, because uppercase between them. So max is 1.
Now, consider a more subtle case: Could there be a scenario where we can pick more distinct letters than the number of distinct letters in any single maximal lowercase segment? Suppose we have a segment that contains all distinct letters but also there is an uppercase letter that we could "jump over" by not including some positions? Wait, if we pick positions that are not all within one uppercase-free segment, then there must be at least one uppercase between some selected positions, so it's forbidden. So all selected positions must be within some contiguous region with no uppercase. But that region could be a subsegment of a maximal segment? Actually, if the region has no uppercase, it must be wholly contained within some maximal segment, because maximal segments are exactly the largest contiguous blocks with no uppercase. So any uppercase-free contiguous region is a subset of a maximal segment. Therefore, the set A's min and max indices must lie within some maximal segment, and since the entire interval between them contains no uppercase, that interval is a substring of that maximal segment. So A is a subset of that maximal segment. So the letters we can select are limited to letters that appear in that segment. So the maximum distinct letters we can select cannot exceed the number of distinct letters in that segment. And we can achieve that number by picking one occurrence of each distinct letter in that segment. So indeed, answer is maximum distinct letters in any contiguous substring that contains no uppercase letters? Wait, but any such substring is a subset of a maximal segment, and its distinct letters count is ≤ that of the whole segment. The maximum distinct count for a segment is the count over the whole segment. So we can take the whole segment as the region, but we may not need to include all positions; we just need to select positions corresponding to each distinct letter. That's valid. So answer is max over maximal segments of distinct letters.
But wait: Could there be a scenario where the segment contains uppercase letters but we can still select positions that straddle across uppercase if we ensure that there is no uppercase between any two selected positions? For example, consider string: a A b B c. Positions: 1:a,2:A,3:b,4:B,5:c. If we pick A = {1,5}, then there is uppercase at 2 and 4 between them, violation. If we pick {1,3}, uppercase at 2 between? 1<2<3 -> uppercase at 2, violation. So any pair that spans an uppercase is violation. So we cannot have two selected positions with an uppercase in between, even if there are other uppercase? So the only way to have more than one is that all selected positions are within a region with no uppercase. So it's as argued.
Thus answer is straightforward.
But wait, check the condition "letters on positions from A in the string are all distinct and lowercase". This means each selected position must have a lowercase letter, and all these letters must be distinct. So we cannot pick two positions with same letter even if they are in different segments? Already disallowed. So distinctness is a constraint.
So solution: iterate through string, keep a set of lowercase letters for current segment, reset when encountering uppercase. Track maximum size of set.
Complexity O(n). n ≤ 200, trivial.
But hold on: Are there any tricky cases where a segment contains many letters but picking all distinct letters may conflict with the "no uppercase between" condition if we skip some positions? No, because if we pick positions that are not necessarily covering the whole segment from min to max, there could still be uppercase between them if there is an uppercase somewhere in between. But if the segment is uppercase-free, there is none. So it's fine.
Edge cases: n=0 not allowed. All uppercase => answer 0. All lowercase => answer = number of distinct letters in whole string (max 26). String length up to 200, but distinct letters limited to 26.
Thus answer is computed as described.
But wait, is there any nuance: The condition says "there are no uppercase letters in the string which are situated between positions from A (i.e. there is no such j that s[j] is an uppercase letter, and a1 < j < a2 for some a1 and a2 from A)". This means: For any uppercase letter at position j, it must not lie strictly between any two positions from A. That is, it is allowed to be equal to a selected position? No, selected positions are lowercase, so that's impossible. So for an uppercase letter, there must not exist a1, a2 in A with a1 < j < a2. So it is okay if uppercase letter is to the left of all selected positions, or to the right of all selected positions, or maybe if there is only one selected position, then no pair exists, so condition vacuously true. So that matches our reasoning.
Thus solution is as above.
But maybe there is a twist: The set A can be any subset of positions, not necessarily contiguous, but the condition about uppercase means that the positions must be "uppercase-free" in the sense that the region between any two must not contain uppercase. That is equivalent to: For any two positions in A, the interval between them contains no uppercase. This is stronger than just the overall interval from min to max being uppercase-free? Actually, if the overall interval from min to max contains no uppercase, then any subinterval between any two selected positions also contains no uppercase. So it's equivalent. Conversely, if there is an uppercase letter in the overall interval, then there exists at least one pair (min, max) that has it between them. So condition is equivalent to the interval between the minimum and maximum containing no uppercase. So it's exactly the condition we used.
Thus the solution is correct.
But why n ≤ 200? Maybe they expected DP or something, but it's simple.
Double-check with sample: sample1 gave 2, sample2 gave 3, sample3 gave 0. Good.
Thus we can code.
Implementation: read n, read s. Initialize max_distinct = 0, current_set = empty set (or boolean array for 26). Iterate over characters of s:
- if character is lowercase: add to current_set.
- else (uppercase): update max_distinct = max(max_distinct, size of current_set), clear current_set.
After loop, also update max_distinct with size of current_set (if string ends with lowercase). Print max_distinct.
Need to handle characters: check if 'a' <= c <= 'z'? Actually uppercase vs lowercase: we can use islower() or manually.
Edge: If there are no segments, max remains 0.
Thus solution done.
But let's confirm: Could there be a case where a segment contains all distinct letters, but we cannot pick all because some letters might be separated by uppercase? In a segment no uppercase, fine.
So final answer.
But wait, maybe there is a nuance: The condition says "letters on positions from A in the string are all distinct and lowercase". It says "letters ... are all distinct and lowercase". That means each letter is lowercase, and all are distinct. So we cannot pick two same letters even if they are in different segments anyway. So yes.
Thus solution complete.
But let's double-check with a custom test: s = "aab". All lowercase, distinct letters are a and b => 2. Our method: segment is whole string, set = {a,b} size 2, max=2. Could we pick 3? Only three positions but letters would be a,a,b not distinct, so cannot. So 2 is correct.
Another test: s = "abcABCabc". Here uppercase at positions 4-6: A,B,C. Segments: first segment positions1-3: a,b,c -> distinct 3. Second segment positions7-9: a,b,c -> distinct 3. So answer 3.
What about string with lowercase letters but some uppercase inside but we can still pick positions that are not spanning uppercase by picking a subset that is entirely within the segment? That's what we are doing.
Thus answer.
So code is trivial.
But wait: Did we misinterpret "between positions from A"? Could it be that the condition only forbids uppercase letters that are between any two consecutive positions in A (when sorted)? The phrasing "a1 < j < a2 for some a1 and a2 from A" could be any pair, not necessarily consecutive. So it's as we used. So fine.
Thus answer.
I'll produce solution in C++ or Python.
But the problem is from Codeforces? I recall a problem "Polycarp loves lowercase letters" or something like that. The solution is indeed to count distinct letters in each segment.
Alright.
But we should double-check the example: In first example they said desired positions might be 6 and 8 or 7 and 8. Our segment2 from 6 to 8 contains a,a,b, so distinct a and b, so we can pick 6 and 8 (a and b) or 7 and 8. So indeed, we can pick any one a and b. So distinct count is 2.
Second example: they said positions 7,8,11. But note: position 11 is z? Actually in segment3 we had positions 6-11: letters: 6 b,7 b,8 a,9 a,10 z,11 z. Distinct: b,a,z =3. They pick 7(b),8(a),11(z). That's valid. So 3.
Thus matches.
So answer.
Let's also consider possibility: The set A might have more than one position but not necessarily covering the entire segment? For instance, if the segment is "abCdef" (but that has uppercase C, so not a segment). So no.
So final solution: iterate, count distinct per segment.
Given the low n, can also brute force all intervals, but simple.
Thus code.
But maybe the problem intended that the set A must be such that there is no uppercase letter between any two positions, but you could skip over uppercase if you don't have any uppercase between the specific pair? Suppose we pick A = {2,4,6} where string: a B c D e (positions:1 a,2 B uppercase? Wait we need selected positions lowercase. So selected must be lowercase. Let's try: positions:1 a,2 b (lowercase),3 C (uppercase),4 d (lowercase),5 E (uppercase),6 f (lowercase). So A = {2,4,6}. Check pairs: (2,4): between them is position3 which is uppercase C -> violation. So no.
What if we pick A = {2,6} and there is uppercase at 4? (2,6) includes 4 between, violation.
So indeed.
Thus correct.
Now, consider scenario where we have two uppercase letters but we choose positions that are between them but not crossing any uppercase? That is within a segment.
Thus answer is simple.
So final code.
We'll implement in Python.
Pseudo:
def solve():
import sys
input = sys.stdin.readline
n = int(input().strip())
s = input().strip()
max_distinct = 0
current_set = set()
for ch in s:
if 'a' <= ch <= 'z': # lowercase
current_set.add(ch)
else:
max_distinct = max(max_distinct, len(current_set))
current_set.clear()
max_distinct = max(max_distinct, len(current_set))
print(max_distinct)
Test with given examples.
Edge: n maybe not needed but use.
Thus done.
But hold on: The problem statement says "letters on positions from A in the string are all distinct and lowercase". This means that if we pick a set of positions, the letters at those positions must be distinct. However, does it also require that those letters are lowercase? Yes, each position's letter must be lowercase. So we cannot pick uppercase positions.
Now, consider the possibility that within a segment, there may be repeated letters; we only pick distinct ones. So the maximum number we can pick is the number of distinct letters in that segment.
But could we pick a set that does not include all distinct letters but still yields the same size? No, maximum is distinct count.
Thus answer.
But what about this: Could we pick positions from multiple segments if we avoid having an uppercase between any pair? Suppose we have string: a b C d e. We could pick A = {1,2} (both in first segment). That's fine. But if we pick {1,4}? uppercase at 3 between? yes violation. So can't.
Thus done.
Given the simplicity, the solution is correct.
But we should verify with a more complex scenario: The condition says "there are no uppercase letters ... situated between positions from A". It does not forbid uppercase letters that are not between any two positions. So if we have only one position, it's fine. So maximum size is at least 1 if any lowercase exists.
But what about scenario where we have a segment with many distinct letters, but also there is an uppercase inside the segment? That would split it into two segments because uppercase breaks the contiguity. So there is no segment containing both sides of uppercase.
Thus answer.
Thus I'll finalize.
But just to be thorough, let's analyze if there is any possibility to have a set A that does not lie within a single maximal segment but still satisfies the condition because the uppercase letters that are between some selected positions might be "shielded" by not having a pair that straddles them? For example, consider positions 1,3,5 and uppercase at position 2 and 4. If A = {1,3,5}, then check pair (1,3): uppercase at 2? yes, violation. So no. If A = {1,5} and uppercase at 3? yes violation. So any uppercase that lies between min and max will be between the min and max. So condition fails. So indeed must have no uppercase between min and max. So A must be contained within a contiguous region without uppercase.
Thus correct.
Now, just to double-check: Could the condition be interpreted as "for every uppercase letter, it is not the case that there exist two positions from A such that the uppercase is between them"? That is the same.
Thus final answer.
Now, write code. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
n = int(data[0])
s = data[1]
max_distinct = 0
cur_set = set()
for ch in s:
if 'a' <= ch <= 'z': # lowercase
cur_set.add(ch)
else: # uppercase
max_distinct = max(max_distinct, len(cur_set))
cur_set.clear()
max_distinct = max(max_distinct, len(cur_set))
print(max_distinct)
if name == "main":
solve()