Hi everyone!
So we have successfully coded the brute force solution of the given question.
Now let's take a simple string and walk through the code that we have written and see what's happening.
Now.
The indices of the various characters of the given string are these 0 to 7.
Right now let's say this function has been called and this string is passed to this function.
Now over here we have declared repeat.
And then we have this for loop.
When we enter this for loop initially I is equal to zero right.
And repeat.
The value of repeat is set to false.
And then we have the nested for loop over here.
And the value of j is initially zero.
And we are going to keep compare the.
Character in the given string at the ith index and at the jth index.
So that's what we're doing over here, right.
That's happening over here.
We're checking whether these two are equal.
And I should not be equal to J.
So in this case I is zero and j is zero.
So it will not enter over here.
And j will be incremented.
Now when j is equal to one we are going to check a and b right string at zero is uh a and string at
one is b.
So when j is equal to one.
Also we are not going to enter over here.
Now when j is equal to two J is over here pointing over here and I is over here.
And these two are equal right string at zero and string at two.
Both are a.
So these are equal.
So we enter in over here.
And repeat is set to true right.
So repeat is set to true.
Now when we come out of this for loop right when we have done all the iterations for J012 up to uh seven,
right up to uh seven, because the length over here is eight.
So this will keep going up to seven now when we come out of it, because we were able to enter in over
here, the value of repeat is true.
Right.
So we are going to check this whether repeat is false.
But the value of repeat is true.
So I'm just writing R for repeat in this case.
So repeat is true.
So it will not return anything.
And the value of I will be incremented which is happening over here in this for loop.
Right.
So I will be set to one.
And again we are going to set repeat to false.
And we will keep traversing the array with j.
So j will be zero, one, two, three etc..
So when I is one so I is pointing over here.
So we have the value b.
And then we are going to check all of these except j should not be one itself.
So this will fail because we have this condition written over here.
Right.
So it will not go inside.
So in these cases a again we get a at at zero we get a at two we get a at three we get R and at four
we get B.
And over here the values are same.
So string of one and string of four.
These are equal.
So again repeat is set to true at this point also.
And then when we keep the remaining values for j we go ahead for five six and seven.
And we come out of this for loop this one.
And we check over here whether repeat is false.
But at this point repeat has been set to true.
So it does not enter over here.
And again I is incremented and I becomes two.
And at this point when J is zero itself right.
This is where I is.
When j is zero itself, the values are the same.
So repeat is set to true and it keeps going till seven.
And when we come to this part repeat is again true.
So nothing is returned.
Now I is incremented to three.
Now at this point, when we traverse the remaining part of the array, we can see that in no place the
values are equal.
Therefore, repeat stays false, repeat stays false.
And when we come out of this for loop and have this check over here, whether repeat is false.
Yes, repeat is false in this case.
Therefore, we return this I which is equal to three.
So we return three.
And that's the answer to this question.
So that's what's happening in the code that we have written.
Now let's quickly look at the time and space complexity of our code.
We have seen that the time complexity of this solution is O of n square.
Again, that's because we have a nested for loop over here.
Right.
So in this case in the first for loop we are doing n operations.
And for each one operation of these n we are doing again an operations when we have the second for loop.
That's why the time complexity is n into n which is n square or o of n square.
And what about the space complexity?
We are not storing anything new over here, right?
We are not storing anything new.
And hence this algorithm runs in constant space or O of one space complexity.