1
00:00:00,940 --> 00:00:06,280
Hi everyone, I hope you have given it a try to code the brute force solution which we discussed in

2
00:00:06,280 --> 00:00:07,510
the previous video.

3
00:00:07,510 --> 00:00:09,940
Now in this video, let's do it together.

4
00:00:09,940 --> 00:00:17,890
Now for this we create a function and let's call it find non repeating character.

5
00:00:19,750 --> 00:00:23,080
And this function takes in a string as input.

6
00:00:24,120 --> 00:00:29,550
Now what we are going to do is let me just parallelly show that over here.

7
00:00:29,580 --> 00:00:33,720
Now let's say that the input string is a, b, c, d.

8
00:00:34,710 --> 00:00:37,530
B a d.

9
00:00:37,560 --> 00:00:39,900
Let's say this is the input string for example.

10
00:00:39,900 --> 00:00:45,330
Now what we're going to do is we will take one character, let's say over here that's a and compare

11
00:00:45,330 --> 00:00:48,270
it with all the other characters to see whether it's repeating.

12
00:00:48,270 --> 00:00:48,570
Right.

13
00:00:48,570 --> 00:00:53,850
So there we need a variable to keep track whether the particular character which we are trying to see,

14
00:00:53,850 --> 00:00:56,640
whether it's repeating or not, is actually repeating or not.

15
00:00:56,640 --> 00:00:57,990
So we need a variable for that.

16
00:00:57,990 --> 00:01:00,510
So let me declare a variable.

17
00:01:00,510 --> 00:01:02,100
Repeat for that over here.

18
00:01:02,580 --> 00:01:06,180
Now to traverse the given string we will use a for loop.

19
00:01:06,180 --> 00:01:12,540
So let's say for let I is equal to zero I less than string dot length.

20
00:01:13,530 --> 00:01:14,760
I plus plus.

21
00:01:15,210 --> 00:01:17,850
So we will traverse the array with this for loop.

22
00:01:17,850 --> 00:01:20,940
With this the traverse the string with this for loop.

23
00:01:20,940 --> 00:01:21,540
All right.

24
00:01:21,570 --> 00:01:27,120
Now when we enter this for loop let's set repeat to be equal to false.

25
00:01:27,120 --> 00:01:30,750
So we assume that the character is non-repeating.

26
00:01:30,750 --> 00:01:36,780
Now what we are going to do is we are going to traverse the string again with another for loop right.

27
00:01:36,780 --> 00:01:44,520
So let's say for let j equal to zero j less than string dot length.

28
00:01:45,720 --> 00:01:46,770
And J plus.

29
00:01:46,770 --> 00:01:47,430
Plus.

30
00:01:48,050 --> 00:01:55,490
Now, when we do this, we are going to compare the character at the ith and jth index of the given

31
00:01:55,490 --> 00:01:55,940
string.

32
00:01:55,970 --> 00:01:57,110
That's what you're going to do.

33
00:01:57,140 --> 00:02:04,730
You're going to check if string at the ith index is equal to string at the jth index.

34
00:02:05,060 --> 00:02:10,010
Now if this is true, it means that the string at the ith index is repeating.

35
00:02:10,010 --> 00:02:10,310
Right.

36
00:02:10,310 --> 00:02:16,220
For example, over here we are taking a and we are checking a with all the other characters over here.

37
00:02:16,220 --> 00:02:20,450
Now when we reach over here, we can see that yes, it's repeating right now.

38
00:02:20,450 --> 00:02:23,300
There's one more thing to be kept in mind over here.

39
00:02:23,390 --> 00:02:28,640
Notice that over here I can take values from zero to string dot length minus one.

40
00:02:28,640 --> 00:02:32,630
Similarly, j can take values from zero to string dot length minus one.

41
00:02:32,630 --> 00:02:38,900
So if I is equal to j, always the characters at those two indices will be the same.

42
00:02:38,900 --> 00:02:40,700
But that's not what we are trying to do over here.

43
00:02:40,700 --> 00:02:41,090
Right?

44
00:02:41,090 --> 00:02:48,530
When I have a of string of I as a, I need to check it with all the other indices, the characters at

45
00:02:48,530 --> 00:02:49,460
the other indices.

46
00:02:49,460 --> 00:02:55,970
So I need to add a condition over here that I should not be equal to j, so I not equal to j.

47
00:02:56,300 --> 00:03:02,930
Now if this is true, that is when I is not equal to j, but still the character at the ith index in

48
00:03:02,930 --> 00:03:07,280
the given string and the character at the jth index in the given string are equal.

49
00:03:07,280 --> 00:03:13,760
If this is true, it means that the character at the ith index is repeating, so we will set.

50
00:03:13,760 --> 00:03:15,950
Repeat is equal to true in this case.

51
00:03:16,220 --> 00:03:20,360
So in this way we will traverse the array using j.

52
00:03:20,390 --> 00:03:22,310
Now at the end of this for loop.

53
00:03:22,310 --> 00:03:27,110
So when we come out of this for loop we will check whether repeat is true or false.

54
00:03:27,110 --> 00:03:31,670
So if repeat at this point is equal to false.

55
00:03:32,850 --> 00:03:40,560
It means that we checked string I with all the other characters in the given string by changing the

56
00:03:40,560 --> 00:03:41,460
value of J, right.

57
00:03:41,460 --> 00:03:45,630
So we kept checking this and we got the value of repeat as false.

58
00:03:45,630 --> 00:03:48,120
That is, it never went inside over here.

59
00:03:48,120 --> 00:03:51,120
So if repeat is false it means that.

60
00:03:51,750 --> 00:03:55,440
The character at the ith index is non-repeating.

61
00:03:55,440 --> 00:03:59,940
So all we need to do is we just need to return the index, which is I.

62
00:03:59,970 --> 00:04:03,330
So that means the character at the ith index is non-repeating.

63
00:04:03,360 --> 00:04:11,250
Now if we go through all the elements of the given string, let's say the input was a, b, c, d,

64
00:04:11,310 --> 00:04:14,070
let's say the input was a a b b c, c.

65
00:04:14,160 --> 00:04:19,170
So we take all the values of I so zero one, two, three, four five.

66
00:04:19,200 --> 00:04:22,980
We keep doing that but we were never able to return anything.

67
00:04:22,980 --> 00:04:24,330
So always return.

68
00:04:24,330 --> 00:04:25,200
Repeat was true.

69
00:04:25,200 --> 00:04:26,910
So it never returned anything.

70
00:04:26,910 --> 00:04:28,620
So we come out of this for loop.

71
00:04:28,620 --> 00:04:35,190
At this point we just need to return null because we clarified from the interviewer that if we are not

72
00:04:35,190 --> 00:04:38,850
able to find a non-repeating character, we can just return null.

73
00:04:38,850 --> 00:04:43,200
So at this point we will return null and we have our solution.

74
00:04:43,200 --> 00:04:45,090
Now let's test our solution.

75
00:04:45,090 --> 00:04:47,340
Let's say string.

76
00:04:47,340 --> 00:04:51,360
Let's say a is equal to this string over here a a b b c c.

77
00:04:51,360 --> 00:04:54,240
Now in this case we should get null as output.

78
00:04:54,240 --> 00:04:56,580
So let's try to see whether that's working.

79
00:04:56,580 --> 00:05:00,600
So console dot log find non-repeating character.

80
00:05:00,600 --> 00:05:02,340
And we are passing a over here.

81
00:05:02,880 --> 00:05:05,760
Now let's run this and see what we are getting.

82
00:05:07,280 --> 00:05:11,120
So when we run this, we are having an error over here.

83
00:05:13,920 --> 00:05:14,250
Okay.

84
00:05:14,250 --> 00:05:16,320
So we did not write an equal to over here.

85
00:05:16,350 --> 00:05:16,740
Okay.

86
00:05:16,740 --> 00:05:18,990
So repeat is equal to true in this case.

87
00:05:18,990 --> 00:05:20,310
So let's run it again.

88
00:05:22,850 --> 00:05:23,240
All right.

89
00:05:23,240 --> 00:05:29,360
So when we run it, we can see that we are getting null because all of these characters are repeating,

90
00:05:29,360 --> 00:05:32,030
what if I add D over here and again run it.

91
00:05:32,030 --> 00:05:37,250
So in this case I'm getting six over here 120123456.

92
00:05:37,250 --> 00:05:38,690
That's the index of D.

93
00:05:38,690 --> 00:05:41,840
So it's working right now let's try a few combinations.

94
00:05:41,840 --> 00:05:46,250
What if it's a a and uh a b.

95
00:05:46,250 --> 00:05:49,160
So in this case we should get zero one.

96
00:05:49,810 --> 00:05:52,270
Which is the index where capital A is there.

97
00:05:52,270 --> 00:05:53,350
So that's the correct answer.

98
00:05:53,350 --> 00:05:55,300
We are getting the correct answer over here.

99
00:05:55,300 --> 00:05:57,880
Now what if we have an empty string.

100
00:05:58,480 --> 00:06:01,570
In this case we are getting null as output which is correct.

101
00:06:01,570 --> 00:06:04,180
And what if we have a string?

102
00:06:04,930 --> 00:06:11,350
Where we have a number, let's say we have one, two a, one, two, three, four, and then we have

103
00:06:11,350 --> 00:06:12,580
one, two and a.

104
00:06:12,580 --> 00:06:18,370
So it should give us the index of three which is 1230123.

105
00:06:18,370 --> 00:06:20,680
So the answer in this case should be three.

106
00:06:20,680 --> 00:06:21,310
Let's see.

107
00:06:21,310 --> 00:06:22,780
And we are getting the answer as three.

108
00:06:22,780 --> 00:06:25,150
So yes our algorithm is working.

109
00:06:25,150 --> 00:06:28,120
Now let's take a look at this code.

110
00:06:28,120 --> 00:06:30,640
Let's take a test case and walk through this code again.
