1. Problem understanding
We are given:
A string
scontaining lowercase English letters.A permutation array
p[]that determines which character is replaced by'?'on each day.
On Day 1, we replace the character at index p[0]. On Day 2, we replace the character at index p[1], and so on.
Our goal is to find the minimum number of days required so that no two adjacent characters are equal, unless at least one of them is '?'.
Example
s = "aabb"
p = [2, 1, 3, 0]Initially:
aabb
The adjacent pairs are:
aa→ Invalid, because both characters are equal.ab→ Valid, because characters are different.bb→ Invalid, because both characters are equal.
We need to replace at least one character from each invalid pair.
2. The important observation
Instead of simulating every day, we can find the day on which each character is replaced.
For each invalid adjacent pair, we need to know the earliest day that either of its two characters becomes '?'.
But we need to fix all invalid pairs, so the answer will be the latest among those earliest replacement days.
This is an inverse permutation problem.
3. C# solution
class Solution
{
public int getMinimumDays(string s, int[] p)
{
int n = s.Length;
int[] day = new int[n];
// Store the replacement day for each index
for (int i = 0; i < n; i++)
{
day[p[i]] = i + 1;
}
int ans = 0;
// Check every adjacent pair
for (int i = 0; i < n - 1; i++)
{
if (s[i] == s[i + 1])
{
ans = Math.Max(ans, Math.Min(day[i], day[i + 1]));
}
}
return ans;
}
}4. Core concept – Inverse permutation
The permutation array tells us which index will be replaced on each day.
Given:
p = [2, 1, 3, 0]This means:
Day | Index replaced |
|---|---|
Day 1 | 2 |
Day 2 | 1 |
Day 3 | 3 |
Day 4 | 0 |
But our problem is easier if we know the replacement day for each index.
So, we reverse the mapping.
day[p[i]] = i + 1;Why i + 1?
Because i is zero-based, but days are counted starting from Day 1.
Original permutation
p = [2, 1, 3, 0]Inverse mapping
day = [4, 2, 1, 3]String index | Replacement day |
|---|---|
Index 0 | Day 4 |
Index 1 | Day 2 |
Index 2 | Day 1 |
Index 3 | Day 3 |
Now we can quickly find when any character becomes '?'.
5. How do we calculate the minimum days?
We examine every adjacent pair in the original string.
if (s[i] == s[i + 1])If the characters are equal, the pair is invalid.
For example:
aaTo make this pair valid, we need to replace either the first a or the second a.
The replacement days are:
day[i] = 4
day[i + 1] = 2The earliest day this pair becomes valid is:
\[ \min(4,2)=2 \]
We use Math.Min() because we only need one of the two characters in the pair to be replaced.
However, there may be several invalid pairs. We need to make all of them valid.
Therefore, we take the maximum of these earliest days:
\[ \boxed{ans=\max(\min(day[i],day[i+1]))} \]
We calculate this only when the adjacent characters are equal.
Math.Min()→ earliest replacement day for one invalid pair.Math.Max()→ latest required day across all invalid pairs.
6. Dry run – Example 1
Input
s = "aabb"
p = [2, 1, 3, 0]Step 1: Create the inverse day array
for (int i = 0; i < n; i++)
{
day[p[i]] = i + 1;
}Let's trace:
|
|
| Updated array |
|---|---|---|---|
0 | 2 |
|
|
1 | 1 |
|
|
2 | 3 |
|
|
3 | 0 |
|
|

Join the conversation! Your thoughts help the community grow.