Hi everyone, I hope you have given it a try to code the brute force solution which we discussed in
the previous video.
Now in this video, let's do it together.
Now for this we create a function and let's call it find non repeating character.
And this function takes in a string as input.
Now what we are going to do is let me just parallelly show that over here.
Now let's say that the input string is a, b, c, d.
B a d.
Let's say this is the input string for example.
Now what we're going to do is we will take one character, let's say over here that's a and compare
it with all the other characters to see whether it's repeating.
Right.
So there we need a variable to keep track whether the particular character which we are trying to see,
whether it's repeating or not, is actually repeating or not.
So we need a variable for that.
So let me declare a variable.
Repeat for that over here.
Now to traverse the given string we will use a for loop.
So let's say for let I is equal to zero I less than string dot length.
I plus plus.
So we will traverse the array with this for loop.
With this the traverse the string with this for loop.
All right.
Now when we enter this for loop let's set repeat to be equal to false.
So we assume that the character is non-repeating.
Now what we are going to do is we are going to traverse the string again with another for loop right.
So let's say for let j equal to zero j less than string dot length.
And J plus.
Plus.
Now, when we do this, we are going to compare the character at the ith and jth index of the given
string.
That's what you're going to do.
You're going to check if string at the ith index is equal to string at the jth index.
Now if this is true, it means that the string at the ith index is repeating.
Right.
For example, over here we are taking a and we are checking a with all the other characters over here.
Now when we reach over here, we can see that yes, it's repeating right now.
There's one more thing to be kept in mind over here.
Notice that over here I can take values from zero to string dot length minus one.
Similarly, j can take values from zero to string dot length minus one.
So if I is equal to j, always the characters at those two indices will be the same.
But that's not what we are trying to do over here.
Right?
When I have a of string of I as a, I need to check it with all the other indices, the characters at
the other indices.
So I need to add a condition over here that I should not be equal to j, so I not equal to j.
Now if this is true, that is when I is not equal to j, but still the character at the ith index in
the given string and the character at the jth index in the given string are equal.
If this is true, it means that the character at the ith index is repeating, so we will set.
Repeat is equal to true in this case.
So in this way we will traverse the array using j.
Now at the end of this for loop.
So when we come out of this for loop we will check whether repeat is true or false.
So if repeat at this point is equal to false.
It means that we checked string I with all the other characters in the given string by changing the
value of J, right.
So we kept checking this and we got the value of repeat as false.
That is, it never went inside over here.
So if repeat is false it means that.
The character at the ith index is non-repeating.
So all we need to do is we just need to return the index, which is I.
So that means the character at the ith index is non-repeating.
Now if we go through all the elements of the given string, let's say the input was a, b, c, d,
let's say the input was a a b b c, c.
So we take all the values of I so zero one, two, three, four five.
We keep doing that but we were never able to return anything.
So always return.
Repeat was true.
So it never returned anything.
So we come out of this for loop.
At this point we just need to return null because we clarified from the interviewer that if we are not
able to find a non-repeating character, we can just return null.
So at this point we will return null and we have our solution.
Now let's test our solution.
Let's say string.
Let's say a is equal to this string over here a a b b c c.
Now in this case we should get null as output.
So let's try to see whether that's working.
So console dot log find non-repeating character.
And we are passing a over here.
Now let's run this and see what we are getting.
So when we run this, we are having an error over here.
Okay.
So we did not write an equal to over here.
Okay.
So repeat is equal to true in this case.
So let's run it again.
All right.
So when we run it, we can see that we are getting null because all of these characters are repeating,
what if I add D over here and again run it.
So in this case I'm getting six over here 120123456.
That's the index of D.
So it's working right now let's try a few combinations.
What if it's a a and uh a b.
So in this case we should get zero one.
Which is the index where capital A is there.
So that's the correct answer.
We are getting the correct answer over here.
Now what if we have an empty string.
In this case we are getting null as output which is correct.
And what if we have a string?
Where we have a number, let's say we have one, two a, one, two, three, four, and then we have
one, two and a.
So it should give us the index of three which is 1230123.
So the answer in this case should be three.
Let's see.
And we are getting the answer as three.
So yes our algorithm is working.
Now let's take a look at this code.
Let's take a test case and walk through this code again.