This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different.
You are given a string s consisting of n lowercase Latin letters.
You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in s).
After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times.
The goal is to make the string sorted, i.e. all characters should be in alphabetical order.
Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps.
Input
The first line of the input contains one integer n (1 ≤ n ≤ 200) — the length of s.
The second line of the input contains the string s consisting of exactly n lowercase Latin letters.
Output
If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line.
Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of n characters, the i-th character should be '0' if the i-th character is colored the first color and '1' otherwise).
Examples
Input
9
abacbecfd
Output
YES
001010101
Input
8
aaabbcbb
Output
YES
01011011
Input
7
abcdedc
Output
NO
Input
5
abcde
Output
YES
00000
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to analyze the problem. This is an easy version with n ≤ 200. The hard version likely has larger constraints, but we only need to solve for small n. However, the problem is about coloring each character with 0 or 1, and then we are allowed to swap adjacent characters if they have different colors. That essentially means we can rearrange the string by moving characters of one color relative to characters of the other color, but we cannot swap two characters of the same color directly. This is similar to having two "tapes" or sequences that can interleave arbitrarily? Actually, think of it as: we have a sequence of colored items. A swap of adjacent items of different colors is allowed, which is like we can bubble-sort across color boundary. That means the relative order of items of the same color cannot change because any swap only involves items of different colors; but swapping two items of same color would require them to cross each other, which might be possible indirectly? Let's examine.
Consider two positions i<j with the same color. If we want to swap them, we would need to move them past each other, but any adjacent swap swaps opposite colors. So can we change the relative order of two items of the same color? Suppose we have ... a ... b ... with same color. To invert them, we need to pass them through each other. They can be moved only by swapping with opposite color items. In general, the relative order of items of the same color is preserved because swapping only adjacent differing colors cannot change the relative order of same-colored items. Reason: In any sequence of swaps, consider the permutation applied. Each swap is transposition of adjacent elements with different colors. This operation is essentially a "colored" adjacent transposition. If we think of the sequence as having two types, the relative order of items of type 0 is invariant under swapping with type 1, because you can view the sequence as a merge of two independent lists. When you interleave two lists, the order within each list is preserved. Indeed, if we have two sequences L0 (order of 0-colored items) and L1 (order of 1-colored items), then the combined sequence is an interleaving of these two sequences. Swapping adjacent 0 and 1 items changes the interleaving but does not change the order within each list. Therefore, after any sequence of allowed swaps, the subsequence of characters with color 0 remains in the same order as originally, similarly for color 1.
Thus, the problem reduces to: can we assign each character a color (0/1) such that there exists an interleaving of the two subsequences (preserving order within each) that yields a sorted string (non-decreasing alphabetical order). Moreover, the allowed swaps let us achieve any interleaving of the two subsequences? Actually, we can reorder arbitrarily the interleaving? Let's think: given initial coloring, the relative order of 0s and 1s can be rearranged by swapping adjacent 0-1. Since we can swap any adjacent with different colors arbitrarily many times, we can achieve any interleaving of the two sequences, provided we have enough swaps. This is analogous to merging two queues: you can always get any interleaving of two sequences by bubble swaps (like sorting network). More formally, any permutation that respects relative order of each color can be achieved by adjacent swaps of opposite colors. This is known: given two lists, you can reorder to any interleaving by moving items. So yes.
Thus, the problem: Given string s, we need to assign colors to positions such that when we take the subsequence of characters with color 0 (in original order) and the subsequence of characters with color 1 (in original order), these two sequences can be merged (interleaved) to form a sorted non-decreasing string.
Equivalently, we need to partition the original string into two subsequences (preserving order) such that merging them yields a sorted string. This is like checking if we can split the string into two "stacks" or sequences that can be output in order to produce sorted output.
Another viewpoint: We need to find a sequence of colors (0/1) such that if we take the characters of color 0 in order, they form a non-decreasing sequence (since they will appear in the merged sorted string in that order, but the merged sorted string is sorted overall, so each subsequence must also be non-decreasing because they appear as a subsequence of a sorted string; indeed, if the final sorted string is non-decreasing, any subsequence drawn from it in order is also non-decreasing). Also, the characters of color 1 in order must be non-decreasing. Additionally, the two sequences must be such that they can be interleaved to produce exactly the sorted multiset of characters. That is, we need to assign each character to one of two sorted subsequences, such that when we merge them, we get the sorted order.
But is it sufficient that each subsequence is non-decreasing? If both subsequences are non-decreasing individually, can we always merge them to get a sorted string? Not necessarily: merging two sorted sequences yields a sorted sequence. However, the condition is that the merged sequence must be the sorted version of the original string (i.e., the letters in non-decreasing order). So we need that the concatenated multiset of the two sequences, when sorted (i.e., merged) equals the global sorted string. But since each subsequence is individually sorted, merging them gives a sorted sequence. So if we take the two sorted subsequences and merge them, we obtain some sorted string. For this to be exactly the sorted version of s, we need that the union of characters in the two subsequences is exactly the multiset of s (which it is, because we assign all characters) and that the merging yields the sorted order. However, if both subsequences are sorted, then any interleaving that respects their orders (like merging) yields a sorted sequence. But note: we are not forced to merge in a specific way; we can choose any interleaving, and we need to achieve exactly the sorted string. So we need to be able to produce the sorted string by some interleaving. But if both sequences are sorted, merging them in the usual way (taking the smallest available) will produce a sorted sequence. But can it produce exactly the sorted string? That would require that the sorted string is the result of merging the two sorted subsequences. But the sorted string is unique (since it's just the characters sorted). So we need that when we merge the two sorted subsequences using the standard merge (taking the smaller front), we get the sorted string. However, we are not limited to the standard merge; we could interleave differently, but any interleaving that respects orders must produce a sequence that is non-decreasing? Actually, no: if both subsequences are sorted, any interleaving (i.e., any sequence that respects the relative order within each subsequence) is not necessarily sorted overall. For example, subsequence0: [2, 4]; subsequence1: [1, 3]. Interleaving: 2,1,4,3 respects orders? Wait, does it respect orders? In subsequence0, order is 2 then 4; in subsequence1, order is 1 then 3. The sequence 2,1,4,3: For subsequence0, 2 appears before 4 (OK). For subsequence1, 1 appears before 3 (OK). But the sequence is not sorted. So merging two sorted sequences does not guarantee sortedness unless the interleaving is done appropriately (i.e., the merge that picks the smallest among the two next elements). So we must be able to arrange them into a sorted overall sequence. That imposes constraints: The two subsequences must be such that there exists an interleaving that is sorted. Equivalently, the two sequences must be "compatible" to be merged into a sorted whole. This is the same as saying that the entire set of items, when colored, must permit a linear extension that respects the order within each color and is sorted alphabetically.
We can think of it as a partial order: For each color class, we have a chain (the order of indices). We need to find a total order (the final sorted sequence) that respects these two chains and is non-decreasing. Also, the final order must be a permutation of the characters that is sorted.
So we have a partial order: For any two positions i < j with same color, we must have that the final position of i comes before final position of j. For positions with different colors, there is no constraint from the coloring (since they can be arranged arbitrarily relative to each other). However, the final sequence must be sorted: i.e., if the character at position i is larger than the character at position j, then in the final sorted order, the smaller character must come before the larger. But the sorting order imposes constraints on relative ordering: if a character x is less than y, then all occurrences of x must come before all occurrences of y in the sorted order? Actually, sorted non-decreasing means that for any two positions in the final order, the character values are non-decreasing. That implies that if there is an occurrence of 'a' and an occurrence of 'b' with 'a' < 'b', then all 'a's must come before all 'b's? Not necessarily; if there are multiple of same letter, order among them can be arbitrary; but if 'a' < 'b', then any 'a' must appear before any 'b' in a strictly increasing sorted order. Yes, because sorted means the sequence is non-decreasing. So if you have an 'a' and later a 'b', the 'a' must come before the 'b' if you want the sequence to be non-decreasing. However, if there are multiple 'a's and 'b's, you could interleave them? Actually, non-decreasing means you can have a, a, b, b, but you cannot have b before a. So any 'a' must precede any 'b' if a<b. So the total order must respect the ordering by character value: for any two characters, if char1 < char2, then all occurrences of char1 must come before all occurrences of char2. That is a strict total order on distinct letters. So we have a partial order defined by character values (a DAG: all positions with letter 'a' must come before all with 'b', etc.). Additionally, we have the color-chain constraints. We need to assign a total order (i.e., a permutation) that respects both sets of constraints. And such a total order exists iff the combined partial order is acyclic. Since we are free to choose the coloring, we need to see if there exists a coloring such that the partial order given by the letter ordering and the two chains (one for each color) is consistent. But note the two chains are defined by the coloring: each color class imposes that original order indices are maintained. So we have a set of constraints: For each pair of indices i < j:
If they have same color, then we must have i before j in final order.
If they have different colors, there is no direct constraint, but the letter ordering might impose constraints: if s[i] > s[j], then in final sorted order, j (the smaller) must come before i (the larger). However, if s[i] == s[j], there is no ordering requirement (they can be in any order relative to each other). So for i and j with s[i] > s[j], we require that final order puts j before i. But if we already have a constraint from coloring that i must come before j (if same color and i<j but s[i] > s[j]), then it's impossible because we need i before j from same-color, but j before i from letter ordering => conflict. So to avoid conflict, for any i<j with s[i] > s[j], they cannot be assigned the same color. Because if they are same color, the chain constraint forces i before j, but letter order demands j before i => contradiction. Therefore, a necessary condition: For any inversion (i<j and s[i] > s[j]), the two indices must have different colors.
Conversely, if we assign colors such that every inversion pair (i<j, s[i] > s[j]) has different colors, is that sufficient? Let's examine. The constraints we need to satisfy are:
For all i<j with same color: i before j.
For all i,j with s[i] < s[j] (strictly less): we need i before j in final order? Actually careful: The sorted order demands that if character at i is less than character at j, then the final position of i must be before that of j if we consider all occurrences. But more precisely, sorted order requires that all 'a' come before all 'b', etc. So the constraint is: For any two positions p and q, if s[p] < s[q], then p must come before q. That is a global requirement. However, if s[p] == s[q], no constraint. So the partial order from sorting is: define a relation: if s[i] < s[j] then i must precede j. This relation is transitive and forms a partial order (actually a total preorder?). But note that if s[i] < s[j] and also s[j] < s[k], then i before k. Also, it imposes that all positions with smaller letters come before all with larger letters. So it's a comparability between any two positions with different letters.
Now, if we assign colors, we also have the same-color chain constraints. The combined partial order must be acyclic. The potential cycles could come from:
Two indices i<j with same color and s[i] > s[j] -> then we have i before j (chain) and j before i (since s[i] > s[j] means s[j] < s[i] => j before i). That's a direct 2-cycle. So that must be avoided: as said, for any inversion (i<j, s[i] > s[j]), they must be different colors.
More complex cycles involving multiple indices: For example, i<j<k with colors maybe creating constraints that combine to a cycle. Suppose i and j are different colors, j and k same color, i and k? We need to examine if the condition "every inversion pair has different colors" is sufficient to avoid any cycle. Let's think.
We have partial orders:
P1: For any i<j, if color[i]==color[j] then i->j (i precedes j).
P2: For any i,j (any order), if s[i] < s[j] then i->j.
We need that there is a linear extension (total order) consistent with both. This is equivalent to the directed graph defined by these constraints being acyclic.
Now, note that P2 is already a DAG (since letters from a..z, it's like layers: all 'a' must come before all 'b', etc.). However, if there are multiple same letters, there is no constraint between them. So P2 is a partial order that is basically a total order on distinct letters, but ties among equal letters are not constrained.
P1 adds edges based on coloring.
If there is any cycle, it must involve at least one P2 edge and at least one P1 edge (since P2 alone is acyclic, P1 alone is acyclic as it's a set of chains by original order). So cycles arise from interactions.
Consider three indices: i<j<k. Suppose s[i] = 'c', s[j] = 'b', s[k] = 'a'. This is decreasing. Inversions: (i,j), (i,k), (j,k) all are inversions because c>b>a. So all pairs must be different colors according to the inversion condition. But if we assign colors such that each adjacent pair? Actually need all three pairwise different colors? But only two colors available. So it's impossible to assign 3 indices such that every pair has different colors because with two colors, you cannot have a 3-clique of distinct colors. So such a triple would make it impossible. That suggests that the inversion graph (graph with edges between inversions) must be bipartite. Indeed, if we consider the graph where vertices are indices, and we draw an edge between i and j if i<j and s[i] > s[j], then to satisfy the condition that each inversion pair has different colors, we need a proper 2-coloring of this graph. But is this graph always an interval graph? Actually, it's a comparability graph? Let's examine: The graph defined by inversions in a sequence: It is the graph of a permutation; it's known as the inversion graph or permutation graph. Permutation graphs are comparability graphs of permutations, and they are known to be perfect and can be checked for bipartiteness. For a permutation graph, bipartiteness is equivalent to the permutation being separable? Not exactly. But we can test bipartiteness easily for n≤200. However, is the condition "every inversion pair must be different colors" also sufficient? Let's test on the sample where answer is NO: s = abcdedc (n=7). Let's compute inversions.
s = a b c d e d c
Indices 1..7:
1:a
2:b
3:c
4:d
5:e
6:d
7:c
Now inversions: i<j, s[i] > s[j]. Check pairs:
(5,e) vs (6,d): e>d -> inversion.
(5,e) vs (7,c): e>c -> inversion.
(6,d) vs (7,c): d>c -> inversion.
Also (4,d) vs (6,d)? d==d not >.
(4,d) vs (7,c): d>c inversion? d>c, so (4,7) inversion.
(3,c) vs (7,c): c==c no.
(3,c) vs (6,d)? c<d no.
(3,c) vs (7,c) already.
(2,b) vs (7,c)? b<c no.
(2,b) vs (6,d)? b<d no.
(2,b) vs (5,e)? b<e no.
(1,a) vs others: a is smallest, no inversion.
So edges: (4,7), (5,6), (5,7), (6,7). Also (4,7) yes. Also (4,6)? d and d equal no. (3,7)? c and c equal no.
Also (4,5)? d<e no. (4,6)? d=d no. (3,6)? c<d no. (3,5)? c<e no.
So graph: vertices {4,5,6,7} essentially. Edges: 4-7, 5-6, 5-7, 6-7. Check bipartiteness: Try to 2-color. Suppose 4=0, then 7=1 because edge 4-7. Now edge 5-7 implies 5 must be 0 (since 7=1). Then edge 5-6 implies 6 must be 1 (since 5=0). But edge 6-7: 6=1 and 7=1 -> conflict because they are adjacent. So graph is not bipartite. So answer NO matches condition.
Now test first sample: s=abacbecfd, n=9. Output: YES with coloring 001010101. Let's verify if the inversion graph is bipartite. We can test quickly mentally maybe: We'll compute inversions later.
Second sample: aaabbcbb, output YES. Inversions? Since string is almost sorted: a a a b b c b b. Check: indices: 1:a,2:a,3:a,4:b,5:b,6:c,7:b,8:b. Inversions occur when a larger letter appears before a smaller. c before b? index6:c, index7:b => inversion. Also maybe b before b? equal not inversion. Also earlier b before later b no inversion. So only inversion edge between 6 and 7? Also check if any b before a? But b's appear after a's, so no. So inversion graph has single edge 6-7, which is bipartite. Coloring could be anything that assigns different colors to 6 and 7. Their solution 01011011: let's see positions: 1:0,2:1,3:0,4:1,5:1,6:0? Actually given string: 8 positions, coloring: "01011011". So index1=0,2=1,3=0,4=1,5=1,6=0,7=1,8=1. They assign 6=0,7=1 => different, okay. So condition holds.
Third sample: abcde, sorted. Inversions none, graph empty, trivially bipartite. Coloring all 0's works (output 00000). So condition works.
Thus it seems the necessary and sufficient condition is that the inversion graph (i<j and s[i] > s[j]) is bipartite. But is it always sufficient? Let's try to construct a counterexample where inversion graph is bipartite but still no coloring exists? Consider the constraints we have: we need to assign colors to vertices (indices) such that for any inversion pair, colors differ. That is exactly a proper 2-coloring of the inversion graph. So if the inversion graph is bipartite, we can color it properly (maybe multiple ways). But does such a coloring guarantee that the combined partial order (chain + sorting) is acyclic? We need to ensure that no cycle arises from more complex interactions beyond direct inversion conflicts.
Suppose we have a proper 2-coloring of the inversion graph. Then for any i<j with s[i] > s[j], colors differ. However, could there be a cycle like i before j via chain, j before k via letter order, k before i via chain? For such a cycle to exist, we need constraints: i and j same color with i<j (so i->j), and s[j] < s[k] (so j->k), and k and i same color? But k and i might not be comparable in index order; but chain constraints only apply if they have same color and order index. So suppose i and j same color, i<j -> i before j. For j->k we need s[j] < s[k] (j before k). For k->i we need either same color and k<i? But if k and i same color, then we need either k before i if k<i, or i before k if i<k. Since we want k before i, we need k<i and same color (so k->i). But then we have i<j, j<k? Actually indices order: i<j, but we have k<i, so k < i < j. So we have k < i < j, colors: i and j same, k and i same (so k and i same color). Since k<i, chain gives k before i. Then we have i before j, j before k? But j before k from s[j] < s[k]? That would require s[j] < s[k] to have j->k. However, if k < i < j, then indices: k is smallest, then i, then j. s[j] < s[k]? That would be an inversion because k<j and s[k] > s[j]? Wait, we need j->k meaning in final order, j must come before k. That requires s[j] < s[k]? Actually sorted order: if s[j] < s[k], then j must precede k. So yes, j before k if s[j] < s[k]. So that would be an edge in the sorting constraints from j to k.
But we also have inversion edges: if s[k] > s[j] (since k<j and s[k] > s[j] would be inversion), but here s[j] < s[k] so not inversion. So the condition of proper coloring does not directly affect this. Let's check if such a configuration can happen while inversion graph is bipartite. Consider indices k < i < j. Colors: k and i same, i and j same => k, i, j all same color? Because if k and i same, and i and j same, then all three same color (since colors are either 0 or 1). So they are all same color. Then chain constraints: k before i (since k<i), i before j (since i<j). So we have k->i->j. Additionally, sorting constraint: j before k (since s[j] < s[k]? Wait j before k if s[j] < s[k]? Actually we need j->k for the cycle, so we need s[j] < s[k] to force j before k. But also note that if s[j] < s[k] and k < j, then this is not an inversion because inversion requires s[k] > s[j] (i.e., bigger before smaller). s[j] < s[k] is the opposite: smaller later, larger earlier. That's actually an inversion? Let's define inversion: i<j and s[i] > s[j]. Here k<j and s[k] > s[j] would be inversion. But if s[j] < s[k], that's the same as s[k] > s[j], yes! Because if s[j] < s[k], then s[k] > s[j]. So k<j and s[k] > s[j] is an inversion. So if k<j and s[j] < s[k], then that is an inversion, so they must have different colors. But in our configuration, k and j are same color (since all three same). That violates the inversion condition. Therefore such a cycle cannot occur if the inversion graph is properly colored. Indeed, if we have a cycle, it must involve at least one inversion edge. But we are ensuring that all inversion edges are between different colors, which likely breaks cycles.
We need to formally prove sufficiency: If we 2-color the inversion graph properly (i.e., for any inversion i<j, colors differ), then the combined partial order is acyclic. Let's attempt a proof:
Define a directed graph D with vertices indices. Add edges:
Type A: for i<j with same color, add i->j.
Type B: for any i,j with s[i] < s[j], add i->j.
Note type B edges also include those where i>j? Yes, direction is from smaller character to larger character regardless of index order. So it's a complete DAG between distinct letter groups.
We need to show D is acyclic.
Suppose there is a cycle. Consider the smallest character value among vertices in the cycle. Let v be a vertex with minimal character in the cycle. Since it's a cycle, there is an edge into v from some u. That edge must be of type B? Could it be type A? Type A edges always go from smaller index to larger index, so if u->v is type A, then u index < v index and same color. But then character? Type A imposes no restriction on characters. However, since v has minimal character, any edge entering v cannot be a type B edge, because type B edges go from smaller character to larger character. If u->v is type B, then s[u] < s[v], contradicting minimality of s[v] (unless equal, but if equal s[u]=s[v], type B does not exist because s[u] < s[v] is strict; if equal, no B edge). So entering edge must be type A. So u->v is type A, meaning u<v and color[u]==color[v].
Now, since it's a cycle, there is also an edge from v to some w. This could be type A or B. Consider v->w.
Case 1: v->w is type B: then s[v] < s[w], but v is minimal character, so s[v] < s[w] is possible (since w larger). That's fine.
Case 2: v->w is type A: then v<w and same color.
Now, because we have a cycle, we must eventually return to u. We need to derive a contradiction with the coloring condition.
Alternatively, we can try to show that the partial order is a suborder of a total order we can construct. Actually, we can think of the final sorted order: we want to place all characters in non-decreasing order. If we have a proper 2-coloring, can we always interleave the two colored subsequences to get the sorted order? Maybe we can explicitly construct the final arrangement: take the sorted order of the string, and assign each occurrence to a color by processing from left to right and matching to the earliest unused index of that color? This is reminiscent of checking if the coloring yields a valid merge into sorted order. This is similar to the problem of determining if a permutation can be partitioned into two increasing subsequences? Wait, there is a known concept: The inversion graph bipartite is equivalent to the permutation being "separable" or something? Actually, a permutation can be partitioned into two increasing subsequences if and only if the permutation avoids the pattern 321? That's for sorting? Let's recall: In the context of sorting with two stacks, etc. But here we have not just increasing subsequences; the constraints are different because the sorting order is based on character values, not just indices. However, note that if the string is already sorted (non-decreasing), then inversion graph is empty, and we can assign any colors. But if the string is not sorted, we have inversions.
Let's try to interpret the problem in terms of two queues: We have two sequences (color0 and color1) in original order. We need to merge them to obtain sorted order. This is exactly like we have two queues, and we need to output sorted order by dequeuing from the front of either queue, but we must preserve order within each queue. The question: given initial sequence and a coloring, can we output the sorted sequence by repeatedly taking the first element of either queue (like a merge)? Because allowed swaps essentially allow us to reorder arbitrarily between queues, but within each queue the order is fixed. So the final sequence is some interleaving of the two queues, and we can achieve any interleaving by appropriate swaps? Actually, can we achieve any interleaving? Suppose we have two queues: we can take elements from the front of either queue and place them in output. This yields exactly the set of interleavings that respect each queue's order. And it's known that by swapping adjacent different colored items, we can simulate any such interleaving: you can bubble the desired next element to the front. So yes, the feasible final sequences are exactly the interleavings of the two colored subsequences.
Thus, the problem reduces to: can we assign colors such that the two subsequences (in original order) can be merged to produce the sorted string.
Now, given a coloring, when can we merge to sorted? This is similar to the problem of checking if a permutation can be partitioned into two monotone sequences with respect to some target order. Equivalent: we have original sequence s, and we assign each element to one of two stacks (or queues) but we need to output sorted order.
Alternatively, we can think of it as: We process the sorted target string t (which is s sorted). We need to match each character of t to a position in s, respecting that each position used once. Additionally, we have two colors, and for each color, the matched positions must appear in increasing order (since we take from the front of the queue). That is, if we map each character occurrence in t to its source index in s, then for each color, the indices of that color must be in increasing order (as we consume from the queues). Also, the mapping must be a bijection that respects the multiset: i.e., we assign each occurrence of a letter to a particular original occurrence.
But we can also decide coloring first, then try to see if merging works. However, maybe the condition "inversion graph bipartite" is indeed necessary and sufficient.
Let's test with some more examples.
Example: s = "cba". n=3. Inversions: (1,2):c>b, (1,3):c>a, (2,3):b>a. Inversion graph is a triangle (complete graph on 3 vertices). This is not bipartite. So answer should be NO. Can we indeed not color? Let's think: to make sorted "abc". Is it possible? We would need to rearrange to abc. With two colors, can we? If we color 0/1, then relative order of same color preserved. So we need to split indices into two sequences that when interleaved yield a,b,c. Suppose we try: maybe color1: index1(c) and index2(b) same color? Then order c before b, but in sorted order we need a before b before c, so c must come last. If c and b same color, they appear in order c then b, so c before b in final. That's opposite of needed. To have b before c, they must be different colors? Actually if they are different colors, we can reorder arbitrarily; we could place b before c. But also need a (index3) before both? a is smallest. a must be first. So we need to place a first. a is index3. To put a first, we need to be able to bring a to front. That requires swapping past c and b. Since we can swap adjacent different colors, if a is a different color from its neighbors as it moves, it's possible. But we also need to ensure final order b before c. Let's attempt a specific coloring: assign index1(c)=0, index2(b)=1, index3(a)=0. Then same color pairs: index1 and index3 both 0, so order must be 1 before 3? Actually indices: 1 (c) and 3 (a) both 0, so constraint: index1 before index3. That means c must appear before a in final order. But sorted order requires a before c. Contradiction. So that coloring fails. Try c=0, b=0, a=1: then 0-colored indices:1,2 -> order c before b. Sorted order needs b before c. So conflict because same color forces c before b. So fail. Try c=0, b=1, a=1: then 1-colored indices:2,3 -> order b before a (since index2 before index3). Sorted order needs a before b. Conflict. Try c=1, b=0, a=0: 0-colored:2,3 -> b before a, but need a before b -> conflict. Try c=1, b=1, a=0: 1-colored:1,2 -> c before b, need b before c -> conflict. Try c=1, b=0, a=1? That's not two colors? Actually we have only two colors, so each index gets either 0 or 1. So all combinations are covered. All 8 possibilities? Actually 2^3=8. The ones we haven't: c=0,b=1,a=0? That we did? c=0,b=1,a=0: 0-colored:1,3 -> c before a (need a before c) conflict. c=0,b=0,a=1 done. c=1,b=0,a=0 done. c=1,b=1,a=1? All 1: then all same color, so order forced as original c,b,a, which is not sorted. So indeed impossible. So NO, consistent.
Now, try a case where inversion graph is bipartite but maybe still impossible? Consider s = "b a a"? Actually need distinct? Let's construct: n=3, s = "bac". Indices:1:b,2:a,3:c. Inversions: (1,2) because b>a; (1,3) b<c? no because b<c is not inversion; (2,3) a<c no. So only inversion edge between 1 and 2. That graph is a single edge, bipartite. So we can color: e.g., 1=0,2=1,3 arbitrary (say 0). Let's see if we can merge to sorted "abc". Colors: 0 indices:1(b),3(c) order: b before c. 1 index:2(a). We need to produce a,b,c. Can we? Merge: we need a first. a is color1, we can put it first. Then b then c. b and c are both color0, order b before c, good. So we can interleave: a,b,c. Works. So YES.
Consider s = "c a b". Indices:1:c,2:a,3:b. Inversions: (1,2) c>a; (1,3) c>b; (2,3) a<b no. So edges: 1-2, 1-3. That's a star centered at 1, edges to 2 and 3. This graph is bipartite (1 with color0, 2 and 3 color1). So coloring: 1=0, 2=1, 3=1 (or 1=0,2=1,3=0? But edge 1-3 requires different colors, so if 3=0 conflict. So 1=0,2=1,3=1 works). Check: Color0: index1(c) alone. Color1: indices2(a),3(b) order: a before b. Need sorted "abc". Can we produce a,b,c? a and b are color1, order a before b; c is color0. To get a first, then b, then c: need c last. That's fine, because we can interleave: a, b, c. The order within color1 is a then b, ok. So works.
Now consider a more complex case: s = "d a b c". indices:1:d,2:a,3:b,4:c. Inversions: (1,2):d>a; (1,3):d>b; (1,4):d>c. So edges from 1 to 2,3,4. Also (2,3):a<b no; (2,4):a<c no; (3,4):b<c no. So graph is star, bipartite. Color 1=0, others=1. Then color0: d alone; color1: a,b,c in order a,b,c. Sorted target: a b c d. Interleave: a,b,c,d works. So yes.
Now consider s = "b c a". indices:1:b,2:c,3:a. Inversions: (1,3):b>a; (2,3):c>a. So edges: 1-3,2-3. Graph is like a V: vertex 3 connected to both 1 and 2. That's bipartite (e.g., 1=0,2=0,3=1). But check if possible: Colors: 1=0,2=0,3=1. Color0: indices1(b),2(c) order b before c. Color1: index3(a). Sorted target: a b c. Need a first, then b, then c. But color0 order forces b before c, that's fine. However, we need a before b and a before c. a is color1, can be placed first. So interleaving a,b,c works? We need to output a (index3), then b (index1), then c (index2). That respects color0 order? Color0 order is index1(b) then index2(c). In the output, b is before c, good. So works. So YES.
Now, is there any bipartite inversion graph that fails? Let's attempt to construct a potential counterexample.
We need that the inversion graph is bipartite, but there exists a cycle in the combined constraints. Consider indices i<j<k with colors maybe causing conflict not directly inversion. Suppose we have i and j same color (so i before j), j and k same color (so j before k). That gives i before k via transitivity. Now suppose s[i] > s[k]? That would be an inversion (i<k and s[i]>s[k]) => they must have different colors. But if i and k are same color (since both same as j) then conflict. So cannot happen. So any chain of same-colored indices must be increasing in value? Actually, if i<j<k and all same color, then by inversion condition we must have s[i] <= s[j] <= s[k]? Because if s[i] > s[j] then inversion between i and j would force different colors, contradiction. So within same color, the subsequence must be non-decreasing. Indeed, if any two indices with same color are in inversion (i<j and s[i] > s[j]), that's forbidden. Therefore, in a valid proper coloring of the inversion graph, each color class itself forms an increasing (non-decreasing) subsequence in terms of both index order (by definition of the class order) and character order (since no inversion within class). That is, for same color, the indices are in increasing order, and the characters are non-decreasing. So each color class is a non-decreasing subsequence. That's a key observation! Let's verify: If i<j and same color, then we cannot have s[i] > s[j] because that would be an inversion requiring different colors. So s[i] <= s[j]. Therefore each color class, when read in original order, is non-decreasing. So each color class is a sorted (non-decreasing) sequence. Great!
Now, if each color class is individually sorted (non-decreasing), then merging them in any way that respects their orders can produce a sorted overall sequence? Not necessarily, as earlier example showed: [2,4] and [1,3] are both sorted, but interleaving 2,1,4,3 is not sorted. However, we are not forced to interleave arbitrarily; we need to achieve the globally sorted string. But can we always merge two sorted sequences to get a globally sorted sequence? The answer is yes if we merge in the standard way: always pick the smaller current element. That merge yields a sorted sequence. So if we have two sorted subsequences, the merge (taking the smaller head) yields a sorted sequence. However, is that resulting sequence exactly the sorted version of the original multiset? Since the two subsequences together contain exactly the multiset of characters, merging them yields some sorted sequence. That sorted sequence is the unique sorted arrangement of the multiset (if we merge correctly, but the sorted arrangement is unique only up to equal letters? Actually, if there are duplicate letters, there may be multiple sorted sequences? A sorted non-decreasing sequence is unique in terms of order of letters: for a given multiset, there is exactly one non-decreasing sequence (since you must place all smallest letters first, etc.). So the sorted target t is unique. Therefore, if we have two sorted subsequences, merging them by always taking the smallest available (i.e., standard merge) will produce t, because t is the sorted sequence. But is it guaranteed that the merge of the two sorted subsequences yields exactly t? Let's consider two sorted sequences A and B. Their merge (by taking the smaller front) yields the sorted merge of A and B, which is indeed the sorted combination of A∪B. Since A∪B = multiset of s, the merge result is the sorted version of s. So yes, it's automatic: If A and B are sorted (non-decreasing), then the merge algorithm produces the sorted overall sequence. So any two sorted subsequences (preserving order) can be merged to give the sorted total. However, we must be careful: The merge algorithm yields a specific interleaving; is that interleaving achievable with the allowed swaps? Since we can reorder arbitrarily between colors, we can produce any interleaving, so certainly we can produce the merge interleaving. So indeed, if each color class is non-decreasing (sorted), then the answer is YES: just perform the merge to get the sorted string.
Thus, the problem reduces to: Can we partition the sequence into two non-decreasing subsequences? Equivalent to: Can we 2-color the indices such that there is no inversion within the same color? That is exactly the condition that the inversion graph is bipartite, because an inversion edge exists between i<j with s[i] > s[j]. Requiring that no such pair has same color is exactly a proper 2-coloring of the inversion graph.
Therefore, the necessary and sufficient condition is that the inversion graph is bipartite.
But wait, is there any subtlety about equal characters? For equal characters, i<j and s[i] == s[j], that is not an inversion, so they can be same color or different. However, could having equal characters in the same color cause a problem with merging? If both same color and equal, that's fine because non-decreasing allows equals. So no issue.
Thus, the solution is: Build a graph with vertices 1..n. For each i<j, if s[i] > s[j], add an undirected edge between i and j. Then check if the graph is bipartite (2-colorable). If not, output NO. If yes, output YES and a valid 2-coloring (any bipartition). Since n ≤ 200, we can easily construct O(n^2) edges and run BFS/DFS for bipartiteness.
But we must be careful: The graph may have many edges (up to ~n^2/2=20k), which is fine.
However, is it always true that if the inversion graph is bipartite, we can partition into two non-decreasing subsequences? Let's double-check with an example that might be tricky: Consider s = "2 1 2"? But letters: for instance, s = "b a b". indices:1:b,2:a,3:b. Inversions: (1,2) b>a; (1,3) b=b? equal not inversion; (2,3) a<b not inversion. So only edge between 1 and 2. Graph bipartite. Coloring: 1=0,2=1,3 arbitrary (say 0). Then color0 subsequence: indices1:b,3:b -> non-decreasing? b <= b, yes. Color1: index2:a -> non-decreasing trivially. So two sorted subsequences. Merge: sorted of "bab" is "abb". Can we get "abb"? A merge: color0: b,b; color1: a. Standard merge: compare a and b -> output a, then both b's -> b,b. So abb. Works.
Now consider s = "1 3 2 4"? Actually letters: a,c,b,d? Let's take numeric: 1,3,2,4. Inversions: (2,3) because 3>2. So edge 2-3. Graph bipartite. Color: e.g., 1=0,2=0,3=1,4=0? But must respect edges: 2 and 3 different colors. So assign 2=0,3=1. Others arbitrary. Check color0 subsequence: indices1:1,2:3,4:4 => 1,3,4 which is increasing (good). Color1: index3:2 -> single. Merge: sorted is 1,2,3,4. Merge: color0 next=1, color1 next=2 => output 1 (color0), then compare 3 vs 2 => output 2 (color1), then 3, then 4. Works.
Now what about a more complex graph? The bipartiteness ensures we can 2-color without conflicts. But does that guarantee that each color class is non-decreasing? Yes, because within a color class, any i<j must not have s[i] > s[j] (otherwise they'd be an edge and would have different colors). So s[i] <= s[j] for all i<j same color. So each class is non-decreasing. So that's sufficient.
Thus the solution is straightforward.
But wait, what about the possibility of having a proper 2-coloring that results in each color class non-decreasing, but the bipartite condition is exactly that. So we just need to find a proper coloring of the inversion graph.
Implementation steps:
Read n and s (string of length n).
Create adjacency list for graph of n nodes.
For i in range(n):
for j in range(i+1, n):
if s[i] > s[j]:
add edge i-j.
Perform BFS/DFS to check bipartiteness and assign colors (0/1) for each node (graph may be disconnected). Use an array color of length n initialized to -1.
For each node not colored, set color to 0, then BFS: for each neighbor, if same color, output NO; else set opposite.
If no conflict, output YES and the colors as a string of '0'/'1' corresponding to the assigned colors.
But careful: The colors we assign (0/1) correspond to the required output coloring (the answer). However, note that our assignment from bipartite coloring may produce a coloring where each color class is non-decreasing, but is that the only requirement? The problem statement: "you have to color all its characters one of the two colors... After coloring, you can swap any two neighboring characters of the string that are colored different colors... The goal is to make the string sorted." We have proven that if we have a proper coloring of inversion graph, then we can sort. So yes.
Let's verify with provided examples.
Example1: s=abacbecfd. Let's compute inversion graph manually? It should be bipartite because they gave YES. We'll trust.
Example2: aaabbcbb -> bipartite.
Example3: abcdedc -> not bipartite as we saw.
Example4: abcde -> bipartite.
All match.
But we should also consider edge cases: n=1, no inversions, graph trivial, bipartite. Output any color, e.g., "0". Good.
Now, the problem statement says "easy version ... the constraints and output format are different." Constraints: n ≤ 200. So O(n^2) is fine. In hard version maybe n up to 2e5, requiring a more efficient method.
Thus, this solution is correct.
However, we must consider one more nuance: The swapping operation allowed is only between neighboring characters of different colors. As argued, we can achieve any interleaving. But is it always possible to achieve exactly the sorted string via swaps? Given that each color class is non-decreasing, we can merge them by repeatedly taking the smaller front, but can we simulate that sequence of swaps? Typically, if we have two stacks or two queues, you can reorder by swapping adjacent different colors to bring the desired element to the front. There is known equivalence: with two colors, you can permute arbitrarily as long as the relative order within each color is fixed. This is because you can bubble any element of color 0 to the left past any sequence of 1's by swapping with them (since swapping allowed only for different colors). Similarly, you can move 1's past 0's. So you can achieve any interleaving. Formal proof: Consider the final desired interleaving. We can transform the original sequence to that interleaving by a series of adjacent swaps of different colors: we can treat it as sorting by bubble sort where we compare desired order; when we find a pair that is out of order relative to the target, they must be of different colors (since same color order fixed). Actually, careful: The relative order of same-color items must be the same in the target as in original. In our case, target is the sorted merge. Since each color class is non-decreasing, the target interleaving will respect the original order within each color (because merge of two sorted sequences respects the order of each). So indeed, the target permutation preserves same-color orders. Then we can transform to it by adjacent swaps: this is like sorting a sequence where items have colors, and we can swap adjacent items of different colors. The set of reachable permutations are exactly those that preserve the relative order of same-colored items. This is a known fact: it's a parallel to the fact that by swapping elements of different types, you can achieve any shuffle that preserves type-order. So yes.
Thus condition sufficient.
One more nuance: Could there be a scenario where inversion graph bipartite but the proper coloring we obtain does not produce each color class non-decreasing? Wait, by definition of proper coloring, for any inversion edge, vertices have different colors. So for any i<j same color, there is no inversion edge between them, meaning s[i] <= s[j]. So yes, non-decreasing. So all good.
Therefore, the problem is solved.
Now, we need to output the coloring string. Our BFS coloring gives a valid 2-coloring. However, note: The output expects '0' and '1' characters. Our assignment can be either 0 or 1 for each node, as long as it's consistent. There might be multiple valid colorings; any is fine.
Edge Cases: The graph may be disconnected. BFS will assign colors arbitrarily start with 0 for each component. That yields a valid coloring.
Complexity: O(n^2) edges, O(n^2) time.
Now, implement.
But before finalizing, let's double-check the reasoning with a slightly more intricate example that might reveal a hidden condition.
Consider s = "c a b d". indices:1:c,2:a,3:b,4:d. Inversions: (1,2) c>a, (1,3) c>b. No other inversions because (2,3) a<b, (1,4) c<d, (2,4) a<d, (3,4) b<d. So graph edges: 1-2, 1-3. That's bipartite. Color: 1=0, 2=1, 3=1, 4=0 (say). Then color0: indices 1(c),4(d) -> c,d non-decreasing? c <= d yes. Color1: indices2(a),3(b) -> a,b non-decreasing. Merge: a,b,c,d -> sorted. Works.
Now, what if we have equal letters but with inversion condition? For equal, no edge, so they can be same color or different. But could having both equal letters same color cause a problem for merging? Suppose s = "a a a a". All same, any coloring works. Merge trivial.
Consider s = "a b a". Indices:1:a,2:b,3:a. Inversions: (2,3) b>a => edge 2-3. Graph single edge, bipartite. Color: 2=0,3=1,1 arbitrary, say 0. Then color0: indices1:a,2:b -> a,b non-decreasing. Color1: index3:a -> a. Merge: sorted is a a b. Can we produce a a b? With color0: a,b; color1: a. Standard merge: compare a (color0) and a (color1). Both equal, can pick either. If we pick color0's a first, then we have remaining color0: b, color1: a. Then need a then b. But order: next smallest is a from color1, then b. So output a,a,b. That respects color0 order (first a, then b) and color1 order (a). So yes.
If we had colored differently: 1=0,2=0,3=1 -> color0: a,b (non-decreasing), color1: a. Same. Good.
Now, consider s = "b a b a". indices:1:b,2:a,3:b,4:a. Inversions: (1,2) b>a, (1,4) b>a, (2,3) a<b? No, a<b not inversion. (2,4) a>a? equal no. (3,4) b>a. So edges: 1-2, 1-4, 3-4. Graph: vertices 1,2,3,4; edges: 1-2, 1-4, 3-4. Is this bipartite? Let's check: try to 2-color. 1=0 => 2=1, 4=1. Then 3 connected to 4, so 3 must be 0. Check edges: 1-2 ok, 1-4 ok, 3-4 (0-1) ok. So bipartite. Color0: indices1:b,3:b => b,b non-decreasing. Color1: indices2:a,4:a => a,a non-decreasing. Merge sorted of "baba" is "a a b b". Can we merge to get that? Color0: b,b; color1: a,a. Standard merge: compare a and b -> output a (color1), then compare a and b -> output a (color1), then b,b. So yields a,a,b,b. Good.
Now, consider a trickier case: s = "c b a d". indices:1:c,2:b,3:a,4:d. Inversions: (1,2) c>b, (1,3) c>a, (2,3) b>a. So edges: 1-2,1-3,2-3. That's a triangle (1,2,3). Not bipartite. So NO.
Thus seems consistent.
Now, there might be a nuance about the fact that we can only swap neighboring characters of different colors. We argued that we can achieve any interleaving that respects color orders. But is it always possible to achieve the exact target permutation via a sequence of such swaps? Yes, it's analogous to the fact that the set of permutations that preserve relative order within each color is reachable. More constructively: we can simulate a stable merge: repeatedly, if the first character is not the one that should be next in the sorted order, but its color differs from the next character, we can swap them. But we need to guarantee that we can bring the correct character to the front. Consider we have two queues, we can pop from front of either by moving it through the opposite colors. However, it's possible that the needed character is blocked by a same-colored character? Since same-colored items cannot be swapped, they maintain order. But in the final merge, the order of same-colored items is the same as original. So if we need to bring a particular occurrence of a color to the front, there might be earlier same-colored items that must come before it. But those earlier ones should also be part of the merge sequence earlier. So if we follow the merge order, we always take the smallest among the two front items of the two colors. At each step, the item that should be output is either at the front of the current combined list? But the combined list currently is the current string. We need to ensure we can bring it to position 1 by swapping with adjacent different-colored items. If the item is not at the front, there are items before it. Some of those items could be of the same color as the target, which cannot be swapped past it. However, if the target is of color X, then any same-colored item before it must be output before it (since they are in order). In the merge order, those earlier same-colored items will be output earlier if they are smaller or appropriate. So by the time we need the target, those same-colored items will have been already removed (output) if they should come before it. This is like the classic proof that a permutation is reachable if it respects the two sequences' orders. We can also argue: The allowed swaps generate the group of permutations that preserve the relative order of each color. So any such permutation is reachable. Since our target sorted permutation preserves these orders (as each color class is sorted and the merge respects order), it is reachable. So sufficiency holds.
Thus, solution stands.
Now implement.
But wait: The problem says "you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times." So the operations are only allowed if the two neighboring characters have different colors. That's exactly what we used.
Therefore, code.
Now, confirm with sample input:
Input:
9
abacbecfd
We should compute graph and output a coloring. Let's simulate quickly to ensure it matches given output? The output given is "001010101". That is a specific coloring. Our algorithm might output a different valid coloring, but any is acceptable. So it's fine.
Now, think about potential pitfalls: The graph might have many edges, but n is only 200, so adjacency list is fine. BFS for each component.
Edge case: If the graph is empty (no inversions), BFS will assign all nodes color 0 (since we start with 0 for each uncolored, and no edges so all remain 0). That yields "000...0". That is valid. But output might be all 0's, or we might assign some 1's due to multiple components? If graph has no edges, each node is isolated; BFS will color each component arbitrarily (we start with 0). So they all get 0. That's okay.
Now, about reading input: The first line integer n, second line string s. Possibly there are spaces? No, it's a string.
Implementation details: Use 0-based or 1-based indexing? We'll use 0-index.
Now, time complexity: O(n^2) for building edges, plus O(n + E) for BFS. E can be up to n*(n-1)/2 ≈ 20k, fine.
Now, test with custom cases.
Case: n=2, s="ba". Inversions: (0,1) because b>a -> edge. Graph: single edge bipartite. BFS: assign colors 0 and 1. Output e.g., "01" or "10". Both correct.
Case: n=2, s="ab". No inversion, graph empty. BFS gives "00" or "00"? Actually both get 0: "00". That works? Check: colors both 0, same color; order fixed: a then b -> sorted. Allowed swaps? Since both same color, cannot swap but already sorted. So YES.
Case: n=2, s="aa". No inversion, "00" works.
Now, let's test with a case where inversion graph is bipartite but we might incorrectly assign colors that cause conflict? The BFS ensures proper coloring, so fine.
Now, confirm with a case where inversion graph has multiple components and we might assign colors that conflict? No, BFS ensures each component properly colored; different components have no edges so no cross constraints.
Now, think about the possibility that the graph is not connected. In that case, we assign colors arbitrarily per component. That's fine.
Now, about output: Print "YES" on first line (if possible) and coloring string on second line. If impossible, print "NO".
Now, I'll write code.
But before final, I recall a known result: The condition of bipartiteness of the permutation graph corresponds to the permutation being separable? Actually, for a permutation, being 2-colourable under inversion graph is equivalent to the permutation avoiding the pattern 321? Wait, a permutation that is 321-avoiding has inversion graph that is perfect and may be bipartite? But the permutation 321 (i.e., 3,2,1) has inversion graph a triangle, not bipartite. So any permutation containing 321 is not bipartite. But there exist permutations not containing 321 that still have odd cycles? Let's test: 2143? Permutation 2,1,4,3? Inversion edges: (1,2) 2>1; (3,4) 4>3; also (1,4)? 2<4 no; (2,3)? 1<4 no; (2,4)? 1<3 no; (1,3)? 2<4 no. So graph has two disjoint edges, bipartite. 2413? 2,4,1,3: inversions: (2,3) 4>1; (1,3) 2>1; (2,4) 4>3; also (1,4) 2<3 no; (1,2) 2<4 no; (3,4) 1<3 no. So edges: 2-3,1-3,2-4. Graph: vertices 1,2,3,4; edges: 1-3,2-3,2-4. Is this bipartite? Let's color: set 1=0 -> 3=1; then 2 must be 0 because adjacent to 3? Actually 2-3 edge: if 3=1 then 2=0; then check 2-4: 4 must be 1. All edges satisfied: 1-3 (0-1), 2-3 (0-1), 2-4 (0-1). So bipartite. 3142? 3,1,4,2: inversions: (1,2) 3>1; (1,4) 3>2; (3,4) 4>2. Also (2,3) 1<4 no; (2,4) 1<2 no; (1,3) 3<4 no. Edges: 1-2,1-4,3-4. Graph: try bipartite: set 1=0 -> 2=1, 4=1; then edge 3-4: since 4=1, 3=0. Check all edges: 1-2 ok,1-4 ok,3-4 ok. Bipartite. So many permutations are bipartite. So condition is not exactly pattern avoidance of 321? Actually 321 is a 3-cycle, so if any triple i<j<k with s[i]>s[j]>s[k] (i.e., decreasing triple) then there are edges among all three? In a decreasing triple, all three pairs are inversions because s[i]>s[j], s[i]>s[k], s[j]>s[k]. So that gives a triangle, which is odd cycle, graph not bipartite. So necessary condition is that there is no decreasing triple. But is that also sufficient? If there is no decreasing triple, does the inversion graph become bipartite? Not necessarily; there could be longer odd cycles not formed by a triple. For instance, can we have a 5-cycle without a decreasing triple? In permutation graphs, odd cycles may exist without a triangle. But does the absence of decreasing triple guarantee bipartiteness? I'm not sure. Let's explore: Inversion graph is a comparability graph? Actually, it's a permutation graph, which is known to be perfect. For perfect graphs, bipartiteness is equivalent to being triangle-free? Not exactly: A perfect graph is bipartite iff it has no odd cycles. But triangle is an odd cycle. However, there could be odd cycles of length 5 or more without triangles. But in permutation graphs, is it possible to have an odd cycle without a triangle? Yes, I think so. For example, consider permutation 2,4,1,5,3? Let's examine: positions 1..5: [2,4,1,5,3]. Inversions: i<j, s[i] > s[j]. Let's list pairs: (1,3):2>1, (2,3):4>1, (2,5):4>3, (4,5):5>3. Also (1,5):2<3 no, (1,2):2<4 no, (1,4):2<5 no, (2,4):4<5 no, (3,4):1<5 no, (3,5):1<3 no. So edges: 1-3,2-3,2-5,4-5. Graph: vertices 1,2,3,4,5. Edges: 1-3,2-3,2-5,4-5. That's like two "V"s. No triangle. Check for odd cycle: maybe 1-3-2-5-4-? No edge 4-1. So no cycle. Try to find an odd cycle of length 5. Perhaps permutation 2,5,1,4,3? [2,5,1,4,3]. Inversions: (1,3):2>1, (2,3):5>1, (2,5):5>3, (4,5):4>3, (2,4):5>4? Actually 5>4 yes, so (2,4) inversion. Also (1,5):2<3 no, (1,2):2<5 no, (1,4):2<4 no, (3,4):1<4 no, (3,5):1<3 no. Edges: 1-3,2-3,2-5,4-5,2-4. Graph: vertices 1,2,3,4,5; edges: 1-3,2-3,2-4,2-5,4-5. Now check cycles: 3-2-4-5? That's 3-2,2-4,4-5, but need edge back to 3: none. 3-2-5-4-? maybe 4 to? no. 3-2-4-? no. So still no odd cycle. But is there a permutation graph that is odd-cycle but triangle-free? I recall that permutation graphs are comparability graphs of partial orders dimension 2, and they are perfect. A perfect graph is bipartite iff it contains no odd cycle, but it may contain odd cycles without triangles? For chordal graphs, odd cycles have chords, but permutation graphs are not necessarily chordal. However, permutation graphs are known to be exactly the graphs that are both comparability and co-comparability? Not sure. Actually, permutation graphs are a subclass of perfect graphs, and they can have odd cycles of length >=5 without chords? Let's try to construct a 5-cycle in a permutation diagram. Permutation graph is defined by two parallel lines with points, and edges correspond to crossing lines. A cycle of length 5 without chords might be possible if the permutation is something like? Let's attempt systematically: We need a set of intervals (the lines) that produce a cycle. Consider permutation π: vertices 1..n on top in order 1,2,...,n, bottom order given by π. Edge between i and j (i<j) if π(i) > π(j) (i.e., lines cross). To have a cycle of length k, we need an alternating crossing pattern. I think it's possible to have a 5-cycle in a permutation graph. Example: Permutation 2,4,6,1,3,5? Let's test n=6: positions: 1:2, 2:4, 3:6, 4:1, 5:3, 6:5. Inversions: (1,4):2>1, (2,4):4>1, (3,4):6>1, (2,5):4>3, (3,5):6>3, (3,6):6>5, (5,6):3<5? no, (1,5):2<3 no, (1,6):2<5 no, (2,6):4<5 no, (4,5):1<3 no, (4,6):1<5 no. Edges: 1-4,2-4,3-4,2-5,3-5,3-6. Graph: Let's examine cycle possibilities: 1-4-2-5-3-? 3 connected to 1? no. 2-4-1? no edge 2-1. 5-3-4-2 is a triangle? 2-4 edge, 4-3 edge, 3-2 edge? 3-2? Since 2<3 and s2=4, s3=6, 4<6 so no inversion, so no edge. So not triangle. Possibly a 4-cycle? 1-4-2-? need edge 2-1? no. So no cycle.
Maybe there is known characterization: The inversion graph of a permutation is bipartite iff the permutation avoids the pattern 321? Actually, I recall something: A permutation is separable if it avoids 2413 and 3142? Not sure. But we don't need to go that deep; we just need to check bipartiteness directly.
Given n ≤ 200, O(n^2) is fine.
Thus, solution: check bipartiteness of inversion graph.
Now, confirm that the output format: In examples, they print "YES" and then the coloring string with no spaces. That's what we'll do.
One nuance: The problem says "the coloring is the string consisting of n characters, the i-th character should be '0' if the i-th character is colored the first color and '1' otherwise." So we output '0' and '1' accordingly.
Now, implement.
Potential pitfalls: The graph may be built with edges only when s[i] > s[j]. But if s[i] == s[j], no edge. That's correct.
Also, the graph might have many edges, but BFS can handle.
Now, test with given sample 3: abcdedc. Our code should detect non-bipartite and print NO.
Now, think about: The inversion graph might have self-loops? No.
Now, let's actually compute for sample 3 programmatically mental? Already did and found odd cycle.
Thus solution stands.
I will implement in Python.
Pseudo:
def solve():
import sys
input = sys.stdin.readline
n = int(input().strip())
s = input().strip()
adj = [[] for _ in range(n)]
for i in range(n):
for j in range(i+1, n):
if s[i] > s[j]:
adj[i].append(j)
adj[j].append(i)
color = [-1]*n
from collections import deque
for i in range(n):
if color[i] == -1:
color[i] = 0
q = deque([i])
while q:
u = q.popleft()
for v in adj[u]:
if color[v] == -1:
color[v] = 1 - color[u]
q.append(v)
elif color[v] == color[u]:
print("NO")
return
# if all good
print("YES")
print(''.join(str(c) for c in color))
Now, one more subtlety: The colors we output are 0/1. They correspond to the first color (0) and second color (1). In our assignment, we used 0 and 1 arbitrarily. That's fine.
But we must ensure that the coloring yields each color class non-decreasing. Our condition ensures that for any i<j same color, s[i] <= s[j] because otherwise they'd have an edge and would be different colors. But we must also ensure that there are no contradictions in the BFS (i.e., graph bipartite). So it's correct.
Now, could there be a case where the graph is bipartite but our BFS assignment yields a coloring where some same-color pair has s[i] > s[j]? That would be a bug because then that pair would have an edge and be same color, contradicting proper coloring. But BFS ensures no adjacent vertices share same color, so if there is an edge, they are different colors. So safe.
Thus, finalize.
But let's think about an alternative approach: This problem is essentially about partition into two increasing subsequences. Another known method: Greedy algorithm for partitioning into two increasing subsequences (like patience sorting with 2 piles). But we already have a simple graph approach.
I will mentally test some random strings to ensure no oversight.
Case: s = "zxy". n=3. Inversions: (1,z)>(2,x)? z>x yes; (1,z)>(3,y)? z>y yes; (2,x)>(3,y)? x>y? x<y? Actually x=120, y=121? x < y typically? x=120, y=121 so x<y, no inversion. So edges: 1-2,1-3. That's a star, bipartite. Color: 1=0, 2=1, 3=1. Check color0: index1:z; color1: x,y (order x then y). Are both non-decreasing? color1: x <= y? x=120, y=121 yes. color0: single. Merge sorted of "zxy" is "xyz". Can we get xyz? Merge: compare x and z -> output x, then compare y and z -> output y, then z. Works. So YES.
Case: s = "d c b a". n=4, all decreasing. Inversions: all pairs (6 edges). Graph complete on 4 vertices? Actually complete graph because any i<j has s[i] > s[j]. So K4, not bipartite. So NO. That makes sense because it would require partition into two increasing subsequences of a decreasing sequence of length 4, which is impossible (need at least 3 increasing subsequences? Actually, by Dilworth, width is length of longest decreasing subsequence; the minimum number of increasing subsequences needed to cover the permutation equals the length of the longest decreasing subsequence. So for decreasing sequence of length 4, we need 4 increasing subsequences. Here we have only 2, so impossible. So NO.
Thus condition matches Dilworth's theorem: partition into k increasing subsequences is possible iff the length of longest decreasing subsequence ≤ k. Here k=2. So the condition that the inversion graph is bipartite might be equivalent to LDS ≤ 2? Actually, a permutation can be partitioned into two increasing subsequences iff it avoids the pattern 321 (i.e., no decreasing subsequence of length 3). This is a known result: the permutation is 2-increasing-coverable iff it avoids 321. Because Dilworth: min number of increasing sequences needed = size of longest decreasing subsequence. So if longest decreasing subsequence length > 2, you need more than 2. So for strings with distinct letters? But here letters can repeat, so it's not exactly a permutation. However, with repeats, we need non-decreasing sequences. The condition might be: the length of the longest strictly decreasing subsequence? But due to equal letters, we can have ties. Actually, we need each color class to be non-decreasing, which allows equal letters. The obstruction would be a strictly decreasing subsequence of length 3 (i.e., indices i<j<k with s[i] > s[j] > s[k]). Because such a triple forces all three to be in different colors? In a proper coloring of inversion graph, such a triple forms a triangle, which requires 3 colors to properly color, so not possible with 2 colors. So indeed, the existence of a strictly decreasing subsequence of length 3 makes inversion graph not bipartite. However, could the inversion graph be non-bipartite even if there is no decreasing triple? Possibly with odd cycles of length >=5 that don't contain a triangle. But does such odd cycle exist without a decreasing triple? Let's try to construct an odd cycle in a permutation graph that is triangle-free. I recall that permutation graphs are perfect, which implies that if they contain an odd cycle, they contain an odd hole (induced cycle of odd length ≥5). Perfect graphs are those with no odd holes or odd antiholes. Actually, perfect graphs are those where the chromatic number equals the size of the largest clique for every induced subgraph. For perfect graphs, bipartiteness is equivalent to being triangle-free? Not exactly. For perfect graphs, being bipartite means it has no odd cycles, but odd cycles could exist without triangles. However, perfect graphs are not necessarily chordal; they can have holes. But are permutation graphs known to be "comparability graphs"? I think permutation graphs are a subclass of comparability graphs? Actually, permutation graphs are both comparability and co-comparability. Comparability graphs are perfect. But can comparability graphs have odd holes? Yes, for example, a cycle of length 5 is a comparability graph? Let's check: C5 is not a comparability graph because comparability graphs are transitively orientable; C5 is not a comparability graph? I recall that comparability graphs are those that can be oriented transitively. C5 is not a comparability graph; it's not a comparability graph because it's not perfect? Actually, C5 is perfect? C5 has chromatic number 3, clique number 2, so it's not perfect? Wait, perfect graph: For every induced subgraph, chromatic number equals clique number. C5 has chromatic number 3, clique number 2, so it's not perfect. So perfect graphs exclude odd holes (odd cycles of length >=5). Since permutation graphs are perfect, they cannot contain an induced odd cycle of length >=5. And any odd cycle in a perfect graph must contain a triangle (since if it's not induced, there are chords that would create a triangle maybe). Actually, perfect graphs are exactly those with no odd holes or odd antiholes (Strong Perfect Graph Theorem). Since permutation graphs are perfect, they contain no odd holes. Therefore, if there is an odd cycle, it must have a chord, which would create a triangle. So any odd cycle implies existence of a triangle. So for permutation graphs, bipartiteness is equivalent to being triangle-free. Because if there is a triangle, it's not bipartite; conversely, if there is an odd cycle, by perfection there must be a triangle (since odd hole forbidden). So bipartiteness = no triangle. And a triangle corresponds exactly to a decreasing triple (i<j<k with s[i] > s[j] > s[k])? Let's verify: A triangle means three vertices pairwise adjacent. In inversion graph, vertices i<j<k. Edges exist if s[i] > s[j], s[i] > s[k], s[j] > s[k]. So indeed s[i] > s[j] > s[k] (strictly). So triangle corresponds to a strictly decreasing subsequence of length 3. Thus, for strings with distinct letters? But we can have equal letters; if there are equalities, some edges might be missing. For triangle we need all three strict >. So a triangle requires a strictly decreasing triple. Therefore, the inversion graph being non-bipartite is equivalent to the existence of a strictly decreasing subsequence of length 3. But is that always true even with repeats? Let's check: Could we have an odd cycle without a triangle? Suppose we have a cycle of length 5 but with chords? But if there's any chord, it might create a triangle. However, could there be a 5-cycle that is not induced but still odd cycle without a triangle? For graph to be non-bipartite, it's enough to have any odd cycle, not necessarily induced. If the graph has an odd cycle, but no triangle, then the odd cycle must be an induced odd cycle (hole) because if it has chords, it might have a shorter odd cycle, possibly a triangle. However, it's possible to have an odd cycle of length 5 with a chord that creates a triangle? Actually, if there's a chord, that chord splits the cycle into a triangle plus a quadrilateral. If the chord connects vertices that are two apart, then you get a triangle and a 4-cycle. If the chord connects vertices three apart (in a 5-cycle, that's also two apart; in 5-cycle, any chord connects vertices at distance 2 (since distance 1 are edges already). So a chord would create a triangle and a quadrilateral. So if there is a chord, there is a triangle. So if the graph has an odd cycle but no triangle, that odd cycle must be induced (hole). But perfect graphs forbid odd holes, so permutation graphs cannot have induced odd cycles of length >=5. Therefore, any odd cycle implies a triangle. So indeed, for permutation graphs (including those derived from strings with possible repeats), the graph is bipartite iff it contains no triangle (i.e., no decreasing triple). However, careful: Our graph is not exactly a permutation graph if there are equal characters because edges only for strict >. If s[i] == s[j], no edge. So the graph is still a comparability graph of a partial order? It's a graph defined by inversions (strict >). This graph might not be perfect? But I think it's still a comparability graph of a poset defined by i<j and s[i] > s[j]? Actually, it's the incomparability graph? Might still be perfect. But regardless, even if there exists an odd cycle without a triangle, we can still detect bipartiteness via BFS. So we don't need to rely on equivalence. But it's interesting that for the given constraints, we can simply do BFS.
Nevertheless, the condition "no decreasing triple" might be simpler to check: if there exists i<j<k with s[i] > s[j] > s[k], then answer NO. But is that sufficient for YES? Not necessarily because there could be odd cycles without a decreasing triple if the graph is not perfect? But for strings with repeats, maybe it is sufficient? Let's test if a string can have an odd cycle without a decreasing triple. We can attempt to construct. Suppose we have indices: 1: c, 2: b, 3: a? That's decreasing triple -> triangle. So that's covered.
What about a 5-cycle: Need 5 indices such that the inversion edges form a 5-cycle but no three of them are pairwise connected (i.e., no triangle). Is that possible given the definition of edges based on >? Let's try to construct a set of indices with values such that we get a cycle but avoid three mutually inversions. Inversion edges are defined by > relationship and the ordering of indices. This graph is known as the "inversion graph" of a sequence (not necessarily a permutation). Its properties? It might still be a comparability graph of a poset? I'm not entirely sure. But we can try to manually search for a counterexample where no decreasing triple exists but the graph is not bipartite.
We need an odd cycle. The simplest odd cycle is a triangle. So if we avoid triangles, we need a cycle of length 5 or more. Can we have a cycle of length 5? Let's try to construct five indices a<b<c<d<e with certain values such that edges exist only for certain pairs and they form a 5-cycle without chords (induced 5-cycle). Suppose we want edges: (a,b), (b,c), (c,d), (d,e), (e,a). Also maybe other edges? To be induced, there should be no other edges among these five. But note that if a<b and there is an edge (a,b), that means s[a] > s[b]. For (b,c): s[b] > s[c]. For (c,d): s[c] > s[d]. For (d,e): s[d] > s[e]. For (e,a): we need an edge between e and a. Since e > a (as indices: e is largest), we need s[e] > s[a] to have an edge (because for i<j, edge if s[i] > s[j]; if i>j, we consider the order: edge exists if the smaller index has larger value than the larger index. For (e,a), since a<e, the pair is (a,e). Actually edge (e,a) is same as (a,e) because graph undirected; we need an edge between a and e. For indices a and e with a<e, edge if s[a] > s[e]. So for edge between a and e, we need s[a] > s[e]. But we already have from previous: s[a] > s[b] > s[c] > s[d] > s[e]. That implies s[a] > s[e] automatically. So edge (a,e) would exist. But that gives also edges between a and c? Because s[a] > s[b] > s[c] implies s[a] > s[c], and a<c, so edge (a,c) exists. That would be a chord, creating a triangle. So this 5-cycle is not chordless; it has many chords. To avoid chords, we need to break the transitivity: we cannot have a chain of strict inequalities that imply other edges. Specifically, if we have a>b and b>c, then a>c, so edge (a,c) exists (since a<c? Actually indices: a<b<c? Let's assume indices are in increasing order a<b<c. If s[a] > s[b] and s[b] > s[c], then s[a] > s[c] indeed. And since a<c, edge (a,c) exists. So any three indices in order with decreasing values will produce triangle edges? Not exactly: For a<b<c, if s[a] > s[b] and s[b] > s[c], we have s[a] > s[c], so edge (a,c). So those three vertices form a triangle (a,b), (b,c), (a,c). So any strictly decreasing subsequence of length 3 gives a triangle. To avoid triangles, we must avoid any three indices i<j<k with s[i] > s[j] > s[k]. However, we could have i<j<k with s[i] > s[j] but s[j] not > s[k] maybe s[j] < s[k] but still s[i] > s[k]? Let's see: If s[i] > s[j] and s[i] > s[k], and s[j] < s[k], then edges: (i,j) and (i,k) exist, but (j,k) does not because s[j] < s[k]. That's a V shape, not triangle. So we could have a graph that is triangle-free if we avoid decreasing triples. So could we have an odd cycle using such V shapes? Possibly.
Let's try to construct a 5-cycle without triangles. We need five vertices with indices increasing order (we can order them by index). Let the indices be 1,2,3,4,5 for simplicity. We need edges among them to form a 5-cycle. Since indices are increasing, edges are defined based on values. Let's denote values v1...v5.
Edges exist between i v_j.
We want the cycle to be, say, 1-2-3-4-5-1 (edges: (1,2), (2,3), (3,4), (4,5), (5,1)). Also, we need to avoid other edges to prevent triangles (though even if extra edges exist, they might create triangles; but we need to ensure no triangle exists, because if any triangle exists, that's a decreasing triple, which we are trying to avoid. So we must avoid any triple i<j v_j > v_k. But we could still have other edges as long as they don't create a triangle. However, if we have edges (1,2) and (2,3), to avoid triangle we must ensure that (1,3) is not an edge, which means v1 <= v3 (since 1<3). So condition: v1 > v2 (for edge 1-2), v2 > v3 (for edge 2-3), but v1 <= v3. That is possible: e.g., v1=5, v2=3, v3=4? Wait v2 > v3 requires v2 > v3, so v2=4, v3=3? Actually we need v2 > v3, so v2 > v3; and we need v1 <= v3 to avoid edge (1,3). With v1 > v2 (since edge 1-2), we have v1 > v2 > v3? That would imply v1 > v3, giving edge (1,3). So to avoid (1,3), we cannot have both v1 > v2 and v2 > v3 simultaneously because that would imply v1 > v3 (transitivity of >). Actually transitivity holds: if v1 > v2 and v2 > v3, then v1 > v3. So edge (1,3) would exist. So to avoid edge (1,3), we must break either v1 > v2 or v2 > v3. But we need those edges for the cycle. So the cycle pattern 1-2-3 with edges (1,2) and (2,3) forces (1,3) edge if indices are 1<2<3. Because if 1<2<3, then (1,2) edge implies v1 > v2; (2,3) edge implies v2 > v3; thus v1 > v3, so (1,3) is an edge. Therefore any three consecutive vertices in the cycle with indices in increasing order will force a triangle. To avoid that, the cycle cannot have all indices in increasing order along the cycle; the indices must not be monotonic. But the graph is defined on a linear order of indices. The vertices are ordered by index. The edges depend on the values.
In an undirected graph on a linearly ordered set, if we have a cycle, it may not be that the cycle respects the index order. However, the vertices are labeled by indices, but the cycle is just a set of edges. For three vertices i<j<k, if there are edges (i,j) and (j,k), then automatically there is edge (i,k) because of transitivity of >? Let's check: Edge (i,j) exists => v_i > v_j. Edge (j,k) exists => v_j > v_k. Then v_i > v_k, and since i<k, edge (i,k) exists. So indeed, if we have three vertices with indices i<j<k and both edges (i,j) and (j,k) present, then (i,k) must also be present. This is a property of the inversion graph: It is a comparability graph of a total order? Actually, the condition is: For any i<j<k, if s[i] > s[j] and s[j] > s[k] then s[i] > s[k] (transitivity). So the existence of edges (i,j) and (j,k) forces edge (i,k). Therefore, the graph is "triangulated" in the sense that any such configuration creates a triangle. This means that the graph cannot contain an induced path of length 2 on three vertices with indices in increasing order without also having the chord. However, if the three vertices are not in order i<j<k? But vertices are labeled by indices; we can consider any triple, we can sort them. Suppose we have vertices a, b, c with indices such that after sorting, they are i<j<k. If the graph contains edges (i,j) and (j,k), then it also contains (i,k). So any path of length 2 where the middle vertex is the middle index forces a triangle. For a cycle of odd length, you could have a path where the middle vertex is not the middle index? For three vertices on the cycle, consider edges (u,v) and (v,w). When we sort u,v,w by index, v might not be the middle; it could be the smallest or largest. Let's examine: Suppose we have three vertices with indices i < j < k. The edges are (i,k) and (k,j)? But if we consider edges (i,k) and (k,j), for these to exist, we need v_i > v_k (since i<k) and for edge (k,j), note that k > j? Actually if k>j, then edge (j,k) would be considered with the smaller index j. Edge (k,j) is same as (j,k). So condition: v_j > v_k? Wait, for edge between j and k (j<k), we need v_j > v_k. But we have edge (k,j) meaning we need that. So if we have edges (i,k) and (j,k) (since (k,j) is same as (j,k)), then that means v_i > v_k and v_j > v_k. That does not force an edge between i and j unless we have order i v_j? Not necessarily. So it's possible to have two edges sharing the largest index k without forcing an edge between i and j. So consider a path i - k - j, where k is the largest index and edges are (i,k) and (j,k). For these edges to exist, we need v_i > v_k and v_j > v_k. Since i<k and j<k, both conditions are v_i > v_k, v_j > v_k. This does not imply anything about v_i vs v_j; they could be in any order. So we could have edges (i,k) and (j,k) without edge (i,j). That's a V shape with apex at largest index. Similarly, if k is the smallest index, edges (k,i) and (k,j) would require v_k > v_i and v_k > v_j. That also doesn't force edge between i and j.
Thus, it's possible to have a path of length 2 that doesn't force a triangle, if the shared vertex is an extreme (min or max) among the three indices. So the forced triangle only occurs when the shared vertex is the middle index (by value of index) and both edges are present with the two extremes. In a cycle, the pattern of indices matters.
Now, can we construct an odd cycle without a triangle? Possibly using such configurations where the edges avoid the "middle index as shared vertex" pattern. Let's try to construct a 5-cycle with indices 1,2,3,4,5 and values such that the edges are, for example: (1,2), (2,5), (5,3), (3,4), (4,1). Let's assign indices: we need to check if this yields any triangle. We'll need to satisfy the conditions for each edge based on index order.
List edges with index order (smaller, larger):
(1,2): edge if v1 > v2.
(2,5): 2<5, need v2 > v5.
(5,3): but 3<5, so edge (3,5): need v3 > v5.
(3,4): 3<4, need v3 > v4.
(4,1): 1<4, need v1 > v4.
Now, check for triangles: any three vertices that are all mutually connected? Let's see if triangle 1-2-5: edges (1,2) and (2,5) present; to be triangle need (1,5). (1,5): 1<5, need v1 > v5. Do we have that? Not necessarily from given. Could we assign values to avoid (1,5)? We can try to assign values satisfying all edge conditions but not creating extra edges. The conditions:
v1 > v2
v2 > v5
v3 > v5
v3 > v4
v1 > v4
We also need to avoid certain extra edges to prevent triangle? But triangle would be formed if an extra edge exists. The graph will include all edges that satisfy condition, not only the ones we list. So we must ensure that no unintended edges appear (i.e., for any pair not listed, the condition for edge should be false). So we must assign values such that for every pair (i,j) not among our desired edges, we have v_i <= v_j when i<j (or i>j but we consider the smaller index). So the assignment must be a total order of values that satisfies strict > exactly for the listed pairs and <= for all others.
Is that possible? We need to find values v1..v5 (real numbers or integers) such that for each desired edge (i,j) with i<j, v_i > v_j, and for each non-edge (i,j) with i<j, v_i <= v_j.
This is a classic problem of realizing a tournament? Actually it's about constructing a linear ordering of values consistent with constraints. This is exactly a partial order: we require v_i > v_j for edges (i<j), and v_i <= v_j for non-edges (i<j). But <= is not a strict order; we can have equality. However, equalities could create edges? No, edge requires >, so equality is safe (no edge). So we need to assign values such that v_i >= v_j for non-edges (i<j) with possibility of equality, and v_i > v_j for edges. This is like finding a realizer of a poset extended with ties.
We can attempt to assign numeric values respecting these constraints. Since n is small, we could attempt to see if such assignment exists.
Let's try to solve the constraints for the 5-cycle above.
We have inequalities:
v1 > v2
v2 > v5
v3 > v5
v3 > v4
v1 > v4
We also need for non-edges:
For pair (1,3): 1<3, we need v1 <= v3 (to avoid edge).
(1,5): 1<5, need v1 <= v5? Actually to avoid edge (1,5), we need not v1 > v5, i.e., v1 <= v5.
(2,3): 2<3, need v2 <= v3.
(2,4): 2<4, need v2 <= v4.
(3,1): already covered by (1,3) since order 1<3, we used v1 <= v3.
(3,2): 2<3 we already have v2 <= v3 (non-edge).
(3,5): we have edge (3,5) actually? Wait, edge (5,3) is same as (3,5) and we have that as an edge, so v3 > v5 is required (we have that). So that's okay.
(4,2): 2<4, need v2 <= v4.
(4,5): 4<5, need v4 <= v5 (to avoid edge (4,5)).
(5,1): 1<5, need v1 <= v5 (already).
(5,2): 2<5, edge exists (2,5) so v2 > v5.
(5,4): 4<5, need v4 <= v5 (to avoid edge (4,5)).
Also (5,?) etc.
Now, combine constraints:
From 1: v1 > v2.
From 2: v2 > v5.
From 5: v1 > v4.
From 3: v3 > v5.
From 4: v3 > v4.
Now, let's see if these can be satisfied simultaneously.
From v2 > v5 and v2 <= v4 (non-edge (2,4)) => v4 >= v2 > v5? Actually v2 <= v4 means v4 >= v2. Since v2 > v5, we get v4 > v5 (strict because v2 > v5 and v4 >= v2 implies v4 > v5). But we also have non-edge (4,5) requires v4 <= v5 to avoid edge. Contradiction: v4 > v5 and v4 <= v5 cannot both hold. So this assignment is impossible. So that specific 5-cycle is impossible because it forces contradictory constraints. This suggests that the inversion graph might be "transitively orientable" and thus perfect, and may not allow odd cycles without triangles. Indeed, I suspect that the inversion graph of any sequence (with possible equalities) is a comparability graph (maybe of a partial order where we define i < j if i<j and s[i] > s[j]?), and comparability graphs are perfect and contain no odd holes. So they are bipartite iff triangle-free. But we don't need to rely on that; we can just run bipartite check.
Thus, the solution is correct.
Now, implement and submit.
Given the problem statement: "This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version." So this approach is valid for easy version.
One more thing: Input constraints: n ≤ 200. So we can safely store adjacency matrix or list.
Now, finalize code.
But wait, there might be a nuance: The inversion graph includes edges for all i<j with s[i] > s[j]. However, what about if s[i] == s[j]? No edge. That's fine.
Now, test with a case where there are many equal letters and a decreasing triple with equal? For a decreasing triple we need strict >. So if s[i] > s[j] and s[j] > s[k], triangle exists. If s[i] > s[j] and s[j] == s[k] and s[i] > s[k], then edges: (i,j) and (i,k) exist, but (j,k) does not because equal. So triangle may not be present. Could such a configuration cause a longer odd cycle? Possibly. But as argued, likely still bipartite if no strict decreasing triple. However, we should test a specific example to be safe.
Let's attempt to construct a string without any strictly decreasing triple (i.e., no i<j<k with s[i] > s[j] > s[k]) but still the inversion graph is non-bipartite. Is that possible? We'll try to find a counterexample by brute reasoning or small n.
We can attempt to systematically search for small n with all possible strings of length up to maybe 5 to see if any non-bipartite inversion graph lacks a triangle. Since n ≤ 200, we can trust BFS; we don't need to simplify.
But for correctness, we can reason: The inversion graph is a "comparability graph" of a poset defined as: define a relation R: i precedes j if i<j and s[i] > s[j]. This relation is a partial order? Check: It is irreflexive (i not related to itself). Antisymmetric? If i<j and s[i] > s[j], then not j<i and s[j] > s[i] because indices order opposite. Transitivity? If i<j, s[i] > s[j] and j<k, s[j] > s[k] => then i<k and s[i] > s[k] (since > is transitive). So R is transitive. So it's a strict partial order (irreflexive, transitive, antisymmetric). The comparability graph of a poset is the undirected graph where vertices are elements and edges connect comparable pairs (i.e., either i<j in poset or j<i). In our case, the comparability graph of this poset would have an edge between i and j if either i<j and s[i] > s[j] OR j<i and s[j] > s[i]; which is exactly our inversion graph (since if i<j and s[i] > s[j] -> edge; if j<i and s[j] > s[i] -> edge, which is the same condition). So indeed, the inversion graph is the comparability graph of a partial order. Comparability graphs are known to be perfect (since they are a subclass of perfect graphs). Therefore, they contain no odd holes (induced odd cycles of length ≥5). Moreover, if the graph has an odd cycle, it must contain a triangle (since any odd cycle would have a chord that creates a triangle, because comparability graphs are not necessarily chordal but they are perfect, but perfect graphs can have odd cycles? Wait, perfect graphs can have odd cycles that are not induced? But if it's not induced, it has chords; those chords may create a triangle. But could there be an odd cycle that is not induced yet still no triangle? Suppose we have a 5-cycle with a chord connecting vertices that are distance 2; that chord creates a triangle (since chord plus two edges of the cycle form a triangle). For a 5-cycle, any chord will create at least one triangle because the chord splits the cycle into a triangle and a quadrilateral. So if there's any chord, there is a triangle. So if the graph has an odd cycle, either it is induced (hole) or it has chords leading to triangle. Since comparability graphs cannot contain odd holes (as they are perfect), any odd cycle must have chords, and thus must contain a triangle. Therefore, bipartiteness is equivalent to triangle-free. So condition reduces to no decreasing triple (strict). However, this reasoning relies on the graph being a comparability graph of a poset, which it is. And comparability graphs are perfect? I think comparability graphs are perfect; it's a known theorem. Yes, comparability graphs are perfect. Also, they are a subclass of perfect graphs and they contain no odd holes? Actually, perfect graphs include bipartite graphs, chordal, etc., and comparability graphs are perfect. The Strong Perfect Graph Theorem says a graph is perfect iff it contains no odd hole or odd antihole as an induced subgraph. Comparability graphs are perfect, so they have no odd holes. Therefore, if there is an odd cycle, it must have a chord, leading to a triangle. So indeed, the graph is bipartite iff it contains no triangle. So we could simplify by just checking for decreasing triple. But that's not necessary for n=200; we can just do BFS.
Nevertheless, I'll keep BFS.
Now, ensure we handle the case when the graph is disconnected properly.
Edge case: n=0? Not possible, n≥1.
Now, about output: The coloring string should be of length n, consisting of characters '0' and '1'. Our color list contains integers 0 or 1; we convert to string.
Testing with sample 1: Let's compute quickly to see if our BFS yields a valid coloring. But it's fine.
Now, implement and test with sample inputs.
I'll write the code.
Potential optimization: Instead of building adjacency list for all edges, we could directly BFS using the condition? But it's fine.
Now, consider memory: adjacency list of size up to 200, each with up to 199 neighbors, okay.
Now, write final solution.
But wait, there might be a subtlety: The graph is built based on s[i] > s[j]. However, we should consider that the string s consists of lowercase Latin letters. So we can compare directly.
Now, final code.
But before finalizing, let's double-check sample 1: s = "abacbecfd". I'll compute the inversion graph quickly to see if BFS yields a coloring maybe similar to output? Not needed.
Now, I'll implement solve().
One more nuance: The problem might expect that we output the coloring as a string of '0's and '1's, but our assignment uses 0 and 1. That's fine.
Now, I'll produce the final answer.
But wait: There is a possibility that the graph is bipartite but the coloring we output might not actually allow sorting? We argued that if each color class is non-decreasing, then sorting is possible. However, we should confirm that our coloring indeed makes each color class non-decreasing. Because our condition ensures that for any inversion edge (i<j, s[i] > s[j]), colors differ. That ensures that within each color class, there is no inversion, i.e., for any i<j same color, s[i] <= s[j]. So yes, each color class is non-decreasing.
Thus, correct.
Now, perhaps there is another necessary condition: The two subsequences must be mergeable into sorted order. But as argued, two sorted sequences can always be merged into a sorted sequence. So that's fine.
Thus, solution done.
Now, I'll produce final answer. ```python
import sys
from collections import deque
def solve() -> None:
it = iter(sys.stdin.read().split())
n = int(next(it))
s = next(it).strip()
# Build inversion graph: edge between i<j if s[i] > s[j]
adj = [[] for _ in range(n)]
for i in range(n):
for j in range(i + 1, n):
if s[i] > s[j]:
adj[i].append(j)
adj[j].append(i)
code
color = [-1] * n
for i in range(n):
if color[i] == -1:
color[i] = 0
q = deque([i])
while q:
u = q.popleft()
for v in adj[u]:
if color[v] == -1:
color[v] = 1 - color[u]
q.append(v)
elif color[v] == color[u]:
print("NO")
return
print("YES")
print(''.join(str(c) for c in color))