09(March) Find the N-th character
09. Find the N-th character
The problem can be found at the following link: Question Link
My Approach
Initialization:
Start by determining the length of the input string
s
and store it in a variable calledlen
. This gives us the number of characters we need to manipulate.
Iteration Process:
Iterate
r
times, as specified, to apply the transformation on the string.For each iteration:
Temporary Storage:
Create a temporary string
temp
and copy the content of the original strings
into it. This preserves the original string for further iterations.
Character Transformation:
Traverse through each character of the original string
s
.For each character at position
j
, apply the following transformation:If the corresponding character in
temp
at positionj/2
is'0'
, sets[j]
to'0' + (j % 2)
.Otherwise, set
s[j]
to'1' - (j % 2)
.
Finalization:
After completing all iterations, the desired character will be at index
n
in the final transformed strings
.
This approach essentially iterates over the string s
multiple times, adjusting characters based on their position and values in a calculated manner. By following this process, we ensure that the desired character is appropriately modified according to the specified logic after the defined number of iterations.
Time and Auxiliary Space Complexity
Time Complexity :
O(r*|s|)
, as there are two nested loops.Auxiliary Space Complexity :
O(|s|)
, as the temporary string temp is of the same length as the input string.
Code (C++)
class Solution{
public:
char nthCharacter(string s, int r, int n)
{
int len=s.length();
for (int i=0;i<r;i++)
{
string temp=s;
for (int j=0;j<len;j++)
{
if (temp[j/2]=='0')
s[j]='0'+(j%2);
else s[j]='1'-(j%2);
}
}
return s[n];
}
};
Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Letβs make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated