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 |
|
|
Final day array: [4, 2, 1, 3]
Step 2: Check adjacent pairs
Original string:
a a b b
0 1 2 3Pair | Characters | Replacement days | Earliest day | Valid? |
|---|---|---|---|---|
0–1 |
| 4, 2 | 2 | Invalid initially |
1–2 |
| 2, 1 | – | Already valid |
2–3 |
| 1, 3 | 1 | Invalid initially |
Pair aa
day[0] = 4
day[1] = 2\[ \min(4,2)=2 \]
This pair becomes valid on Day 2.
Pair ab
The characters are already different.
a != bNo replacement is needed.
Pair bb
day[2] = 1
day[3] = 3\[ \min(1,3)=1 \]
This pair becomes valid on Day 1.
Step 3: Calculate the answer
ans = Math.Max(ans, Math.Min(day[i], day[i + 1]));We have:
Pair
aa→ 2 daysPair
bb→ 1 day
\[ ans=\max(2,1)=2 \]
Output: 2
Visual explanation
Initially
aabb
Both aa and bb are invalid.
Day 1 – Replace index 2
aa?b
The bb pair is fixed, but aa is still invalid.
Day 2 – Replace index 1
a??b
All adjacent pairs are valid. The answer is 2.
7. Dry run – Example 2
Input
s = "abca"
p = [3, 0, 2, 1]Original string:
abcaCheck every adjacent pair:
Pair | Characters | Condition |
|---|---|---|
0–1 |
| Different |
1–2 |
| Different |
2–3 |
| Different |
There are no invalid pairs.
Therefore, we never need to replace any character.
Output: 0
8. Dry run – Example 3 (All same characters)
Suppose:
s = "aaaa"
p = [3, 2, 1, 0]The inverse mapping is:
day = [4, 3, 2, 1]Pair | Replacement days | Earliest day |
|---|---|---|
| 4, 3 | 3 |
| 3, 2 | 2 |
| 2, 1 | 1 |
The answer is:
\[ \max(3,2,1)=3 \]
Output: 3
On Day 3, the string becomes:
?aa?The middle aa pair is also fixed because index 2 was replaced on Day 2, so the actual string after Day 3 is:
??a?All adjacent pairs are valid by Day 3.
9. Line-by-line code explanation
int n = s.Length;
int n = s.Length;Stores the length of the string.
int[] day = new int[n];
int[] day = new int[n];Creates an array to store the day on which each character is replaced.
Build the inverse mapping
for (int i = 0; i < n; i++)
{
day[p[i]] = i + 1;
}Converts the permutation into replacement-day information for each index.
Initialize the answer
int ans = 0;If the string is already valid, the answer will remain zero.
Check adjacent characters
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]));
}
}Loop through every adjacent pair.
If the characters are equal, calculate the earliest day when either character is replaced.
Update the answer to the maximum of all such days.
Return the answer
return ans;Returns the minimum number of days needed.
10. Why does this algorithm work?
We can prove the solution using two observations.
Observation 1: Only initially equal adjacent pairs need to be fixed.
Replacing characters with '?' cannot create new invalid pairs because '?' is always allowed in an adjacent pair.
Observation 2: For every initially equal adjacent pair, the earliest day it becomes valid is the earlier replacement day of its two positions.
For a pair at indices i and i + 1, this is:
\[ \min(day[i],day[i+1]) \]
Since every initially invalid pair must become valid, we need to wait until the slowest pair is fixed. That is the maximum of these values.
Therefore, the algorithm correctly returns the minimum number of days.
11. Complexity analysis
Complexity | Explanation |
|---|---|
Time Complexity: O(n) | One loop builds the inverse permutation, and another checks all adjacent pairs. |
Auxiliary Space: O(n) | The |


Join the conversation! Your thoughts help the community grow.