1 00:00:02,469 --> 00:00:03,140 Hi, everyone. 2 00:00:03,160 --> 00:00:06,490 So in this video, we are going to solve this question, reverse words. 3 00:00:07,590 --> 00:00:13,620 He lost words in a string, so consider this a string, so basically be a string and we have to reverse 4 00:00:13,620 --> 00:00:19,010 the words so the output will be blue, then is then sky then. 5 00:00:19,260 --> 00:00:21,000 So blue is sky. 6 00:00:22,330 --> 00:00:26,030 The OK, so this is the output now consider coding is fun. 7 00:00:26,200 --> 00:00:31,670 Now let us reverse the words so it will be our output will be fun is coding. 8 00:00:32,560 --> 00:00:34,080 So I hope you understood the question. 9 00:00:34,630 --> 00:00:36,760 So this question is basically present. 10 00:00:36,760 --> 00:00:37,360 Only told. 11 00:00:37,360 --> 00:00:39,790 OK, reversing words in a string. 12 00:00:41,060 --> 00:00:47,540 So what we have to do, so the input string, we have to reverse the interesting word by word, so for 13 00:00:47,540 --> 00:00:50,920 the input, the sky is blue, output will be Bluey's Kaida. 14 00:00:51,080 --> 00:00:53,510 So we are reversing the string word by word. 15 00:00:54,110 --> 00:00:55,850 OK, let us take somewhat examples. 16 00:00:57,510 --> 00:00:59,250 So consider these two examples. 17 00:00:59,280 --> 00:01:03,210 OK, so Helliwell, so I have space then haloed in space then. 18 00:01:03,570 --> 00:01:06,210 OK, so after reversing this will be world. 19 00:01:07,310 --> 00:01:11,170 Then a space, then hello, and then basically space. 20 00:01:11,240 --> 00:01:14,570 OK, so you can see so in the reverse string. 21 00:01:15,790 --> 00:01:19,480 Leading and telling the leading and telling spitters should not be dead. 22 00:01:19,510 --> 00:01:27,220 OK, so this is the trailing space, so we will eliminate this space, OK, without this WorldSpace. 23 00:01:27,220 --> 00:01:27,550 Hello. 24 00:01:28,090 --> 00:01:31,280 So this space thing is basically secondary. 25 00:01:31,300 --> 00:01:33,640 OK, so this space thing is basically secondary. 26 00:01:34,300 --> 00:01:36,390 You don't need to worry about this big thing. 27 00:01:36,400 --> 00:01:37,610 We can handle it later on. 28 00:01:37,730 --> 00:01:40,180 OK, first is how to build the logic. 29 00:01:40,720 --> 00:01:41,980 Let us focus on these examples. 30 00:01:42,020 --> 00:01:43,480 The will be example. 31 00:01:44,350 --> 00:01:44,770 Good. 32 00:01:45,990 --> 00:01:52,020 A OK, so this will be the output again, so here there are multiple spaces, there were two spaces, 33 00:01:52,020 --> 00:01:54,060 but in the output there will be only one space. 34 00:01:54,250 --> 00:01:58,350 So we have to reduce the multiple spaces between two worlds into a single space. 35 00:01:59,580 --> 00:02:01,650 So do not worry about spaces for now. 36 00:02:01,950 --> 00:02:03,760 First, let us try to build the logic. 37 00:02:03,780 --> 00:02:06,450 OK, so this space thing is basically secondary. 38 00:02:08,039 --> 00:02:12,550 Now, the logic is very, very simple, this is just the implementation problem. 39 00:02:12,990 --> 00:02:16,370 So what we will do so we'll solve this question in two steps. 40 00:02:16,920 --> 00:02:20,120 So the first step is basically what we will do. 41 00:02:20,130 --> 00:02:21,450 We will reverse each word. 42 00:02:22,660 --> 00:02:27,460 OK, we will reverse each word, and the second step for solving this question is basically reverse 43 00:02:27,460 --> 00:02:29,880 the complete string universe, complete string. 44 00:02:31,300 --> 00:02:33,770 So let us consider this as an example. 45 00:02:33,790 --> 00:02:39,310 OK, so what will reversing each word so after reversing the each word so reverse so it will become 46 00:02:40,620 --> 00:02:41,170 reverse. 47 00:02:41,570 --> 00:02:43,740 So this will become like this. 48 00:02:44,170 --> 00:02:48,850 So this will be a and this will be you Elby be OK. 49 00:02:49,030 --> 00:02:49,930 First step is done. 50 00:02:49,940 --> 00:02:52,030 This was the first step of reversing each word. 51 00:02:52,750 --> 00:02:55,400 Now the second step is basically reverse the complete string. 52 00:02:55,500 --> 00:02:57,730 OK, I will reverse the complete string. 53 00:02:57,740 --> 00:03:00,580 So this will be B, you OK? 54 00:03:00,850 --> 00:03:02,690 Reverse the computing's will be. 55 00:03:03,070 --> 00:03:06,580 So this is B and you then I. 56 00:03:06,610 --> 00:03:14,160 S then why I ask why then the E and you can see we will match the output. 57 00:03:15,190 --> 00:03:16,120 So very, very simple. 58 00:03:16,630 --> 00:03:18,160 We have to follow these two steps. 59 00:03:18,910 --> 00:03:23,530 Now let us for this string, let us perform the first step, reverse each word. 60 00:03:23,560 --> 00:03:24,580 So this will be G. 61 00:03:25,730 --> 00:03:37,070 And I d o c then I have s i then nd you f so the first step is then reversed. 62 00:03:37,080 --> 00:03:39,470 Each word second is reversed, a complete string. 63 00:03:39,470 --> 00:03:41,380 OK, reverse the complete string. 64 00:03:41,660 --> 00:03:44,690 So this will be f you and then I. 65 00:03:44,690 --> 00:03:47,360 S then C or D. 66 00:03:48,290 --> 00:03:49,810 I kn g. 67 00:03:49,890 --> 00:03:50,780 OK, you can see. 68 00:03:52,760 --> 00:03:53,550 Very, very simple. 69 00:03:54,200 --> 00:03:59,630 First, we have to reverse the each word and then we will reverse the competing and how to reverse. 70 00:03:59,990 --> 00:04:01,720 So basically we know how to reverse. 71 00:04:01,730 --> 00:04:09,260 So I will take a start point that I will take and pointer I will start I will say at the start and end, 72 00:04:09,260 --> 00:04:10,460 I will do start placeless. 73 00:04:10,490 --> 00:04:11,730 I will do end minus minus. 74 00:04:11,750 --> 00:04:14,630 OK, so we have done this reverse thing. 75 00:04:14,660 --> 00:04:16,630 OK, so this is very, very simple. 76 00:04:16,640 --> 00:04:19,100 What we do, we will trade over the string. 77 00:04:19,579 --> 00:04:20,690 We will perform the step. 78 00:04:20,690 --> 00:04:26,720 Number one, after performing the step number one, we will get the string and after the string we will 79 00:04:26,720 --> 00:04:28,350 reverse the complete string, OK. 80 00:04:29,030 --> 00:04:32,720 Now after getting this string, after getting the string. 81 00:04:32,720 --> 00:04:36,080 But we have to do we have to handle the space conditions. 82 00:04:36,090 --> 00:04:39,500 OK, so this space condition is basically secondary. 83 00:04:41,780 --> 00:04:49,250 After getting this string, we will modify the string so that the space conditions are met that specified 84 00:04:49,250 --> 00:04:52,400 and negotiated, for example, they should not be leading and trading spaces. 85 00:04:52,700 --> 00:04:54,680 So we will remove leading and trading spaces. 86 00:04:54,680 --> 00:04:56,150 But that is second problem. 87 00:04:56,250 --> 00:04:58,490 OK, so first, let us implement these two. 88 00:04:59,600 --> 00:05:05,090 Reversed each word and then reversed the computer string so we know how to reverse the computer thing, 89 00:05:05,090 --> 00:05:06,080 that is very, very simple. 90 00:05:06,110 --> 00:05:08,180 Now let us see how we can reverse each word. 91 00:05:08,360 --> 00:05:09,830 OK, so consider this a string. 92 00:05:10,350 --> 00:05:11,360 The sky is blue. 93 00:05:11,900 --> 00:05:14,750 The space sky space is blue. 94 00:05:14,780 --> 00:05:18,940 OK, so we know how to reverse the computer thing. 95 00:05:18,950 --> 00:05:20,930 Basically, we know how to do second step. 96 00:05:20,960 --> 00:05:23,300 We have a really solid reversing this string. 97 00:05:23,630 --> 00:05:28,100 OK, now let's see how we can perform the first step, which is basically reversing the words. 98 00:05:28,430 --> 00:05:33,020 OK, so what we will do, I will take two pointer begin pointer and the pointer. 99 00:05:33,020 --> 00:05:39,720 Initially this is my pointer and this is my end point that I will iterate over this string. 100 00:05:39,740 --> 00:05:41,430 OK, so end point related. 101 00:05:41,720 --> 00:05:47,900 So I will come here and they will come here then and they will come here space. 102 00:05:48,090 --> 00:05:50,380 So as soon as the end pointer. 103 00:05:50,420 --> 00:05:53,000 OK, as soon as the end point rent control space. 104 00:05:53,000 --> 00:05:55,130 What we will do, I will reverse the word. 105 00:05:55,910 --> 00:05:57,860 I will call the function reverse. 106 00:05:58,520 --> 00:05:59,900 OK, I will give the string. 107 00:06:00,410 --> 00:06:06,210 Let's do this thing is e I will give the pointer and the end point will be and minus one. 108 00:06:06,230 --> 00:06:10,310 OK, so this will be end and this but president here. 109 00:06:10,320 --> 00:06:12,000 But I have two plus and minus one. 110 00:06:12,020 --> 00:06:14,870 OK, so this word will be reversed then. 111 00:06:15,140 --> 00:06:15,770 What will do. 112 00:06:15,770 --> 00:06:16,830 I will update begin. 113 00:06:17,090 --> 00:06:19,470 So begin will be and plus one. 114 00:06:19,520 --> 00:06:20,930 OK so begin will come here. 115 00:06:22,100 --> 00:06:22,420 OK. 116 00:06:22,430 --> 00:06:23,940 Or you can watch what you can do. 117 00:06:24,260 --> 00:06:25,820 So first what you will do. 118 00:06:25,820 --> 00:06:31,010 You will do and placeless so and will come here and then you will do begin equals end. 119 00:06:31,040 --> 00:06:35,150 OK so begin end the out here again. 120 00:06:35,150 --> 00:06:40,070 I will do the same step and will come here and will come here then and will encounter space. 121 00:06:40,610 --> 00:06:48,590 So as soon as the end encounter space, as soon as the space I will call the reverse function, I will 122 00:06:48,590 --> 00:06:50,340 pass the string, I will pass the pointer. 123 00:06:50,360 --> 00:06:53,470 So this is the pointer I will pass and minus one. 124 00:06:53,480 --> 00:06:57,860 So this is and I will pass and the minus one then the sky will be reversed. 125 00:06:57,920 --> 00:06:59,270 OK, this guy will be reversed. 126 00:06:59,690 --> 00:07:02,960 Then again I will do L'Espresso and will come here. 127 00:07:04,160 --> 00:07:06,560 Begin equals and to begin will come here then. 128 00:07:06,880 --> 00:07:08,060 Then and will reach here. 129 00:07:08,060 --> 00:07:10,960 I will reverse the string then I will reverse this string. 130 00:07:11,330 --> 00:07:12,080 So finally. 131 00:07:13,070 --> 00:07:16,670 After this cold, what will happen, so this will be. 132 00:07:18,110 --> 00:07:22,760 Should then space, then why is that space, then? 133 00:07:22,760 --> 00:07:28,860 S I then space, then you and me, OK, very, very simple. 134 00:07:29,510 --> 00:07:32,660 So what we have to do so as soon as we encounter a space. 135 00:07:34,230 --> 00:07:38,420 What is the meaning of word for word after that word, we will encounter a space. 136 00:07:38,660 --> 00:07:43,980 OK, so as soon as we encounter a space, as soon as we encounter a space, we will reverse the word. 137 00:07:44,970 --> 00:07:45,870 Very, very simple. 138 00:07:46,190 --> 00:07:48,250 OK, so we will reverse all the words. 139 00:07:48,450 --> 00:07:51,320 So these are spaces we will reverse all over. 140 00:07:51,330 --> 00:07:53,130 The special condition is basically this one. 141 00:07:53,520 --> 00:07:58,770 OK, what we are doing, as soon as we are encountering space, then we are reversing, reversing the 142 00:07:58,770 --> 00:08:00,450 word mode for the last word. 143 00:08:00,570 --> 00:08:01,560 Space is not there. 144 00:08:01,590 --> 00:08:03,230 OK, space is not there. 145 00:08:03,450 --> 00:08:05,770 So we will explicitly reverse the last word. 146 00:08:05,820 --> 00:08:09,240 OK, last word is basically the special condition, right? 147 00:08:09,250 --> 00:08:14,130 Especially because I am saying as soon as we encounter a space, we will reverse the word OK. 148 00:08:14,350 --> 00:08:17,130 As soon as I will encounter the space, I will reverse this word. 149 00:08:17,340 --> 00:08:21,330 As soon as I will encounter the space, I will reverse this mode for the last word. 150 00:08:21,510 --> 00:08:22,590 Space is not there. 151 00:08:22,720 --> 00:08:25,890 OK, so if the space is not there, that's why this is a special case. 152 00:08:25,890 --> 00:08:27,750 I have to reverse the last word myself. 153 00:08:28,290 --> 00:08:28,620 OK. 154 00:08:30,010 --> 00:08:35,770 So after the first episode, reversing each word, the second step is basically reversing the complete 155 00:08:35,770 --> 00:08:36,190 string. 156 00:08:36,610 --> 00:08:39,250 OK, I will reverse the complete string. 157 00:08:40,370 --> 00:08:45,230 OK, I will reverse the building, which we have already done, and the third step, which is basically 158 00:08:45,230 --> 00:08:46,310 handling spaces. 159 00:08:46,370 --> 00:08:49,100 OK, so this handling space is very, very simple. 160 00:08:49,610 --> 00:08:52,280 So now let us write the code for first and second. 161 00:08:54,980 --> 00:09:02,470 So I have to return this updated string, so this is string as now first let us write the reverse function. 162 00:09:03,530 --> 00:09:05,750 So the return type of the reverse function will be void. 163 00:09:05,780 --> 00:09:07,170 It will not reverse anything. 164 00:09:07,310 --> 00:09:08,680 It will not return anything. 165 00:09:09,440 --> 00:09:12,590 It will simply modify the given string. 166 00:09:13,040 --> 00:09:14,300 So very important here. 167 00:09:14,300 --> 00:09:16,160 We have to take the string Bio-Reference. 168 00:09:17,000 --> 00:09:22,280 We have to take string Bio-Reference only it is advised by defence and simple as best by default. 169 00:09:22,310 --> 00:09:25,850 OK, only that is by definition C++ by default. 170 00:09:25,850 --> 00:09:27,710 Otherwise you have to write this operator. 171 00:09:27,720 --> 00:09:32,960 OK, I have to pass the string Bio-Reference because I'm going to do the changes in string only, ok. 172 00:09:34,160 --> 00:09:36,770 I will take the beginning and the end point at. 173 00:09:38,050 --> 00:09:44,950 Then I have a very standard code, so we have seen this code many times while being on this list in 174 00:09:44,950 --> 00:09:46,490 order to end what we have to do. 175 00:09:46,750 --> 00:09:48,310 I have to do the slapping part. 176 00:09:48,310 --> 00:09:52,120 So String, I will begin and end. 177 00:09:53,300 --> 00:09:57,830 OK, so I'm stepping begin and end very soon, it could then begin placeless. 178 00:09:59,780 --> 00:10:02,600 And minus minus, so this is very standard. 179 00:10:02,840 --> 00:10:03,180 OK. 180 00:10:03,230 --> 00:10:04,820 We have seen this too many times. 181 00:10:06,450 --> 00:10:12,200 Now, all we have to do, let us perform step one, which is reversing the words. 182 00:10:12,240 --> 00:10:15,170 OK, so step one, reversing the words. 183 00:10:15,600 --> 00:10:17,950 So as we discussed, we will take two pointers. 184 00:10:18,030 --> 00:10:19,490 OK, you can see yourself. 185 00:10:19,800 --> 00:10:22,110 We are taking two pointers begin and end point there. 186 00:10:23,950 --> 00:10:31,120 OK, so initially begin with the president at zero index similarly and will also be present at index 187 00:10:31,120 --> 00:10:31,450 zero. 188 00:10:31,870 --> 00:10:36,090 Simple, then I told you we will address the end point. 189 00:10:36,190 --> 00:10:41,830 OK, you can see yourself the end point that is moving ahead to the speech. 190 00:10:41,890 --> 00:10:42,250 OK. 191 00:10:43,840 --> 00:10:50,230 So and and is basically let's change the name, let's name it a. 192 00:10:51,310 --> 00:10:54,850 So while and is basically less than eroticize. 193 00:10:59,710 --> 00:11:01,990 OK, so what we have to do. 194 00:11:03,900 --> 00:11:04,680 So if. 195 00:11:06,240 --> 00:11:13,980 The end blender is basically encountering a space, so as discussed, what we have to do, we have to 196 00:11:13,980 --> 00:11:21,000 call the reverse function, OK, you can see yourself whenever the end pointer is basically space. 197 00:11:21,950 --> 00:11:28,010 I have to call the reverse function, I have to pass string pointer and a minus one, OK, and is basically 198 00:11:28,250 --> 00:11:30,480 space, so I will pass and a minus one. 199 00:11:30,770 --> 00:11:32,130 So this is there end of minus one. 200 00:11:32,210 --> 00:11:33,680 OK, this is begin. 201 00:11:33,690 --> 00:11:34,850 This is an A minus one. 202 00:11:35,660 --> 00:11:36,980 I will end placeless. 203 00:11:37,960 --> 00:11:41,780 So that and will come here and then I will begin equals end, OK? 204 00:11:41,830 --> 00:11:42,370 Simple. 205 00:11:44,570 --> 00:11:50,600 I'm calling the reverse function, I am putting the string, then I have to pass the beacon, I have 206 00:11:50,600 --> 00:11:52,110 to pass and minus one. 207 00:11:52,160 --> 00:11:59,330 OK, I have to pass and my nisson then I will do and placeless and then I will begin equals goaltend. 208 00:12:01,030 --> 00:12:09,070 Simple now in the LS part, the current characters in Artspace, then I will do placeless and will simply 209 00:12:09,070 --> 00:12:09,640 move ahead. 210 00:12:10,120 --> 00:12:12,380 OK, now, very important condition. 211 00:12:12,400 --> 00:12:15,180 OK, I have to reverse the last word. 212 00:12:16,000 --> 00:12:18,640 I have to reverse the last word of myself. 213 00:12:20,810 --> 00:12:22,400 So let us reverse the last word. 214 00:12:22,430 --> 00:12:24,990 So what I will do, I will call the reverse function. 215 00:12:25,670 --> 00:12:27,860 I will pass a string. 216 00:12:28,280 --> 00:12:31,040 Then I have begun pointer and and minus one. 217 00:12:31,580 --> 00:12:33,560 So this is basically for the last word. 218 00:12:35,150 --> 00:12:39,680 This is for the last word, last word is basically special then after doing this. 219 00:12:41,100 --> 00:12:42,360 I will step one is done. 220 00:12:43,040 --> 00:12:47,570 OK, so basically step one is then now step one. 221 00:12:47,610 --> 00:12:49,420 But I have to do I have to reverse the computer. 222 00:12:49,740 --> 00:12:53,520 OK, you can see step one is basically reversing each word. 223 00:12:54,880 --> 00:13:00,750 So they were saying each word is done now, reverse the computer string, reverse the computer string, 224 00:13:01,330 --> 00:13:06,460 so I will call the reverse function, I will pass the string start, will we begin? 225 00:13:06,460 --> 00:13:13,960 Will be zero and and will be so and will be eroticize minus one and minus one. 226 00:13:14,170 --> 00:13:15,440 Adult size minus one. 227 00:13:16,060 --> 00:13:18,150 So basically second step is also then. 228 00:13:18,400 --> 00:13:20,140 OK, reversing the complete string. 229 00:13:21,160 --> 00:13:22,930 Reversing complete string. 230 00:13:24,080 --> 00:13:29,270 Now, what we have to do, we have to handle spaces, OK, if you in the cold, let's first round the 231 00:13:29,270 --> 00:13:29,780 clock. 232 00:13:31,180 --> 00:13:37,660 OK, sorry, I'm not pretending anything I have to return, OK, I have to return, so I'm not returning 233 00:13:37,660 --> 00:13:38,050 anything. 234 00:13:39,210 --> 00:13:40,110 So first. 235 00:13:40,320 --> 00:13:41,660 OK, so let's get it done. 236 00:13:41,690 --> 00:13:43,710 I am doing changes in only OK. 237 00:13:50,400 --> 00:13:56,730 I'll give this one, it is working because this is not space's now let us submit cold. 238 00:13:56,930 --> 00:14:01,750 OK, so this will give us added because we do not handle the species condition. 239 00:14:01,770 --> 00:14:02,130 OK. 240 00:14:03,210 --> 00:14:06,090 So you can see yourself there are leading spitters. 241 00:14:08,310 --> 00:14:11,190 So I have leading spaces in my output. 242 00:14:11,220 --> 00:14:16,410 OK, so I have to remove the leading and the building spaces and also if there are multiple spaces in 243 00:14:16,410 --> 00:14:21,000 between, if there are multiple spaces in between, I have to convert the multiple spaces into a single 244 00:14:21,000 --> 00:14:21,360 space. 245 00:14:21,570 --> 00:14:25,350 OK, now let us write the code for handling spaces. 246 00:14:26,730 --> 00:14:33,420 So this is pretty much a straight forward, so let us consider this, make us bring help, so. 247 00:14:34,770 --> 00:14:40,620 First, what we have to do, we have to convert multiple spaces into a single space, so let us write 248 00:14:40,620 --> 00:14:43,410 code for converting multiple spaces and do single space. 249 00:14:43,630 --> 00:14:51,120 OK, so what I'm planning to do here is so if we have multiple spaces, I have to consider only one 250 00:14:51,120 --> 00:14:53,700 space and I have to discard all of those spaces. 251 00:14:54,070 --> 00:14:56,910 OK, so this is the first condition. 252 00:14:56,910 --> 00:15:02,220 Second condition is basically I have to remove leading and I have to remove trailing spaces. 253 00:15:03,530 --> 00:15:04,670 I will handle it later. 254 00:15:04,700 --> 00:15:06,860 First, let us handle this, OK? 255 00:15:07,190 --> 00:15:09,730 I have to convert multiple spaces into a single space. 256 00:15:10,070 --> 00:15:16,230 So let's see if this index is basically so I will store one space and I will move ahead. 257 00:15:16,250 --> 00:15:20,110 OK, so I will do a shapelessness till spaces are there. 258 00:15:20,420 --> 00:15:21,350 OK, simple. 259 00:15:22,340 --> 00:15:23,540 Now let us write the code. 260 00:15:24,260 --> 00:15:27,530 So first I'm converting multiple space into a single space. 261 00:15:28,470 --> 00:15:31,530 So while I is basically less than Aeronaut says. 262 00:15:34,380 --> 00:15:42,300 OK, so what we have to do so is basically the current character is a space if the current character 263 00:15:42,300 --> 00:15:46,750 is basically a space, what we have to do, I have to consider I have to consider one space. 264 00:15:47,100 --> 00:15:48,660 I can consider one space. 265 00:15:49,410 --> 00:15:51,210 So help dot. 266 00:15:52,310 --> 00:15:54,350 Pushback when space. 267 00:15:56,470 --> 00:16:00,280 OK, I will move ahead, I placeless now if. 268 00:16:01,870 --> 00:16:06,670 While Alphie, so if there are multiple spaces, I have to avoid them. 269 00:16:06,700 --> 00:16:13,270 OK, so if there are multiple spaces while F.I. equals equal space, what I have to do, I have to avoid 270 00:16:13,270 --> 00:16:13,480 them. 271 00:16:13,480 --> 00:16:15,460 So I will do a plus plus simple. 272 00:16:17,070 --> 00:16:18,240 Now, in the end part. 273 00:16:19,660 --> 00:16:24,140 But we have to do so in the early part, this is not a space character. 274 00:16:24,160 --> 00:16:25,430 This is a normal character. 275 00:16:25,450 --> 00:16:28,730 So if this is a normal character, I can take this character. 276 00:16:28,750 --> 00:16:32,380 So I brought push back if I and I will move ahead, I placeless. 277 00:16:32,770 --> 00:16:35,050 OK, so basically. 278 00:16:38,440 --> 00:16:40,870 What I'm doing here, so let's make it help. 279 00:16:41,020 --> 00:16:42,340 OK, so this is help. 280 00:16:43,720 --> 00:16:49,030 So what I'm doing here is basically I'm converting multiple spaces into a single space. 281 00:16:50,360 --> 00:16:53,420 OK, I'm converting multiple spaces into a single space. 282 00:16:54,920 --> 00:16:58,310 Now I have to do something for drilling and living spaces. 283 00:16:59,240 --> 00:17:00,350 OK, so if. 284 00:17:01,470 --> 00:17:08,670 I encountered a space I am digging that space character, so for example, this is let's call your Dimi 285 00:17:09,180 --> 00:17:10,650 Space Space fun. 286 00:17:11,880 --> 00:17:19,890 So I will consider your name as soon as I encounter space, I am taking that character, I am moving 287 00:17:19,890 --> 00:17:20,450 ahead now. 288 00:17:20,460 --> 00:17:25,200 Suppose if there are multiple spitters, so I have to avoid all these spaces and then I have to reach 289 00:17:25,200 --> 00:17:25,510 Eugh. 290 00:17:25,920 --> 00:17:27,660 OK, so this is the code. 291 00:17:27,700 --> 00:17:29,940 This is the line for avoiding multiple spaces. 292 00:17:31,060 --> 00:17:41,080 OK, so after this line, let us consider this example, so sky, then space, space blue and then I 293 00:17:41,080 --> 00:17:42,280 have multiple spaces. 294 00:17:42,290 --> 00:17:46,380 OK, so suppose this is the output now after this cold. 295 00:17:46,960 --> 00:17:53,720 So this output will be changed to single species sky and the go blue and then a single space. 296 00:17:53,740 --> 00:17:55,330 OK, so these three spaces. 297 00:17:55,600 --> 00:17:59,380 So these were basically the trailing. 298 00:18:01,070 --> 00:18:01,740 Spaces. 299 00:18:02,150 --> 00:18:07,490 OK, so if there are multiple trading spaces now, there is only one single trading spaces. 300 00:18:07,520 --> 00:18:12,740 OK, so if there are multiple leading spaces, so these are leading spaces. 301 00:18:14,720 --> 00:18:19,190 So if there are many leading spaces now, there are only one living space. 302 00:18:19,430 --> 00:18:20,580 OK, simple. 303 00:18:20,930 --> 00:18:22,520 So this is the string. 304 00:18:24,280 --> 00:18:26,620 OK, now let's do something with this. 305 00:18:27,490 --> 00:18:34,060 OK, so first thing before converting this multiple spaces into a single space, let us get rid of this 306 00:18:34,060 --> 00:18:34,880 living spaces. 307 00:18:35,230 --> 00:18:38,230 OK, so how to get rid of this living spaces? 308 00:18:39,930 --> 00:18:40,880 Very, very simple. 309 00:18:41,800 --> 00:18:44,080 So leading spaces will present at different. 310 00:18:44,350 --> 00:18:46,570 So while Alpha equals equal space. 311 00:18:47,170 --> 00:18:48,600 So this is four leading spaces. 312 00:18:48,610 --> 00:18:49,750 I will do a placeless. 313 00:18:49,900 --> 00:18:50,320 OK. 314 00:18:51,360 --> 00:18:53,670 So this is for the leading species. 315 00:18:55,050 --> 00:18:56,910 Now, leading species has been handled. 316 00:18:59,360 --> 00:19:01,920 Multiple services has been converted into single space. 317 00:19:02,630 --> 00:19:09,680 Now we will handle trading spaces, the spaces which will be present at the last now after discord, 318 00:19:10,010 --> 00:19:11,880 if there are multiple trading spaces. 319 00:19:11,900 --> 00:19:15,090 I have only one single dwelling space. 320 00:19:15,140 --> 00:19:16,960 OK, that will present at the last. 321 00:19:17,540 --> 00:19:20,020 So help dart back. 322 00:19:20,390 --> 00:19:22,540 So I have this function back. 323 00:19:22,550 --> 00:19:25,700 It will give me the last element that will give me the last character. 324 00:19:26,660 --> 00:19:28,940 So over the last couple of days, basically space. 325 00:19:30,010 --> 00:19:35,620 If the last quote is basically space, that means I have to pop this character, I do not need trading 326 00:19:35,650 --> 00:19:39,940 spaces, so help Bob back until just like push back function. 327 00:19:39,940 --> 00:19:43,780 I have Bob back function, so I will remove the last character. 328 00:19:43,930 --> 00:19:46,990 OK, so what I am doing here is basically. 329 00:19:48,070 --> 00:19:49,390 So consider this string. 330 00:19:51,500 --> 00:19:56,480 Sky space, space, space, blue, and then space, space, space. 331 00:19:56,720 --> 00:20:00,910 OK, so what this land will do, so it will take care of living spaces. 332 00:20:00,940 --> 00:20:02,820 So this is leading, OK, spelling mistake. 333 00:20:03,170 --> 00:20:04,030 So what will happen? 334 00:20:04,040 --> 00:20:05,310 I will reach here, OK? 335 00:20:05,330 --> 00:20:06,240 This will be my eye. 336 00:20:06,620 --> 00:20:08,420 All these two spaces will be removed. 337 00:20:09,410 --> 00:20:11,750 OK, then, after this cold. 338 00:20:13,150 --> 00:20:19,930 This will convert multiple spaces into a single space, so it will look like this when space then blew, 339 00:20:20,680 --> 00:20:24,190 then all this three spaces will be converted into a single space. 340 00:20:24,740 --> 00:20:27,970 OK, so this is the output after this called. 341 00:20:29,010 --> 00:20:36,030 Now, what I'm doing back, so help this is basically help sting, OK, this is help sting, so help 342 00:20:36,030 --> 00:20:36,660 the dog back. 343 00:20:36,870 --> 00:20:38,460 So this is the last element. 344 00:20:38,460 --> 00:20:40,680 So back element will give me the last element. 345 00:20:41,100 --> 00:20:46,800 OK, so this Beckers and function, it will give me the last element or last element is basically space. 346 00:20:47,130 --> 00:20:49,580 So if the last character is space, what I'm doing helps. 347 00:20:49,880 --> 00:20:54,840 Bombeck So aFunction push back and aFunction Bombeck. 348 00:20:55,440 --> 00:20:57,110 OK, so what will happen. 349 00:20:57,120 --> 00:20:58,740 So this last element will be popped. 350 00:20:58,950 --> 00:21:00,180 OK, this will be removed. 351 00:21:00,510 --> 00:21:03,360 Now my string is basically sky and the called blue. 352 00:21:04,780 --> 00:21:07,540 And I'm returning that string simple. 353 00:21:09,400 --> 00:21:11,080 So I'm retaining that strength. 354 00:21:11,140 --> 00:21:15,880 OK, so I think our culture and fine that does Amitabha good. 355 00:21:20,570 --> 00:21:26,480 OK, so our goal is working fine, so this is basically leading leading spitters and this is trading 356 00:21:26,480 --> 00:21:26,960 spaces. 357 00:21:27,590 --> 00:21:31,310 So this court is for converting multiple spaces into a single space. 358 00:21:32,480 --> 00:21:35,270 Multiple space and two single space. 359 00:21:38,920 --> 00:21:44,380 OK, now let's discuss the time and the space complexity of our solution, so what we are doing, we 360 00:21:44,380 --> 00:21:46,150 are just using the reverse function. 361 00:21:47,350 --> 00:21:51,050 We are trading over the string and we are using the reverse function. 362 00:21:51,910 --> 00:21:54,020 So what is the time, complexity, time complex? 363 00:21:54,040 --> 00:21:55,340 It is simply big often. 364 00:21:56,220 --> 00:21:58,810 OK, so time complexity is big often. 365 00:21:58,810 --> 00:22:01,750 And this is complexity is basically big of one. 366 00:22:01,780 --> 00:22:02,170 OK. 367 00:22:04,740 --> 00:22:06,890 So we have to reverse. 368 00:22:06,960 --> 00:22:10,500 We have to retain our strength, so I created this strong help and I'm reversing. 369 00:22:10,500 --> 00:22:11,650 I am returning that string. 370 00:22:11,690 --> 00:22:14,550 OK, so space complexity, we can say it is big of one. 371 00:22:14,580 --> 00:22:17,670 OK, so this is finally the time and the space complexity. 372 00:22:18,600 --> 00:22:21,170 So if you have any doubt in this question, you can ask me. 373 00:22:21,210 --> 00:22:22,200 OK, thank you.