Well, the series which Stepan watched for a very long time, ended. In total, the series had n episodes. For each of them, Stepan remembers either that he definitely has watched it, or that he definitely hasn't watched it, or he is unsure, has he watched this episode or not.
Stepan's dissatisfaction is the maximum number of consecutive series that Stepan did not watch.
Your task is to determine according to Stepan's memories if his dissatisfaction could be exactly equal to k.
Input
The first line contains two integers n and k (1 ≤ n ≤ 100, 0 ≤ k ≤ n) — the number of episodes in the series and the dissatisfaction which should be checked.
The second line contains the sequence which consists of n symbols "Y", "N" and "?". If the i-th symbol equals "Y", Stepan remembers that he has watched the episode number i. If the i-th symbol equals "N", Stepan remembers that he hasn't watched the epizode number i. If the i-th symbol equals "?", Stepan doesn't exactly remember if he has watched the episode number i or not.
Output
If Stepan's dissatisfaction can be exactly equal to k, then print "YES" (without qoutes). Otherwise print "NO" (without qoutes).
Examples
Input
5 2
NYNNY
Output
YES
Input
6 1
????NN
Output
NO
Note
In the first test Stepan remembers about all the episodes whether he has watched them or not. His dissatisfaction is 2, because he hasn't watch two episodes in a row — the episode number 3 and the episode number 4. The answer is "YES", because k = 2.
In the second test k = 1, Stepan's dissatisfaction is greater than or equal to 2 (because he remembers that he hasn't watch at least two episodes in a row — number 5 and number 6), even if he has watched the episodes from the first to the fourth, inclusive.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Well, the series which Stepan watched for a very long… · The Chat Archive
We are given: n up to 100. We have a string of length n with characters 'Y', 'N', '?'. Y = definitely watched, N = definitely not watched, ? = unknown (could be either Y or N). The dissatisfaction is defined as the maximum number of consecutive episodes that Stepan did not watch (i.e., the length of the longest contiguous segment of N's in the actual watched/unwatched pattern). We need to determine if it is possible that the dissatisfaction exactly equals k, given that we can assign each '?' to either Y or N arbitrarily. So we need to check existence of an assignment to '?' such that the longest consecutive N substring length is exactly k.
This is a classic feasibility problem: we have some fixed Y's (cannot be N) and fixed N's (cannot be Y). For each ?, we choose Y or N. Then compute longest run of N's. Need exactly k.
Constraints small: n <= 100, we can do exhaustive search over assignments? 2^(#?) could be large if many ?. But n=100, worst-case 100 ?, 2^100 too big. However, we can treat as constraint satisfaction with DP: we need to check if there exists an assignment to ? such that max run length = k.
We can think in terms of properties: The longest run of N's must be exactly k. That means:
There exists at least one run of N's of length k (i.e., some contiguous block of k positions that are all N, and they are either from fixed N or we can set ? to N accordingly, and also they are not extended to length >k due to adjacent positions). Actually to have max exactly k, we need that the maximum length of consecutive N's is exactly k. So we need that:
There is no run longer than k (i.e., no contiguous block of N's of length > k).
There is at least one run of length exactly k.
Given that we can set ? arbitrarily, we need to see if we can achieve both conditions simultaneously.
Thus we need to check if there exists an assignment to ? such that:
All constraints from fixed Y: those positions cannot be N.
The longest consecutive N length is exactly k.
Alternatively, we can think about necessary and sufficient conditions. Because n is small, we can try to formulate as a kind of 2SAT or DP over positions tracking current run length and maximum so far, with state (position, cur_run, max_run) but we need to enforce final max_run = k. DP dimensions: n <=100, max run can be up to n, so DP O(n * n * n) = 1e6, fine.
We'll treat each position i (1-indexed) and decide assignment for ?. But for fixed positions, decision forced. So we can do DP: let dp[i][c][m] = boolean whether it's possible to process first i positions, such that the current run length of consecutive N's ending at i is c (if i is N) or 0 if i is Y, and the maximum run length seen so far is m. However, both c and m are bounded by n. At step i, we consider possible assignments for position i if it's ?, else forced. For each possible choice (Y or N), we compute new c, new m accordingly, and update dp. At the end, we check if there exists state where max_run = k.
But careful: c is the length of consecutive N's up to i; if we set position i to Y, then c = 0; if N, then c = previous c + 1, and m = max(old m, c). Initial state: i=0, c=0, m=0 (no positions processed). But we might have initial constraints? That's fine.
We need dp[0][0][0] = true. Then for i from 1 to n, we iterate over previous dp[i-1][c_prev][m_prev] that are true. For each, we consider allowed symbols for position i. For fixed Y: only Y allowed; for fixed N: only N allowed; for ?: both allowed.
For Y: new_c = 0, new_m = m_prev (since no N run continues). For N: new_c = c_prev + 1, new_m = max(m_prev, new_c).
Transition to dp[i][new_c][new_m] = true.
At end, we check if any dp[n][c][k] true for any c (c can be any, but note c is the current run length, but max is k, so it's okay). But we need exactly k, not more than k. However, we also need to ensure that there is no run longer than k; DP's m is the maximum run length so far, so if we ever produce m > k, that state would be pruned (since we only care about final m=k, we could still allow intermediate m>k as long as final is k? No, because if at any point we had a run longer than k, then max would be >k, and that would be m > k. In DP we are tracking maximum so far, so if we allow transitions that lead to m > k, they would result in final m > k. Since we want final max = k, we can restrict DP to only keep states with m <= k, and at the end check m = k. However, careful: it's possible that we have a run longer than k at some point but later we could "reduce" the maximum? No, maximum is the maximum over all runs seen, so once we have a run length > k, the overall maximum becomes > k and cannot later become <=k. So to achieve final max exactly k, we must never have any run longer than k. So we can safely restrict DP to m <= k. Moreover, we need at least one run of length exactly k. So at the end we need m = k (and also there exists a run of length k, captured by m). But what about the possibility that the max is exactly k but we never had a run of length k because maybe we had runs of length less than k only? Then m would be less than k. So m = k ensures there exists a run of length at least k; but since we never allowed runs longer than k (by restricting m <= k), the run that gave m=k must be exactly length k (if it were longer than k, it would have been > k and we wouldn't have allowed). So it's exactly k.
Thus DP: state (i, cur_len, max_sofar) with max_sofar <= k. At the end, if there is any state with max_sofar = k, answer YES, else NO.
Complexity: O(n * n * k) but we can set dimension for cur_len up to n, and max_sofar up to k. Actually cur_len can exceed k? In transitions we might have cur_len > k, but if cur_len > k then max_sofar would become > k, which we are not going to keep. So we can also cap cur_len at k (since if it ever reaches k+1, we would have max_sofar at least k+1 and we can discard that state). However, careful: It is possible that cur_len > k but max_sofar hasn't been updated? Actually when we extend N run, new_c = c_prev + 1, and then new_m = max(m_prev, new_c). So new_m will be at least new_c, so if new_c > k, new_m > k, so we discard. So we can restrict cur_len to <= k as well because any state with cur_len > k leads to m > k, not allowed. So we can keep cur_len from 0 to k. So DP dimensions: i up to 100, cur_len up to 100 but we can bound to k (<=100). So at worst 100100100=1e6, fine.
Thus we can implement DP with a 3D boolean array or 2D for each i (or use set of (cur_len, max) pairs). Since n is small, easy.
Edge cases: k=0. Dissatisfaction is max consecutive N's. So we need that there are no N's at all (i.e., all episodes are watched) because the longest run of N's must be 0. But is that correct? If there is any N, then max consecutive N's >=1 >0. So must be no N. Therefore, we need to check if we can assign all ? to Y such that there are no N's. Also note that if there is any fixed N, then impossible. So DP: with k=0, we only allow states with max_sofar =0, i.e., never any N. So we only allow transitions: if position forced N then invalid; if position is ? we must choose Y. So DP will naturally handle.
Now check examples.
Example 1: n=5, k=2, s="NYNNY". All fixed: N Y N N Y. DP with k=2: Let's simulate: position1 N => cur=1, max=1. pos2 Y => cur=0, max=1. pos3 N => cur=1, max=1? Actually new_c=1, max stays 1 => max=1. pos4 N => cur=2, max becomes max(1,2)=2. pos5 Y => cur=0, max=2. End with max=2 => YES. That's correct.
Example2: n=6, k=1, s="????NN". We need max exactly 1. But we have two fixed N at positions 5,6: they are consecutive, so if we set positions 1-4 to something, we will have at least a run of N length 2 (positions 5-6). So we cannot avoid a run of length >=2. So max will be at least 2 >1. So impossible => NO. DP: k=1, when processing position5 N: from previous, cur_len maybe something. But adding N at 5: new_c = c_prev+1. To keep max<=1, we must have c_prev=0 and then new_c=1, max stays <=1. Then at position6 N: from previous, cur_len=1, then new_c=2, new_m = max(1,2)=2 >1, state discarded. So no final state with max=1. So NO.
So DP works.
But is DP necessary? There might be simpler logical conditions: For the longest run to be exactly k, we need to be able to avoid runs longer than k, and to have at least one run of length k. However, due to fixed Y's and N's, the problem might be solved by checking constraints. But DP is straightforward and efficient.
Let's implement DP: Use a 2D boolean array for current step: dp[cur_len][max_sofar] for positions up to i. Since n <=100, k <= n, we can have dimensions (k+1) x (k+1) (since max cannot exceed k). But careful: max_sofar can be up to k, cur_len up to k. So we can have dp[cur][max] boolean. At step i, we compute new_dp. Initially dp[0][0]=true. For each position i, get char ch. For each state (c, m) where dp[c][m] true, we consider allowed values:
If ch == 'Y' or we choose Y: new_c=0, new_m = m. new_dp[0][new_m] = true.
If ch == 'N' or we choose N: new_c = c+1, new_m = max(m, c+1). If new_c <= k and new_m <= k, then new_dp[new_c][new_m] = true. (If new_c > k or new_m > k, discard)
But note: If ch == '?', we consider both.
If ch == 'Y', only Y.
If ch == 'N', only N.
After processing all positions, check if any state (c, m) with m == k exists in dp. If yes, YES else NO.
Time: O(n * (k+1)^2 * transitions(2) ) ~ O(n * k^2). For n=100, k=100, it's about 1e6, fine.
But we must be careful: The max_sofar may be less than k if we never got a run of length k. So final check.
Edge case: k might be larger than n? Input says 0 ≤ k ≤ n, so within.
One more test: n=1, k=0, s="?" => possible to set Y, so max=0 => YES. DP: initial dp[0][0]=true. ch='?': from (0,0) Y->(0,0); N-> new_c=1 new_m=1. Since k=0, N transition yields new_c=1 >0 so discard (since we only keep states with new_c<=k? Actually we restrict new_c <= k and new_m <= k. k=0, so new_c=1 >0 discards. So only Y transition remains. So final dp[0][0] true => YES. Good.
n=1, k=1, s="?" => can set N => YES. DP: k=1, from (0,0) Y->(0,0); N->(1,1). Keep both. At end check m=1 => true => YES.
n=1, k=0, s="N" => impossible because forced N gives max>=1. DP: forced N, from (0,0) N -> new_c=1, new_m=1, but k=0, so we require new_c<=0? Actually we check if new_c <= k (0) and new_m <= k (0). 1 > 0, discard. So no state after processing, answer NO. Good.
Thus DP works.
But consider scenario where we might need to allow cur_len > k while max stays <= k? That's impossible because max will be at least cur_len. So discarding is safe.
Now, but is there any nuance? The dissatisfaction is defined as the maximum number of consecutive series that Stepan did not watch. That is longest run of N's. So DP is correct.
But is there any other twist? We must ensure that we are not forced to have a run longer than k because of fixed N's; DP captures that.
Thus answer is to implement DP.
But I realize we could also use a simpler approach: For each possible assignment, longest run of N's is determined by the positions of Y. But with '?' we need to decide which positions become Y and which become N. The condition "max run exactly k" is equivalent to: there exists an assignment such that:
There is no run of N's longer than k.
There is at least one run of N's of length k.
Given fixed Y and N, we can try to see if we can set ? to break long runs and to create a run of length exactly k. That might be solvable by checking constraints, but DP is safe.
Given small n, DP is easy.
We need to produce "YES" or "NO".
Now let's think about potential pitfalls: The DP we described uses a 2D boolean for each step. But note that m (max_sofar) can be less than or equal to k, but we need to track it precisely to ensure final value exactly k. However, we might be able to compress by noting that we don't need to know both c and m separately? For instance, we could do DP that tracks only current run length and whether we have already achieved a run of length k, but also ensure we never exceed k. But we need exact maximum k, meaning we must not exceed k. So we need to know if we have already seen a run of length exactly k? Actually we need that the maximum is exactly k, so if we ever have a run of length less than k, but later we get exactly k, that's okay. But we also need to avoid any run >k. So we need to enforce that the run length never exceeds k. So we can treat it as: We need to assign ? such that every block of consecutive N's has length ≤ k, and at least one block has length exactly k. This is like a "constraint satisfaction" where Y's act as separators. This is reminiscent of interval covering. But DP is simpler.
We'll implement.
But note: The DP's state includes max_sofar, which could be up to k. However, k can be up to 100, so maximum number of states per step is (k+1)*(k+1) ≈ 10k^2 = 10,000. For n=100, that's 1e6, fine.
We can also use bitset for speed but not needed.
Implementation details:
We'll have dp as a 2D boolean array of size (k+1) x (k+1) (cur_len, max). At each step, we initialize new_dp to false. Then iterate over cur_len from 0 to k, max from 0 to k, if dp[cur][max] then:
Let ch = s[i] (0-indexed string). Options:
if ch == 'Y' or ch == '?':
new_cur = 0
new_max = max
new_dp[new_cur][new_max] = true
if ch == 'N' or ch == '?':
new_cur = cur + 1
new_max = max(max, new_cur)
if new_cur <= k and new_max <= k:
new_dp[new_cur][new_max] = true
Note: For ch == '?' we consider both; for ch == 'Y' only first; for ch == 'N' only second.
But note: For ch == 'Y', we should not allow the N transition. For ch == 'N', we should not allow the Y transition.
Now, after all positions, we check if any state (c, m) with m == k is true.
But is there any scenario where we could have m == k but the actual maximum is >k? Since we've constrained m <= k, it's impossible.
Thus answer.
But wait: What about the situation where we have a run of exactly k, but that run could be extended if we choose adjacent ? as N, but we don't. Since we are controlling the assignment, it's okay.
One more nuance: The DP doesn't consider the possibility that we might need to set some ? to Y to break a run that would otherwise become too long due to fixed N's. That's handled because we can choose Y at those positions to reset cur_len to 0.
Now, test some more scenarios to be sure:
Case: n=5, k=2, s="?N??Y". Let's see manually: Fixed N at pos2, Y at pos5. Need max exactly 2. Can we achieve? Possibly: set pos1=Y, pos3=N, pos4=N, gives pattern Y N N N Y? Wait pos2 N, pos3 N, pos4 N => run of 3 N's (positions 2-4) length 3 >2. Not good. Maybe set pos1=N, pos3=Y, pos4=N: pattern N N Y N Y -> runs: N N at 1-2 length2, then single N at4 length1 => max=2. That works. Also need to avoid longer runs. DP should find.
Let's simulate DP quickly: k=2. After pos1 '?' choose Y: cur=0 max=0; choose N: cur=1 max=1. After pos2 forced N: from state (0,0): N -> new_cur=1 max=1; from (1,1): N-> new_cur=2 max=2. So we have states: (1,1) and (2,2). After pos3 '?': from each state we can choose Y or N. Let's see if we can eventually get final max=2. DP will propagate.
So fine.
Now, consider edge case k=0. With DP as above, we only allow transitions that keep cur_len and max <=0. That means only Y allowed. So if any position is forced N or ? set to N, it would produce cur_len >0, which is not allowed. So final states will only exist if all positions can be Y. That's correct.
Now, consider k= n (max possible). Then we want a run of length n, meaning all episodes are unwatched (all N). But if there is any fixed Y, impossible. Also need no longer run than n, that's fine. So DP will allow cur_len up to n, but k = n so max <= n, cur <= n. So states allowed. At the end need max = n. So must have a run of length n, i.e., all N. That would require that at each step we choose N and never reset. But if any Y forced, we cannot. DP will handle.
Now, check potential bug: The DP resets cur_len to 0 on Y, but doesn't consider that if we have a run that ends not because of Y but because the string ends. That's fine because max is captured.
Now, we need to ensure that we treat initial state correctly: before any position, current run length 0, max 0.
Also, note that we are not using any memory of previous runs after a reset, so DP is correct.
Thus DP solution is straightforward.
But perhaps we can simplify further: The condition that maximum run length ≤ k is equivalent to that we can insert Y's (i.e., set some ? to Y) to break any runs longer than k. That's like: For any segment of consecutive positions where we cannot set Y (i.e., forced N and possibly ? that can be set to N), the maximum possible run of N's given that we can set ? to either Y or N but we want to avoid >k. Actually, to ensure max ≤ k, we need that there is no forced run of N's longer than k; and for runs that could be longer due to ? being set to N, we can choose to set some ? to Y to break them. However, this might require considering whether we can create a run of exactly k as well. But DP is easier.
Given the small constraints, DP is fine.
But we must be careful about the fact that we need exactly k, not at most k. So we need to be able to achieve at least one run of length k. So DP will check that.
Potential optimization: We can use BFS over states, but same.
I'll code:
Read n, k.
Read string s (maybe using input().strip()).
Initialize dp = [[False]*(k+1) for _ in range(k+1)] (cur_len as rows, max as cols). But careful: cur_len index from 0 to k, max from 0 to k. Set dp[0][0] = True.
Loop for each character ch in s:
new_dp = [[False]*(k+1) for _ in range(k+1)]
For cur in range(k+1):
for mx in range(k+1):
if not dp[cur][mx]: continue
# process Y option
if ch == 'Y' or ch == '?':
new_cur = 0
new_mx = mx
new_dp[new_cur][new_mx] = True
# process N option
if ch == 'N' or ch == '?':
new_cur = cur + 1
new_mx = mx if mx > new_cur else new_cur # max(mx, new_cur)
# but since we are iterating mx up to k, we can compute and check bounds
if new_cur <= k and new_mx <= k:
new_dp[new_cur][new_mx] = True
dp = new_dp
After loop, check if any cur, mx with mx == k has dp[cur][mx] true. If yes print "YES", else "NO".
This should work.
But we should consider that when we choose Y, the new_mx remains mx, which could be less than k. Good.
Now let's test with examples.
Example1: n=5,k=2,s="NYNNY".
Step1: ch='N', dp initially only (0,0). N option: new_cur=1, new_mx=1 <=2 => dp[1][1]=True. Y option not allowed because ch='N'. So after step1: dp[1][1]=True.
Step2: ch='Y', process (1,1):
Y option: new_cur=0, new_mx=1 => dp[0][1]=True.
N option not allowed.
So after step2: dp[0][1]=True.
Step3: ch='N', from (0,1):
Y? no (ch='N') so only N: new_cur=1, new_mx=max(1,1)=1 => dp[1][1] = True. So state (1,1).
Step4: ch='N', from (1,1):
N: new_cur=2, new_mx=max(1,2)=2 => dp[2][2]=True.
Step5: ch='Y', from (2,2):
Y: new_cur=0, new_mx=2 => dp[0][2]=True.
N not allowed.
End: dp[0][2] true => mx=2 => YES.
Matches.
Example2: n=6,k=1,s="????NN".
Initialize dp[0][0]=True.
Step1 '?': from (0,0):
Y: (0,0)
N: (1,1) (since 1<=1)
So dp after step1: (0,0) and (1,1) both true.
Step2 '?': iterate over both.
From (0,0):
Y: (0,0)
N: (1,1)
From (1,1):
Y: (0,1) because new_cur=0, mx remains 1 => (0,1)
N: new_cur=2 => but 2>1 so discard (since new_cur>k). So (0,0),(1,1),(0,1) maybe.
So after step2: states: (0,0), (1,1), (0,1).
Step3 '?': similar, consider states:
(0,0): Y-> (0,0); N-> (1,1)
(1,1): Y-> (0,1); N-> new_cur=2 >1 discard
(0,1): Y-> (0,1); N-> (1,1) (since new_cur=1, mx remains max(1,1)=1) So (1,1) allowed.
So after step3, states: (0,0), (1,1), (0,1) (maybe multiple copies).
Step4 '?': similar, likely we have (0,0),(0,1),(1,1) still.
Now step5 'N': fixed N.
We need to process each state.
From (0,0): N-> new_cur=1, new_mx=1 => (1,1) allowed.
From (0,1): N-> new_cur=1, new_mx= max(1,1)=1 => (1,1) allowed.
From (1,1): N-> new_cur=2 >1 discard. So after step5, states: only (1,1) (maybe from both). So dp after step5: (1,1) true.
Step6 'N': from (1,1): N-> new_cur=2 >1 discard. So no states remain. So dp empty, answer NO.
Matches.
So DP works.
Now consider potential other examples:
n=3, k=2, s="N?N". Can we get max exactly 2? Fixed N at 1 and 3, with ? at 2. If we set ? to Y: pattern N Y N => runs: length1 and length1 => max=1. If we set ? to N: pattern N N N => run length 3 => max=3 >2. So not exactly 2. So answer should be NO. DP: k=2. Step1: N => (1,1). Step2: '?': from (1,1): Y-> (0,1); N-> new_cur=2, new_mx=2 => (2,2) allowed. Step3: fixed N. Process states: from (0,1): N -> new_cur=1, new_mx=1 => (1,1). from (2,2): N -> new_cur=3 >2 discard. So final states: (1,1) only. mx=1, not 2. So NO. Good.
Another: n=3, k=2, s="? ? Y"? Actually let's try "?N?"? I want a case where we can achieve exactly k. For n=4, k=2, s="?NN?" maybe: positions: 1?,2N,3N,4?. Want max=2. Fixed N's already give run of length 2 (positions2-3). Need to avoid longer runs. Can we set position1 and 4 to Y? If pos1=Y, pos4=Y, then run exactly length2. So should be YES. DP: k=2. Step1: '?' from (0,0) => (0,0) and (1,1). Step2: N: from (0,0) -> (1,1); from (1,1) -> (2,2). So states (1,1) and (2,2). Step3: N: from (1,1) -> (2,2); from (2,2) -> (3,3) discard. So states (2,2). Step4: '?': from (2,2): Y-> (0,2); N-> new_cur=3>2 discard. So final (0,2) true => mx=2 => YES.
Works.
Now consider k=0, s with only '?' but also some? It's YES only if we can set all Y. For k=0, any assignment that includes an N would make max>=1 >0. So we must set all ? to Y. So it's always possible unless there is a forced N. So DP would allow only states with cur=0, max=0. For each ?, only Y transition keeps that; N transition would produce state with cur>0, but since k=0, it would be discarded. So final state exists if we never forced N. Good.
Now consider the possibility of negative dissatisfaction? No.
Thus solution is correct.
But is there any situation where DP might incorrectly discard a valid assignment because we restrict cur_len and max to <= k? Since any valid assignment that achieves exactly k will have max=k and cur_len ≤ max (cur_len could be anything ≤ k, but at each step, cur_len can be ≤ k; if at some step cur_len becomes > k, then max would be >k, which contradicts final max = k. So it's safe to discard those states early. So DP is correct.
Time complexity O(n * (k+1)^2). With n=100, k=100, (101)^2 ≈ 10201, times 100 = 1,020,100, times small constant. Fine.
Memory O(k^2).
We can implement using Python.
One more nuance: The DP could be optimized using bitsets, but not needed.
Now, I'll think if there might be any catch: The definition of dissatisfaction is maximum number of consecutive series that Stepan did not watch. That is exactly as we thought. So our DP is good.
But could there be a scenario where the maximum is exactly k but we never have a run of length exactly k because we have runs longer but they are not counted? No.
Thus, DP is solution.
We should also note that the input string might contain spaces? It's a sequence of symbols, probably without spaces. We'll read as string, possibly strip.
Now let's consider if we can get away with a simpler greedy? Might be possible: We need to check if we can assign ? such that longest N block length = k. Equivalent to: The maximum possible longest N block (by setting all ? to N) must be at least k, and the minimum possible longest N block (by setting all ? to Y) must be at most k, and there must be a way to get exactly k. However, the existence of exactly k may not be monotonic: for some k, maybe min <= k <= max but still impossible because the structure forces either >k or <k. Example: n=3, s="?N?", we computed min longest? If we set all ? to Y -> "YNY" => longest N =1. So min=1. If we set all ? to N -> "NNN" => longest N =3. So max=3. k=2 is in [1,3] but we found impossible. So not monotonic. So we need more nuance.
We could view the problem as: We have fixed Y and N. We can set ? to either. Let's denote the intervals between fixed Y's. Actually, Y's break the string into segments of positions where we can have N runs. But ? can be Y or N, so they can act as either break or part of runs.
Think about the segments between known Y's. But also unknown can become Y and split runs.
Better to treat as DP.
Thus I'll code DP.
But note: There's also possibility of using BFS or recursion with memoization. But DP as described is fine.
We'll write:
def solve():
import sys
input = sys.stdin.readline
n,k = map(int, input().split())
s = input().strip()
# maybe s length may be n; but ensure.
# Initialize dp as list of lists.
# Since k can be up to n, but n=100, it's fine.
dp = [[False](k+1) for _ in range(k+1)]
dp[0][0] = True
for ch in s:
new_dp = [[False](k+1) for _ in range(k+1)]
for cur in range(k+1):
for mx in range(k+1):
if not dp[cur][mx]:
continue
# Y
if ch == 'Y' or ch == '?':
new_cur = 0
new_mx = mx
new_dp[new_cur][new_mx] = True
# N
if ch == 'N' or ch == '?':
new_cur = cur + 1
# but new_cur may exceed k, we will check after compute new_mx
new_mx = mx if mx >= new_cur else new_cur # max(mx, new_cur)
if new_cur <= k and new_mx <= k:
new_dp[new_cur][new_mx] = True
dp = new_dp
# Check
for cur in range(k+1):
if dp[cur][k]:
print("YES")
return
print("NO")
But careful: In the Y branch, we also need to check that new_cur and new_mx are within bounds: new_cur=0 always <=k, new_mx=mx <=k because mx is from previous state which was within bounds. So fine.
Potential bug: When we assign new_dp[new_cur][new_mx] = True, we might override previous true states for that (cur,mx) but that's okay since it's boolean; we just set true.
But we must be careful about using the same new_dp for both transitions, but we are setting multiple times, which is fine.
Now, one more nuance: In the N branch, when we compute new_mx = max(mx, new_cur), we need to use max function. If new_cur <= k, but new_mx may be > k. So we check both new_cur <= k and new_mx <= k. Since new_mx = max(mx, new_cur), and mx <= k, new_cur <= k, new_mx <= k is automatically true? Actually if both <= k, max also <= k. So we only need to check new_cur <= k, but also ensure that new_mx computed is not > k. Since mx <= k and new_cur <= k, max <= k. So checking new_cur <= k suffices. However, we also need to check that the state we are coming from had mx <= k, which is guaranteed by our loops. So if new_cur <= k, then new_mx <= k automatically. So we can simply check new_cur <= k. But for safety, we can check both.
Now, what about the case where k = 0? Then in loops, range(k+1) is range(1): only index 0. So for cur=0,mx=0, we consider transitions. Y: new_cur=0,mx=0, okay. N: new_cur = 0+1 =1, but we check new_cur <= k? k=0, so 1 >0, so skip. So only Y. So dp[0][0] remains true if all positions allow Y. If any position forced N, then N branch is forced (ch == 'N') and we skip because new_cur=cur+1=... let's see: from state (0,0) with ch='N', N branch: new_cur=1 >0, skip, and Y branch not allowed. So dp becomes all false. So final check fails. So correct.
Now, we should consider that the DP might generate states where max remains less than k but later we can achieve max = k by extending runs. That's allowed.
Now, we need to confirm that we don't need to track whether we have achieved exactly k yet, because we can still get to k later. So DP as is fine.
Now, what about large k (say k=100) but n=100, DP dimensions 101x101 = 10201, times 100 = 1M, okay.
Thus solution is ready.
But before finalizing, let's test some more edge cases:
Case: n=5, k=3, s="?N?N?".
We can manually test? Not needed.
But we can trust DP.
Potential Pitfall: The DP loops over cur and mx, but we might lose some states due to overwriting if we set new_dp multiple times for the same (new_cur, new_mx) but that's fine; it's boolean OR.
Now, consider using a set of (cur, mx) for more efficiency, but not needed.
Now, one more nuance: The DP state as defined uses cur length up to k. But if k is small, say k=2, and we have a run that we could potentially extend later beyond k, but we don't because we set a Y. However, during the DP, when we process a position that is forced N, and cur=2, then new_cur=3, which is >k, and we discard. That is correct because that assignment would create a run length 3 > k and thus invalid for final max = k. However, suppose we later had the option to set that position to Y? But if it's forced N, we cannot. So discard. If it's ?, we could choose Y to avoid. So DP respects forced constraints.
Now, I think it's correct.
We can also note that the DP essentially solves a feasibility of a regular language: strings over {Y,N} that satisfy given pattern and longest run N ≤ k and = k. That is a regular property, so DP is appropriate.
Now, consider time constraints: 1 second, Python may be okay with up to ~10 million operations, this is about 1 million plus loops overhead. Should be fine.
But we can micro-opt: Use list of list of bools, but perhaps using a bytearray or bitset could be faster, but not needed.
Alternatively, we can reduce state dimension: we only need to know current run length and whether we have already achieved a run of length k, and also ensure no run > k. That would be dp[cur][flag] where flag indicates if we have seen a run of length exactly k (or max so far is exactly k). But we also need to know if we have exceeded k. However, we can incorporate that by discarding states where cur > k. But flag only tells if we have seen exactly k, but we need to ensure that we never exceed k. So we can keep dp[cur][flag] with cur ≤ k and flag ∈ {0,1,2} maybe: 0 means max_sofar < k; 1 means max_sofar = k; 2 means max_sofar > k (discarded). So we can keep flag 0 or 1 only. Because states with flag 2 are discarded. However, we also need to know if we have seen a run of length exactly k but also might have seen runs < k only? Actually, if we have seen a run of length k, then max_sofar = k. But there is also possibility that we have seen a run of length less than k but not yet k. So flag=0 indicates max < k, flag=1 indicates max = k. But we also need to know the exact max if it's less than k? Because future extensions could turn it into exactly k or beyond. For instance, current run length is c (c < k) and max_sofar = m which might be less than k, but m could be less than c? Actually max_sofar is max of previous max and current run length. So if we only know flag=0 (max<k), we don't know the exact current run length c; we need c because it influences future extensions. So we need to store c as well. So state is (c, flag) with flag indicating whether we have already seen a run of length k. But if flag=0, then max < k, but we still need to know the exact c because c could be, say, k-1, and if we add an N next, c becomes k, which would set flag=1. So we need c to compute that transition. So we still need to store c. But flag is redundant if we store max_sofar exactly; we could store max_sofar as an integer up to k. That's what we have. So it's fine.
Maybe we can compress by storing only current run length and a boolean if we have achieved exactly k, but we also need to know the exact previous max if it's less than k, to ensure that we don't accidentally allow a run that later becomes >k while having a run of length exactly k? Actually, to ensure final max exactly k, we need that at the end, the overall maximum is exactly k. So it's enough to know that we have never exceeded k and we have at some point reached exactly k. But we don't need to know the exact maximum if it's less than k; because if we haven't reached k yet, then the maximum so far is some value less than k, but we don't need to know exactly what it is for future constraints? Wait, we need to ensure that we never exceed k. For that, we need to know the current run length to ensure it doesn't exceed k when we add N. That's fine. But for the condition that the maximum so far is exactly k at the end, we need to know whether we have ever achieved k. However, we also need to know the exact maximum if it's less than k because future runs might exceed it but still be ≤ k; but we only need to ensure we don't exceed k. So we don't actually need to know the exact maximum if it's less than k, because as long as we never exceed k, the maximum will be at most k. But we need to know if we ever reached k exactly, because final max must be k, not less. So we could track two things: current run length c (0 ≤ c ≤ k) and a flag f indicating whether we have already achieved a run of length k (0 for not yet, 1 for yes). However, we also need to ensure that we don't have a run that later becomes >k. That's ensured by c ≤ k. But is it possible that we have already achieved a run of length k (f=1) and later we have a run that becomes length k again? That's fine; still f remains 1. So we don't need to know the exact max value if it's less than k. So we can reduce state size from (k+1)*(k+1) to (k+1)*2 = O(k) which is even better. But we need to verify that this reduction is correct.
Let's explore: In the DP with full (c, m), the transitions depend on m to compute new max: new_m = max(m, new_c). If we only know whether we have achieved k (f) and current run length c, can we correctly decide the new f? For f=0 (haven't seen k), we need to know if new_c == k or if we had some previous max that might be less than k but possibly could be combined? Actually, if f=0, that means max so far < k. But we don't know the exact value, but we only need to know if we ever get to k. The condition new_c == k would set f=1. But is it possible that we get to k without new_c == k, by having a previous run that was exactly k? That's already covered by f=1. So for f=0, if new_c == k, then f becomes 1; else f remains 0. But is it possible that max so far < k and we have a run that is not current but some earlier run that was exactly k? No, f=0 means no run has reached k. So that's fine.
But we also need to ensure that we never exceed k. That's enforced by c ≤ k and new_c ≤ k (i.e., we discard if new_c > k). However, what about the scenario where we have f=1, meaning we already have a run of length exactly k. That's okay, but we still must ensure that we never have a run > k. So we still discard if new_c > k. So state (c, f) with c ∈ [0,k] and f ∈ {0,1} is sufficient? Let's test with some cases.
Consider string "N?N" with k=2. Suppose we process positions: pos1 N => c=1, f=0 (since 1<2). pos2 ?: we consider both assignments. If we choose Y => c=0, f remains 0 (since max so far is 1, still <2). If we choose N => new_c = 2 => since new_c == k, f becomes 1; c=2. pos3 N: from state (c=2, f=1) N => new_c=3 >2 => discard. From state (c=0, f=0) N => new_c=1, f remains 0 (since 1<2). So final states: (1,0) only. No state with f=1. So answer NO. That matches.
Now consider string "?N?Y" with k=2 earlier we had YES. Let's test with reduced DP:
init (c=0,f=0).
pos1 ?:
Y: (0,0)
N: new_c=1 <2 => (1,0)
states: (0,0),(1,0)
pos2 N:
For (0,0): N => new_c=1 -> (1,0)
For (1,0): N => new_c=2 ==2 => f=1 => (2,1)
states: (1,0),(2,1)
pos3 ?:
From (1,0):
Y -> c=0, f=0 => (0,0)
N -> new_c=2 => f becomes 1 => (2,1)
From (2,1):
Y -> c=0, f=1 => (0,1)
N -> new_c=3 >2 discard.
States: (0,0),(2,1),(0,1)
pos4 Y:
From (0,0): Y -> (0,0)
From (2,1): Y -> (0,1)
From (0,1): Y -> (0,1)
Final states: (0,0) (f=0) and (0,1) (f=1). Since there is state with f=1, answer YES. Works.
Now consider a more subtle scenario: Suppose we have a run of length less than k, but earlier we had a run of length exactly k? Actually that's covered because f=1 persists even after reset. Example: "NNYNN" with k=2. Let's test: We want max exactly 2. The string has three N's? Actually "NNYNN": positions 1,2 N => run length 2 (exactly 2). Then Y breaks, then positions 4,5 N => run length 2 again. So max =2. DP with reduced state:
pos1 N: (1,0)
pos2 N: new_c=2 => f=1 => (2,1)
pos3 Y: from (2,1): Y => (0,1)
pos4 N: from (0,1): N => new_c=1, f remains 1? Since f=1 already, even if new_c=1, f stays 1. So (1,1)
pos5 N: from (1,1): N => new_c=2 <=k, f stays 1 => (2,1)
End: state (2,1) f=1 => YES.
Works.
Now consider a case where max so far is less than k but we have a current run c that, when extended, could become exactly k, but we also have a previous run that was also less than k but we need to know exactly how long it was to decide if the max will become exactly k? No, because as long as we never exceed k, and we eventually produce a run of exactly k, we achieve max = k. The earlier runs that were less than k are irrelevant. So indeed we only need to know if we've already achieved a run of k (f=1). Also we need to know current run length c to handle future extensions and avoid exceeding k.
But is there any scenario where we need to know the exact value of max_sofar when it's less than k for the purpose of determining whether a future run could become exactly k? Actually, if we have f=0, we haven't seen a run of length k yet. The maximum so far is some value m < k. But we don't know m. However, to achieve final max = k, we must eventually create a run of length k. That run could be an extension of the current run, or a new run after a Y. For a new run after a Y, it starts from 0. So we only need to know current run length to see if it can be extended to k. The earlier max m doesn't affect the possibility of creating a run of length k in the future, because any future run can achieve length k regardless of m, as long as we can assign N's appropriately. However, there might be constraints from fixed Y's that limit the length of a run. But that's captured by the positions and the current run length. The fact that earlier max was m does not restrict future runs; it only matters for the final condition that we have at least one run of length k. So we just need to know whether we have already achieved that. So indeed the reduced DP should be correct.
But is there a scenario where the maximum so far is exactly some m < k, and we need to know m to decide if we can later achieve exactly k without exceeding k? For example, suppose we are at position i with current run length c, and we haven't yet seen a run of length k. The maximum so far is m < k. Suppose later we have a forced Y after a few positions, and then we have a segment where we can only create a run of up to some length L (due to fixed Y's and ? decisions). To have final max = k, we need to ensure that either we already get a run of length k later, or we have already gotten it earlier. But if we haven't gotten it yet, we need to create it later. The ability to create a run of length k later does not depend on m; it depends on the structure of remaining positions and our decisions. So not storing m is fine.
But what about the condition that the overall maximum should be exactly k, not less. If we end with f=0 (never reached k), that's invalid. So we need f=1 at the end.
Now, could there be a scenario where we have f=1 (i.e., we had a run of length exactly k) but later we accidentally create a run longer than k, which would invalidate? That would be prevented by discarding states where c exceeds k. However, it's possible to have f=1 and then later create a run that exceeds k but we discard that state, so it's fine.
Thus, reduced DP works and is more efficient.
But is there any subtlety: Suppose we have f=0 and current run length c is less than k. Later we create a run of length exactly k, but we also had a previous run that was length m < k. That's fine. However, what if later we create a run that is length k, but also we might have had a previous run that was also length k? That's fine. So f becomes 1 when we first create a run of length k; it stays 1.
Now, is it possible that we might have a run of length > k that we could "fix" by later making it not count? No, once we have a run of length > k, max > k, can't undo.
Thus, the reduced DP should be correct.
But to be absolutely rigorous, let's prove equivalence: For any string, define property P: max run length = k. This is equivalent to (i) no run length > k, and (ii) some run length = k. In the DP with state (c, f) where c is current run length (0 ≤ c ≤ k) and f indicates whether we have seen a run of length k (f=1) or not (f=0), we can simulate processing left to right. The transition when we see a symbol that is N: new_c = c+1; if new_c > k → discard (since it would violate (i)). Otherwise, new_f = f or (new_c == k) (i.e., f' = f or (new_c == k)). When we see Y: new_c = 0, new_f = f (since Y does not create a run, but it doesn't change whether we have seen a run of length k). This exactly captures the necessary conditions: we track whether we have ever seen a run of length k, and ensure we never exceed k. At the end, we require f=1. This is indeed equivalent to the original condition because if we have never exceeded k and f=1, then there exists at least one run of length exactly k, and all runs are ≤ k, so max = k. Conversely, if max = k, then no run > k, and some run = k, so DP will have a path with f=1 and never discarded. So yes.
Thus we can implement reduced DP for simplicity: dp is a 2D array of size (k+1) x 2 (or a set). We'll have dp[cur][f] boolean.
Initialize dp[0][0] = True.
For each character ch:
new_dp = [[False, False] for _ in range(k+1)] # or use 2D list.
For each cur in 0..k:
for f in [0,1]:
if not dp[cur][f]: continue.
# Y allowed?
if ch == 'Y' or ch == '?':
new_cur = 0
new_f = f
new_dp[new_cur][new_f] = True
# N allowed?
if ch == 'N' or ch == '?':
new_cur = cur + 1
if new_cur <= k:
new_f = f or (new_cur == k) # if new_cur == k, set to 1
new_dp[new_cur][new_f] = True
dp = new_dp
At the end, check if any cur in 0..k has dp[cur][1] true.
This uses O(k) states per step.
But note: For k=0, the condition "new_cur == k" (0) would be true if new_cur=0, but that would set f=1 immediately? Let's test: For k=0, the only allowed state initially is cur=0, f=0. For each position, Y: new_cur=0, new_f=f (0). N: new_cur = cur+1 = 1, but 1 >0 so discard. So only Y transitions. At the end, we have f=0. We need f=1? But max=0 should be considered as having a run of length 0? Actually, dissatisfaction is the maximum number of consecutive N's. If max=0, that means there are no N's. In our condition, we need some run of length exactly k. For k=0, what does it mean to have a run of length 0? Typically, a run of consecutive N's of length 0 is not a run; the maximum is 0 if there are no N's. So we need to treat k=0 as a special case: we need that there is no run of N's of length >=1. In terms of f flag, if k=0, the condition of having a run of length exactly 0 is vacuously true? Actually, we need the maximum to be exactly 0, which means there is no N at all. In our DP, we set f to indicate whether we have seen a run of length k. For k=0, a run of length 0 is not something we "see" as a positive event; we would need to check that we have not seen any N. But if we define f =1 when we have seen a run of length 0, it doesn't make sense. So the reduced DP with f flag might need to treat k=0 separately. Let's analyze.
The property max = 0 is equivalent to: no N appears. That means all positions are Y. So we can handle k=0 as a special case: just check if it's possible to have no N. That is, if any forced N exists, NO; otherwise YES (since we can set all ? to Y). So we can special-case k=0. Then for k>0, we can use the reduced DP with f flag.
Alternatively, we could keep the original DP with full m, which naturally handles k=0 because we check m == 0 at the end. That might be simpler than special casing. But original DP also has O(k^2) which is still fine. Considering that n is small, the original DP is fine and we don't need to worry about the special case. So I'll stick with the original DP with full (c, m) to avoid edge cases. Also it's simpler to reason.
But the reduced DP is appealing for efficiency, but not needed. I'll implement original DP to be safe.
Given that the problem is simple, we can code either. But I'll consider that for k=0, the original DP works as described earlier. So let's use original DP with (c, m). I'll keep it.
One more nuance: The DP loops over mx from 0 to k. That's okay. But note that mx can be less than cur? Actually max_sofar is max of previous max and current run length. In our state, we maintain consistency: after each step, we have mx >= cur? Not necessarily: Suppose we had a previous max 3, and then we have a Y resetting cur to 0, but mx remains 3. So mx can be > cur. That's allowed. So state (cur, mx) with mx possibly > cur. So we need to store both.
If we use reduced DP with f flag, for k>0, we don't need to know mx exactly, but we need to know if we've seen a run of length k. That works because we only care about whether we've seen k, not the exact max if it's less than k. But we also need to ensure that we never exceed k, which is done by discarding new_cur > k. However, what about a scenario where we have f=0 (haven't seen k) but the max so far is, say, 2, and k=3. We are at a state with cur=2, f=0. That's fine. But if we then see a Y, cur resets to 0, but f stays 0. That's correct because we haven't seen a run of length 3. But we lost the information that we had a run of length 2. That's okay because we only care about reaching k. The question: Could the existence of a run of length 2 be necessary for some later constraint? No, because later we only need to know if we can eventually get a run of length 3. The fact that we had a run of length 2 doesn't affect future possibilities, except that it doesn't give us a run of length 3. So it's fine.
Thus the reduced DP is correct for k>0. However, for simplicity I'll keep the original DP.
Given the small limits, I'll implement the original DP.
Let's write code.
Potential small bug: In the DP transition for N, we compute new_mx = max(mx, new_cur). Since both mx and new_cur are ≤ k (ensured by checks), new_mx ≤ k. So we don't need to check new_mx, but we do need to check new_cur ≤ k. However, what about case where mx is exactly k but new_cur is less than k? That's allowed, state (new_cur, k) is allowed, and we will keep it. That's valid because we have already achieved max k but not exceeding. So we must allow states with mx = k. In our loops, we loop over mx up to k, so that's fine.
Now, implement.
Pseudo:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data: return
n = int(data[0]); k = int(data[1])
s = data[2].strip()
# maybe s length > n? but it should be n. we can slice if needed, but trust input.
dp = [[False](k+1) for _ in range(k+1)]
dp[0][0] = True
for ch in s:
ndp = [[False](k+1) for _ in range(k+1)]
for cur in range(k+1):
for mx in range(k+1):
if not dp[cur][mx]:
continue
# Y
if ch == 'Y' or ch == '?':
ndp[0][mx] = True
# N
if ch == 'N' or ch == '?':
new_cur = cur + 1
if new_cur <= k:
new_mx = mx if mx >= new_cur else new_cur
ndp[new_cur][new_mx] = True
dp = ndp
# check
for cur in range(k+1):
if dp[cur][k]:
print("YES")
return
print("NO")
Testing with examples.
Now, consider input where n=0? Not possible, n>=1.
Now, what about extra whitespace? We use split, which splits by whitespace. The string may be on the second line, so splitting will give tokens: first token n, second k, third the string. That's fine.
Now, test manually with some cases.
Case: n=3, k=0, s="Y?Y". Should be YES? We can set ? to Y, all Y, max 0. DP: k=0, dp[0][0]=True. Process 'Y': only Y, ndp[0][0]=True. Process '?': from (0,0) Y -> ndp[0][0] true; N -> new_cur=1 >0 discard. So after ?, dp[0][0]=True. Process 'Y': same -> dp[0][0]=True. Final dp[0][0] true, mx=0 => YES. Good.
Case: n=3, k=0, s="Y?N". Should be NO because forced N at last. DP: after 'Y' -> (0,0). After '?': from (0,0) Y->(0,0); N-> new_cur=1 >0 discard, so dp[0][0] remains. After 'N': from (0,0) N forced: new_cur=1>0 discard; Y not allowed. So dp becomes all false. Final no state => NO. Good.
Now, test with k=2, s="?N?N?" we can do quick mental: Could we get exactly 2? Possibly: assign positions: 1=Y,2=N,3=Y,4=N,5=Y => runs: single N's, max=1, not 2. Or 1=N,2=N,3=Y,4=N,5=N => runs: first two N length2, then later two N length2, max=2. But there are consecutive N at 1-2 and 4-5, but need to ensure that positions 1,2,3,4,5: "N N Y N N" => runs length2. That's valid if we can set position3=Y. That is allowed because position3 is '?'. So yes. So answer YES. Let's test DP quickly:
k=2. Steps:
1: '?': states (0,0) and (1,1).
2: 'N': from (0,0)-> (1,1); from (1,1)-> (2,2). So states (1,1) and (2,2).
3: '?': from (1,1): Y->(0,1); N-> new_cur=2->(2,2) (since new_cur=2 => new_mx = max(1,2)=2). So (0,1) and (2,2). From (2,2): Y->(0,2); N-> new_cur=3>2 discard. So also (0,2). So states: (0,1),(2,2),(0,2).
4: 'N': from (0,1): N-> (1,1) (new_cur=1, mx=1); from (2,2): N-> new_cur=3>2 discard; from (0,2): N-> (1,2) (new_cur=1, mx remains 2). So states (1,1) and (1,2).
5: '?': from (1,1): Y->(0,1); N-> (2,2). from (1,2): Y->(0,2); N-> new_cur=2 -> new_mx = max(2,2)=2 => (2,2). So states (0,1),(2,2),(0,2). At end, we have (0,2) and (2,2) with mx=2, so YES. Good.
Thus DP works.
Now, consider a tricky case where it might be impossible even though k is between min and max due to structural constraints. Example earlier: n=3, s="?N?", k=2, we saw NO. DP gave NO. Good.
Now, also consider scenario where k is exactly possible but there is also a forced N run longer than k? Then impossible. DP will discard.
Now, performance: loops: for each position (≤100), for cur 0..k (≤100), for mx 0..k (≤100), that's 1e6. Inside we have condition checks and set booleans. This is fine.
But we can further optimize by using list comprehensions or using sets, but not needed.
Now, we need to ensure we don't get index error: new_cur = cur+1, then check if new_cur <= k. That's fine.
Potential bug: When we set ndp[0][mx] = True for Y transition, we need to ensure that mx is within range. mx is from loop, it's ≤k, so ok.
Now, after processing all characters, we check any cur with dp[cur][k] true.
One more edge: k may be larger than any possible max? For example, n=3, k=3, s="YYY". Then min max=0, max max=0 (since all Y forced, ? none). So cannot achieve 3. DP: k=3, process all Y's, only states with cur=0 and mx=0. At end, check mx=3? None, so NO. Good.
Now, consider k=3, s="???". Should be YES because we can set all N => run length 3, max=3. DP: Process three '?', will have state (3,3) if we always choose N, and since k=3, allowed. So YES.
Now, test with k=3, s="??Y??". Can we get exactly 3? Possibly: set all ? to N except maybe break? But we have a Y in middle. So max run would be at most max(length of left N block, right N block). If we set left block length 2 and right block length 2, max=2; if we set left 3 and right 0, but left can be at most 2 because only two positions before Y. So max=2. So cannot reach 3. DP should give NO. So it's fine.
Thus solution is correct.
Now, I'll write the code accordingly.
But before finalizing, let's think about any pitfalls with reading input. The input format: first line two integers n and k. Second line a sequence of n symbols. There might be spaces? Usually it's a string without spaces. We'll read using sys.stdin.readline for first line, then second line read as string and strip. That's fine.
Now, a subtle case: n may be up to 100, k may be up to n. If k is large but not all positions are N, DP states for mx up to k may include many values that are not achievable, but it's fine.
Now, we should also consider that dp arrays are of size (k+1)*(k+1) and we reinitialize each step with new list of lists. That's okay.
Now, let's write the final code.
We'll import sys.
Edge case: If the input string has length > n due to newline? We'll just take first n characters maybe. But the problem says it consists of n symbols, so it's exactly n. We'll trust.
Now, finalize.
One more check: In the DP transition for Y, we set ndp[0][mx] = True, but if there is also a transition from another state that sets same ndp entry, it's fine.
Now, answer.
But we need to ensure that for k=0 we handle correctly. In our DP, we check dp[cur][0] at the end. For k=0, we have only mx=0. That will be true only if we ended with some state where max_sofar=0. That means we never had any N. As argued, that's correct. So no special case needed.
Now, final answer.
Now, we can also attempt to see if there is a simpler necessary and sufficient condition: The dissatisfaction can be exactly k if and only if:
It is possible to have max ≤ k (i.e., we can avoid runs longer than k) AND
It is possible to have max ≥ k (i.e., we can achieve a run of at least k), AND
Additionally, there might be a conflict: achieving the run of length k might force a run longer than k? But if we can achieve both separately, can we always achieve exactly k? Not always, as the example "?N?" with k=2 shows: It is possible to have max ≤ 2 (by setting ? to Y, resulting max=1) and possible to have max ≥ 2 (by setting all ? to N, resulting max=3), but not possible to have exactly 2 because any assignment that includes a run of length 2 also yields a run longer than 2? Actually check: for "?N?" we can try: assign first ? to N, second is fixed N, third ? to Y => "N N Y" gives run length 2 only. That's actually a run of length 2. Wait, "?N?" we have positions: 1?,2N,3?. For k=2, we can set pos1=N, pos3=Y => "N N Y" => runs: first two are N, length2. That gives max=2. Is there any longer run? No, because after Y. So why did we earlier think it's impossible? Let's re-evaluate: earlier I considered "?N?" and concluded impossible? I used example "?N?" and said if we set ? to N,N => run length 3; if set Y,N,Y? Actually I earlier used "N?N" not "?N?". I need to re-check.
Wait, I previously considered "N?N" with k=2, which gave NO. That's different. For "?N?" with k=2, can we achieve exactly 2? Let's enumerate all assignments for "?N?":
Positions: 1 (?) , 2 (N fixed), 3 (?).
Assignments:
1=Y,2=N,3=Y => "Y N Y": runs: single N -> max=1.
1=Y,2=N,3=N => "Y N N": runs: length2 at end -> max=2.
1=N,2=N,3=Y => "N N Y": runs: length2 at start -> max=2.
1=N,2=N,3=N => "N N N": runs: length3 -> max=3.
So there are assignments with max=2. So for "?N?" k=2, answer should be YES. So my earlier claim that "?N?" is impossible for k=2 was mistaken. The impossible case was "N?N". So the example "N?N" with k=2 is impossible, which we tested earlier. So the condition "?N?" shows that having both possibilities separate does allow exact 2. So the conflict case is when you need to create a run of length k that necessarily extends to >k due to fixed N's on both sides? Actually "N?N": to get a run of length exactly 2, you would need the middle ? to be Y, giving "N Y N" -> runs length1 each, max=1. If you set ? to N, run length3 -> max=3. So no assignment gives exactly 2. So it's not just about separate feasibility; the structure can make it impossible to hit exactly k while avoiding >k. So DP is needed.
Thus DP solution stands.
Now, we should also consider if any state pruning could cause missing solutions? No.
Now, I'll write code.
One more micro-optimization: We could use Python's list of bytearray or use set for dp to reduce loops. But not needed.
But one thing: The DP loops over mx from 0 to k, but many mx values may be unreachable, but we still loop over all. That's okay.
Alternatively, we could use a dictionary to store reachable (cur, mx) pairs. That would be more efficient if many states unreachable, but still fine.
Given n=100 and k<=100, maximum number of states per step is (k+1)*(k+1) ≈ 10k^2, which is at most ~10,000. Loops over 10,000 * 100 = 1,000,000, which is trivial.
We'll implement straightforward.
Now, code:
def solve():
import sys
input_data = sys.stdin.read().strip().split()
if not input_data:
return
n = int(input_data[0])
k = int(input_data[1])
s = input_data[2].strip()
# In case s length > n, we can slice to n.
# But it should be exactly n.
# Might be that there are spaces in the string? No, it's a continuous string.
# So we trust.
code
# DP: dp[cur][max]
dp = [[False]*(k+1) for _ in range(k+1)]
dp[0][0] = True
for ch in s:
ndp = [[False]*(k+1) for _ in range(k+1)]
for cur in range(k+1):
for mx in range(k+1):
if not dp[cur][mx]:
continue
if ch == 'Y' or ch == '?':
ndp[0][mx] = True
if ch == 'N' or ch == '?':
new_cur = cur + 1
if new_cur <= k:
new_mx = mx if mx >= new_cur else new_cur
ndp[new_cur][new_mx] = True
dp = ndp
for cur in range(k+1):
if dp[cur][k]:
print("YES")
return
print("NO")
if name == "main":
solve()
Now, test with given examples.
Example1:
Input:
5 2
NYNNY
Our code: should print YES.
Example2:
6 1
????NN -> NO.
Now, additional tests:
Test: 3 2 ?N? -> should be YES. Let's run mental: earlier we determined YES. Our DP should output YES.
Test: 3 2 N?N -> should be NO.
Test: 1 0 ? -> YES.
Test: 1 0 N -> NO.
All good.
Now, consider memory: dp is two 2D lists of about (101x101) each, ~10k booleans, each bool maybe 28 bytes? Not a problem.
Now, confirm that we didn't need to restrict cur to <=k in the Y transition? cur becomes 0 always <=k, so fine.
Now, done.
But one more nuance: In the N transition, we set new_mx = max(mx, new_cur). But if mx is already > new_cur, new_mx = mx. However, we have already ensured mx <= k. So new_mx <= k. So fine.
Now, final answer.
But what about the possibility of the string containing spaces? The problem says "sequence which consists of n symbols", likely no spaces. So reading with split will work; if there were spaces, they'd be separate tokens, but then the string would be broken. But it's standard to have a string without spaces. We'll use readline for second line to get the exact string, maybe better.
Better to read first line via sys.stdin.readline, then second line via sys.stdin.readline, to avoid issues if string contains spaces? But it doesn't. However, using split might cause if the string contains spaces (unlikely) it would be split. So to be safe, we can do:
import sys
n,k = map(int, sys.stdin.readline().split())
s = sys.stdin.readline().strip()
This reads the second line exactly, even if it contains spaces? The problem says it's a sequence of symbols, no spaces. So it's fine.
I'll use that.
Now, final code:
import sys
def solve():
n,k = map(int, sys.stdin.readline().split())
s = sys.stdin.readline().strip()
# Ensure length n, but we can just iterate over s; if extra spaces, strip.
dp = [[False](k+1) for _ in range(k+1)]
dp[0][0] = True
for ch in s:
ndp = [[False](k+1) for _ in range(k+1)]
for cur in range(k+1):
for mx in range(k+1):
if not dp[cur][mx]:
continue
if ch == 'Y' or ch == '?':
ndp[0][mx] = True
if ch == 'N' or ch == '?':
new_cur = cur + 1
if new_cur <= k:
new_mx = mx if mx >= new_cur else new_cur
ndp[new_cur][new_mx] = True
dp = ndp
for cur in range(k+1):
if dp[cur][k]:
print("YES")
return
print("NO")
if name == "main":
solve()
Now, let's test with sample inputs manually using mental simulation.
Sample1:
Input:
5 2
NYNNY
Processing:
Initially dp[0][0]=True.
ch='N': ndp: from (0,0) N => new_cur=1, new_mx=1 => ndp[1][1]=True. Y not allowed. So dp after step1: (1,1)
ch='Y': from (1,1) Y => ndp[0][1]=True. So dp (0,1)
ch='N': from (0,1) N => new_cur=1, new_mx=max(1,1)=1 => ndp[1][1]=True. So dp (1,1)
ch='N': from (1,1) N => new_cur=2, new_mx=2 => ndp[2][2]=True. So dp (2,2)
ch='Y': from (2,2) Y => ndp[0][2]=True. So final dp[0][2] true => YES.
Sample2:
Input:
6 1
????NN
Step by step earlier gave NO. So output NO.
Now, test with custom:
3 2 ?N?
We trust.
Thus answer is correct.
Now, I think we are ready to output the solution.
But before finalizing, double-check if there is any situation where DP might produce false positive due to not considering that we must assign all ? to some value? We consider both possibilities, so all assignments are covered.
Now, consider the case where there are no ? and the string is fixed. Then DP will just compute the exact max. If it equals k, then YES else NO. That's correct.
Now, note: The DP as written uses dp[cur][mx] for cur up to k. But what if k > n? Input says k ≤ n, so not an issue.
Now, final code.
But let's also think about alternative method: Could we just compute the minimum possible max and maximum possible max, and then check if k is between and there exists an assignment giving exactly k? The earlier counterexample "N?N" with k=2 shows min max = 1? Actually, for "N?N", if we set ? to Y, we get "NYN", max=1. If set ? to N, we get "NNN", max=3. So min=1, max=3. k=2 is between but not achievable. So we need a stronger condition.
Thus DP is necessary.
Now, ensure we handle input correctly if there are multiple test cases? Problem only has single test case.
Now, answer.
Thus I'll present the solution with DP.
Now, I should explain the reasoning in the final answer. I'll provide a clear explanation.
Now, final output. ```python
import sys
def solve() -> None:
n, k = map(int, sys.stdin.readline().split())
s = sys.stdin.readline().strip()
code
# dp[cur][mx] = is it possible after processing some prefix
# that the current run length is cur and the maximal run so far is mx.
# Both cur and mx are kept ≤ k (longer runs would make the answer > k).
dp = [[False] * (k + 1) for _ in range(k + 1)]
dp[0][0] = True
for ch in s:
ndp = [[False] * (k + 1) for _ in range(k + 1)]
for cur in range(k + 1):
for mx in range(k + 1):
if not dp[cur][mx]:
continue
# put 'Y' (or keep as Y if forced)
if ch == 'Y' or ch == '?':
ndp[0][mx] = True
# put 'N' (or keep as N if forced)
if ch == 'N' or ch == '?':
new_cur = cur + 1
if new_cur <= k:
new_mx = mx if mx >= new_cur else new_cur # max(mx, new_cur)
ndp[new_cur][new_mx] = True
dp = ndp
# check whether any state with overall maximum exactly k exists
for cur in range(k + 1):
if dp[cur][k]:
print("YES")
return
print("NO")