1 00:00:01,200 --> 00:00:01,830 Hi, everyone. 2 00:00:01,859 --> 00:00:06,600 So today we are going to solve this question, find the length of the last word. 3 00:00:06,780 --> 00:00:10,440 OK, so the input will basically be a string. 4 00:00:11,500 --> 00:00:16,000 Input will basically be a string and we have to find the length of the last word. 5 00:00:16,030 --> 00:00:18,260 OK, so let us consider some example. 6 00:00:18,550 --> 00:00:19,870 So if I were in Putin's. 7 00:00:19,870 --> 00:00:20,240 Hello. 8 00:00:20,560 --> 00:00:22,190 So these are is OK. 9 00:00:22,920 --> 00:00:27,010 So if I were in Putin's hello space based world, then this is the last word. 10 00:00:27,090 --> 00:00:31,030 OK, so this is the last road and the land of this word is basically five. 11 00:00:31,110 --> 00:00:36,880 OK, so our output for this example will basically be a five the length of the last word, which is. 12 00:00:36,880 --> 00:00:39,250 Well, and it's Lantis five. 13 00:00:39,640 --> 00:00:44,800 OK, now this is an empty string so far and protesting, obviously the last word does not exist. 14 00:00:44,900 --> 00:00:49,190 So our output will be zero of the last word is basically zero. 15 00:00:49,480 --> 00:00:51,030 Now let us consider this input. 16 00:00:51,370 --> 00:00:54,030 So for this input, we have a space here. 17 00:00:54,040 --> 00:00:57,280 Then we have a word, then we have a word world. 18 00:00:57,490 --> 00:00:59,210 Then we have spaces again. 19 00:00:59,260 --> 00:01:00,910 OK, so which is the last word? 20 00:01:00,940 --> 00:01:07,030 So this is basically the last word and this is also the first word and this is also the last word. 21 00:01:07,270 --> 00:01:09,500 So basically the length to the last word is five. 22 00:01:09,520 --> 00:01:13,180 So our output for this input is basically a five. 23 00:01:13,690 --> 00:01:17,080 OK, so this is simply an implementation problem. 24 00:01:17,110 --> 00:01:20,530 OK, this is simply an implementation problem. 25 00:01:21,490 --> 00:01:25,240 So what we will do so we will start from the beginning of the string. 26 00:01:25,240 --> 00:01:27,690 So suppose this is a very large string. 27 00:01:27,850 --> 00:01:30,970 So what we have to do so we will start from the beginning of the string. 28 00:01:30,970 --> 00:01:35,270 We will like trade, we will maintain a variable count, which we will update. 29 00:01:35,560 --> 00:01:41,410 OK, so as soon as we encounter a space, as soon as we encounter space, so what we will do, we will 30 00:01:41,410 --> 00:01:43,120 make this count variable zero. 31 00:01:43,690 --> 00:01:44,010 OK. 32 00:01:44,140 --> 00:01:49,000 And then when we will encounter the variable again, we will encounter another character. 33 00:01:49,210 --> 00:01:52,130 We will increment we will update this current variable. 34 00:01:52,160 --> 00:01:54,910 OK, so this is simply an implementation problem. 35 00:01:54,940 --> 00:01:57,190 Now let us write the code, OK? 36 00:01:57,370 --> 00:01:58,530 We can write the code. 37 00:01:58,540 --> 00:02:00,700 Basically, this is a little problem. 38 00:02:00,730 --> 00:02:03,400 OK, so you can read the problem statement. 39 00:02:03,410 --> 00:02:09,100 So the input will basically be a string that can be empty spaces and we have to return the length of 40 00:02:09,100 --> 00:02:09,880 the last word. 41 00:02:09,910 --> 00:02:12,080 OK, so now let us write the code. 42 00:02:13,090 --> 00:02:17,560 So first, let us create a variable count which will store our answer. 43 00:02:17,590 --> 00:02:21,260 OK, the second variable will store the length of the last word. 44 00:02:21,640 --> 00:02:27,540 So what I'm doing here is so basically there are many ways to implement the solution for this problem. 45 00:02:27,940 --> 00:02:28,860 So what are the ways? 46 00:02:29,140 --> 00:02:30,490 So first, what can do? 47 00:02:30,520 --> 00:02:31,850 You can reverse the string. 48 00:02:32,680 --> 00:02:34,180 So if you will reverse the string. 49 00:02:34,510 --> 00:02:36,100 So the problem is very simple. 50 00:02:36,100 --> 00:02:38,100 You have to find the length of the first word. 51 00:02:38,790 --> 00:02:43,450 OK, you have to find the length of the first word, if you will, reverse the string. 52 00:02:43,720 --> 00:02:49,330 Second approach is basically start iterating from the end of the string, if you will, start iterating 53 00:02:49,330 --> 00:02:50,200 from the end of the string. 54 00:02:50,230 --> 00:02:50,870 What do you have to do? 55 00:02:50,920 --> 00:02:55,850 You have to find the length of the first word from the end, OK? 56 00:02:55,980 --> 00:03:00,370 You have to find the length of the first word from the end, if you will start updating from the end. 57 00:03:00,400 --> 00:03:00,790 OK. 58 00:03:02,790 --> 00:03:07,230 You have to find the Lenthall the first word from the end, if you will start outrating from the end 59 00:03:07,230 --> 00:03:12,930 of the string and the second the third way to implement this problem is basically start outrating from 60 00:03:12,930 --> 00:03:15,970 the first, OK, start dating from the first. 61 00:03:16,740 --> 00:03:18,810 So there are many ways to implement this problem. 62 00:03:18,840 --> 00:03:21,840 So let's say I am implementing this approach. 63 00:03:21,870 --> 00:03:24,580 OK, so these two approaches are very simple to implement. 64 00:03:25,200 --> 00:03:27,520 So this is a little bit difficult, I think. 65 00:03:27,540 --> 00:03:29,340 So let me implement this approach. 66 00:03:29,370 --> 00:03:29,700 OK. 67 00:03:31,640 --> 00:03:34,460 So I have a variable count, which is initially zero. 68 00:03:35,220 --> 00:03:38,570 Now let us find out the size of the string. 69 00:03:38,570 --> 00:03:39,920 So as nazis'. 70 00:03:42,950 --> 00:03:45,350 OK, so let us take a appointer. 71 00:03:46,790 --> 00:03:49,910 So we are dating from the start of the spring, so equal zero. 72 00:03:51,330 --> 00:03:53,820 So while I is listening. 73 00:03:54,150 --> 00:03:59,920 OK, so first of all, check whether this character is a space character or any other character. 74 00:04:00,480 --> 00:04:04,320 So if this character is not space. 75 00:04:05,890 --> 00:04:12,460 So if this is not space, I can safely do countless plays, OK, because this character is not a space, 76 00:04:13,150 --> 00:04:15,610 so I can do countless plays, I can do ableist. 77 00:04:15,640 --> 00:04:16,450 Let's move ahead. 78 00:04:16,519 --> 00:04:16,810 OK. 79 00:04:17,720 --> 00:04:18,890 Now in the Elzbieta. 80 00:04:20,220 --> 00:04:23,070 So the current character is basically be so. 81 00:04:24,310 --> 00:04:27,010 The current character is Aspies. 82 00:04:29,090 --> 00:04:33,050 OK, so while the current characters are space, so. 83 00:04:34,780 --> 00:04:36,490 While the current character. 84 00:04:38,290 --> 00:04:41,050 Is space, what can I do, I can do a plus plus. 85 00:04:41,790 --> 00:04:43,300 OK, so what I'm doing here is. 86 00:04:44,930 --> 00:04:47,570 So what I'm doing here is, for example, you have a string hello? 87 00:04:48,850 --> 00:04:53,380 Then you have space and then you have multiple spaces and then you have the word blue. 88 00:04:53,420 --> 00:04:56,160 OK, so if you will encounter a character. 89 00:04:56,170 --> 00:05:03,190 So at this point, if I if I am going to space, so while that while the character space I am moving, 90 00:05:03,530 --> 00:05:04,290 I had to move ahead. 91 00:05:04,330 --> 00:05:05,830 Move ahead, move ahead, move ahead. 92 00:05:05,830 --> 00:05:06,790 Move ahead and hit. 93 00:05:06,790 --> 00:05:07,540 I will stop. 94 00:05:07,550 --> 00:05:10,680 OK, because this is not space OK. 95 00:05:10,960 --> 00:05:16,720 But while writing this code you have to also write the condition I is less than an otherwise you may 96 00:05:16,720 --> 00:05:17,200 get around. 97 00:05:17,770 --> 00:05:18,130 OK. 98 00:05:19,180 --> 00:05:22,870 We have to right this condition, otherwise you can't get segmentation fault, although you can get 99 00:05:22,870 --> 00:05:23,620 runtime added. 100 00:05:26,720 --> 00:05:29,730 So now let us ride that condition also. 101 00:05:30,260 --> 00:05:38,900 So while I is less than in and basically the characters space, what they can do, you can do a plus 102 00:05:38,900 --> 00:05:39,200 plus. 103 00:05:40,190 --> 00:05:42,980 Now, there are two ways to break this wide loop. 104 00:05:43,010 --> 00:05:45,650 First, they become sequels to end. 105 00:05:45,970 --> 00:05:47,600 OK, so if. 106 00:05:49,190 --> 00:05:53,240 I become so close to end, that means we reach the end of the string. 107 00:05:53,270 --> 00:05:59,120 OK, so we have reached the end of this thing, evidence on this besant in the variable count so you 108 00:05:59,120 --> 00:05:59,990 can return count. 109 00:06:00,020 --> 00:06:03,770 OK, so we have reached the end of the string. 110 00:06:05,330 --> 00:06:11,120 OK, so and our answer was the variable count, so I would have done count now in the L'ESPRIT. 111 00:06:12,390 --> 00:06:15,760 I have not reached the end of the string and there are still characters. 112 00:06:15,780 --> 00:06:19,170 So what I will do, I will make count equals zero. 113 00:06:20,160 --> 00:06:26,490 OK, so the meaning of this is basically you have you didn't reach the end of the string and there are 114 00:06:26,490 --> 00:06:27,280 still characters. 115 00:06:27,330 --> 00:06:32,220 So basically we have to make the current variable zero because there are still characters present. 116 00:06:32,250 --> 00:06:33,960 OK, and finally. 117 00:06:35,050 --> 00:06:40,660 What we will do so when this valuable land, I can return my answer, which is present in the current 118 00:06:40,660 --> 00:06:41,050 variable. 119 00:06:41,110 --> 00:06:42,870 OK, so now let us first. 120 00:06:42,910 --> 00:06:45,550 Then I will call and then we will try to submit our code, OK? 121 00:06:49,010 --> 00:06:50,420 Now only does some good. 122 00:06:54,290 --> 00:07:00,640 OK, so Al Gore is looking fine now let us discuss the time and the space complexity for our solution. 123 00:07:01,070 --> 00:07:04,790 So as we can see clearly, we are just creating only a few variables. 124 00:07:04,790 --> 00:07:07,070 So the space complexity is basically constant. 125 00:07:07,340 --> 00:07:14,050 So this is our one big off one and the time complexities linear because we are just getting over the 126 00:07:14,070 --> 00:07:14,350 string. 127 00:07:14,360 --> 00:07:17,240 OK, so we are simply iterating over the string. 128 00:07:17,450 --> 00:07:22,010 So the time complexity is big off and and the space complexities are the rough one. 129 00:07:22,070 --> 00:07:25,220 OK, so if you have any doubt in this question, you can ask me. 130 00:07:25,220 --> 00:07:25,910 OK, thank you.