1. Problem understanding
We are given a string s and an integer pos. We need to find the character at the pos-th position (1-based indexing) in an infinitely generated string.
In every step i, the string is updated using this rule:
\[ S_{new}=S+\underbrace{\$\$\cdots\$}_{i\text{ times}}+reverse(S) \]
That means:
- Keep the current string.
- Append
idollar ($) characters. - Append the reverse of the current string.
We must return the character at the given position without actually building the infinite string.
2. Example
Given:
s = "12345", pos = 10Initially:
12345
After Step 1:
12345$54321
| Position | Character |
|---|---|
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 5 | 5 |
| 6 | $ |
| 7 | 5 |
| 8 | 4 |
| 9 | 3 |
| 10 | 2 |
| 11 | 1 |
Output: 2
The answer is 2.
3. Why not build the string?
Suppose the initial string has length 5.
| Step | String length |
|---|---|
| Initial | 5 |
| Step 1 | 11 |
| Step 2 | 24 |
| Step 3 | 51 |
| Step 4 | 105 |
The string grows very quickly. With pos up to \(10^9\), constructing and storing the string is inefficient.
Instead, we use mathematical length calculation and backward position mapping.
4. Core concept – Work backwards instead of forwards
At every expansion, the string has three parts:
Part 1: Previous string
Part 2: Added $ characters
Part 3: Reversed previous string
For example, if the previous string is "12345" and Step 1 adds one $:
Previous string: 12345
Dollar section: $
Reverse section: 54321The new string is:
12345$54321We can locate the required position by checking which section it belongs to.
Three possible cases
Case 1: Position is in the previous string
If pos is within the previous string's length, we don't need to change the position. We simply move to the previous expansion level.
Case 2: Position is in the $ section
If the position falls between the previous string and its reversed copy, the answer is $.
Case 3: Position is in the reversed string
We convert the position into the corresponding position in the previous string using:
\[ \boxed{p_{\text{new}}=L-p+1} \]
Where:
- \(L\) = current string length
- \(p\) = current position
- \(p_{\text{new}}\) = mapped position in the previous string
This allows us to move backwards without constructing or reversing strings.
5. C# code
class Solution
{
public char posInfinite(string s, int pos)
{
long length = s.Length;
long p = pos;
long step = 1;
// Find the expansion level containing pos
while (length < p)
{
length = 2 * length + step;
step++;
}
// Trace the position backwards
step--;
while (step > 0)
{
long previousLength = (length - step) / 2;
// Position is in the original string
if (p <= previousLength)
{
length = previousLength;
step--;
continue;
}
// Position is in the '$' section
if (p <= previousLength + step)
{
return '$';
}
// Position is in the reversed string
p = length - p + 1;
length = previousLength;
step--;
}
// Position is in the initial string
return s[(int)p - 1];
}
}
Join the conversation! Your thoughts help the community grow.