Hello ji, how are you all? This is Love Babbar and welcome to the channel Code Help. This is lecture number 22. We are going to talk about Character arrays, Strings... no we are going to solve a lot lot lot of questions. The length of the video might be long so be with us, alright! So, Let's start. First, we learnt about 'char' in the starting of our basic videos. We take a variable 'a' and we added 'z' in char a. So, in this way we made a variable named 'a' which is of character type and the
value in it is 'z'. Then in this case, a block would have been made inside the memory which consists of 'z'. You are depicting this block with 'a', so this happens when you write this function. Alright! Something like this would have happened. Perfect! Here the limitation was that whatever character variable you make, you can only add a single character. But I want to store 'Babbar' , I want to make such a variable where I can store 'Babbar'. It has 6 characters in it, how will I able to do it or multiple characters? How to handle
this case? Here we'll talk about character arrays and strings. Perfect! I understand character arrays that it'll be of character type. But what are strings? Its a very good doubt of yours. Let's clear it once. Strings are very easy, if I talk about c++ we can say it as 1 - dimensional character type array. 1- dimensional character type array When you will go in depth and see the implementation, you will get this only. Alright! So assume the 1 - dimensional array as the string. We call it string, perfect! Just explain us what they are called. I
said 1 - dimensional character array which means your array will be of this type. It will contain some values. You can clearly see that it's 1- dimensional, right! It has values like - a, b, c, d, e, f, in this way. This is string. We called it a character array. Alright! so you have understood this. We have understood it on upper surface now. We are going to study what is in- depth string, exact definition, the difference. We'll start it now, so here you understood what is string. The 1 - dimensional character array in C++ is
called String. Perfect! We understood in the Use case, why do we need character arrays or strings. Because the character type variable can store only a single character. And If I want to store multiple character then I have to use character arrays or strings. Perfect! Let's understand once how did it occur? So this video is sponsored by Crio, the world's leading project driven platform. If we are targeting to get a proper job then we are already working on problem solving skills and DSA in this series. along with that we have to be strong in development then
Crio is the place for you. You will get different programs in Crio, for example full stack development program, Backend development program And the grade of the programming is of the level at which the project you are going to develop in this program those are internship level projects, here you are going to do it practically. At the end of every program, you'll have so much projects that your resume will very impactful. Not only this, here we have a fellowship program, where you are given the guarantee for the placement that you will be placed in a product
- based company. All the programs have a 1- week free trial facility and you will also get assured scholrship So a link would have been showing in the description, where you can avail the maximum discount available. You might see a coupon code on the screen here, you can also use it to take the maximum discount. And this is applied on every program. Thank you! What is character array? How do we make it? We know that in integer we wrote' int arr[10];' and it used to make an array which consisted of 10 values with the index
(0, 1, 2......9) Alright! and it had integers in it like for example-- 7, 9, 11.... 41, anything in this way and arr is a block which consists of the address of the first block for example, 701 then the address will be stored in the arr, in this way. Right! so arr knows where is its base address and through that base address I can traverse the entire array. Perfect! So you have understood that in case of integer arrays something like this happens. Now talking about character array, like here its written int arr[10], if I want to
make an array of 10 characters then I'll Write char ch[10];, right! so in this way you made a character array, easy, so we'll be using the same process, just we have to change the data type instead of 'int' its written as 'char' and you made a character type array, now let's understand it once. Here in this , you have the array and it consists of some values, alright! the values are -- a, b, d.....g, suppose indexes are 0, 1, 2,........till 9 since it was an array of 10, perfect! So there is a block naming it
'ch' which contains the address '701' , assume that this address is 701, for the first block. Its the same thing, understood!! so in this way character arrays are made. We are going to understand, INPUT, OUTPUT, PROCESSING, STORING, don't worry. So now you have understood how do the character arrays look like in the memory And we have also understood the difference between integer and character array. Perfect! Now, integer array was simple, i used to write--- cin>>n; Right! If I had write the input in the array then I used to take Input as---- cin>>arr[i] In the case
of character array, simple, its nothing difficult suppose you made a character array named as the word, 'name' and the value is 20, so how will you take the input?? Writing input is very easy Just write, cin>> name; Very easy, right! So now in the input , if it says to enter name and you wrote Babbar or whatever your name is, so where will it be stored Babbar will be stored in this name array, perfect! And let's see how will it be stored. So your array would be of this type, Of this type, so in the
first block its 'B', in the 2nd its' a', then 'b', then 'b', then 'a', then 'r' Let's make one more block here, and the index is 0, 1, 2, 3, 4, 5 in this way, the 6 and at last its 19. Remember one thing, whenever we take the input then automatically, here your input ends, at 'r' So here i side the next memory block after 'r', we made a character array which is called 'null- character'. It is called 'null character'. Now what is this null character? I had once told you to see the ASCII value
table , right!! in the starting lectures. So the ASCII value is 0 and why do we use it? As a terminator. Not the movie one. How 'terminator'? so that I get to know that, where the string ends. Done! So through this null character you are able to know where your string ends. Done! So if I print it, then it will print only till the null character, therefore it will always be printed till the character 'r'. So when you'll do the 'cout', then it will print only till the null character, no need to go forward, understood!
Perfect! Let's understand it once more. I told you that if you input any name, for example, cin>> name; and then you entered your name in the input, for example, love, so what would have happened? In the 1st block, it'll be l, then o, then v, e and now I know that my string ended when my string ended then automatically after the last character 'e' a null character will be added, in this way. So this is the way to take your input, this is how the input goes inside in the case of character arrays. We understood
this, great, perfect! So you have understood that input is usually something like this!! Now moving forward, if I say this is fine and tell me about the output. For output, you have to do 'cout', so, cout << name; what happens in this case?? In this case, let's assume this string, then it'll print character by character It will print l, then o, v, e then came the null character so stop. Finished!! stop!! Understood the processing! First print 'l', then 'o', then 'v' , then 'e', null character arrived so stop. Same thing happened in the case of
input, the name 'love' that was given, first 'l' was entered Then 'o', then 'v', then 'e' now nothing is left here so put the null character. So in this way, you had the input, so you have understood the process of input and output in the character arrays. You have also understood the significance of null character. Suppose, I made an array, 'a' , then null character, 'b' , then null character and 'c', then again null character, now what will be printed?? Let's understand what will be printed in this case. We printed 'a' then null character arrioved,
so we stopped, finished. So only 'a' will be printed here in this case, when you will face a null character your compile character will stop right there. It will not process further, alright! Let's understand one more thing here. We'll understand better if we do it, right!! So firstly , we made a character type array, char ch .... name, we entered the variable name the size is 20, let's 'cin' it--- cin>> name; before that to look good, I wrote cout>>" Enter your name" << endl; Alright! So we've 'cin' it and now here let's print it as
well. '' Your name is'' I printed it. Perfect! Now we'll run it. It said 'Enter your name' I entered Babbar Who writes his own name wrong!!!!! I wrote Babbar and then clicked enter. Now its says 'Your name is ' Babbar, let's remove this from here, we don't need this. It's looking uselessly dirty, decrease one space, now let's run it once again. Let's run it and it says 'Enter your name' I said Babbar. It says 'Your name is Babbar'. Perfect! right! Now I'll write full name and observe. I ran it, it says 'Enter your name' and
I wrote Love Babbar. It says your name is Love, really, it picked up Love but not babbar. Why? Let's understand this, in the input I gave it two things 'Love Babbar' This is Love , this is space and this is Babbar. So we have to understand one thing here that this is love, this is space, this is Babbar. You have to understand that 'cin' stops the execution. when? whenever you'll give it a space, tab, newline character which is enter. Done! You know how to give the space '_', you have understood what I want to depict,
you only clicked the space bar. How is tab made? The character of tab is this way--\t, and for newline its --\n Right! So you should know that when you put any of one among these three, then 'cin' will stop the execution there. How? I'll explain once again. Suppose If I had put 'tab' instead of space I wrote Love, then instead of space I clicked on tab, tab is above the Capslock in the keyboard I clicked on Tab, then Babbar, its still coming Love. Now I ran it once again, I have seen space and tab now
I'll see for enter, therefore Love and then not space but enter for new line oh! but at the time of enter, it took Love as the answer at the time of enter. He didn't let me write Babbar, alright! So you have understood that in case of 'cin' , if we use space, enter and tab then extension will stop. The upcoming character wouldn't be considered, but how can we do it? Don't worry I'll tell you. But have we understood this?? Let's do one more thing. Let me clear it first. I ran it again and I wrote
Babbar, so the answer is Babbar. In this case, suppose, I store Babbar and then null character, If I go in depth So the 7 characters have been stored, if I put null character instead of the third one that is 'b'-- Ba\0bar\0 In this case, according to the rule, first 'B' will be printed and then 'a' , after that null character came so we stop So 'B' and 'a' shall be printed. Now let's see what happens, so we wrote here null character at the 2nd Index of name character. Now, let's print it. Our name is Babbar
....not here there Your answer is only Ba, you understood the logic, what happened, i'll explain you properly So that you get a clarity. You made an array, let's do like this... on and on.. You wrote B, a, b, b, a, r and null character, so your array is of this type. You put null character at the second Index of 'name'. Which is the 2nd Index, 0, 1, 2,3 ....19, which means you wrote it here Now the value of this array is B a \0 b a r \0 in this way and here there will be
some garbage values. where we didn't put anything there will be garbage values. So now you will be printing this ---- I can print 'B' and then 'a' , then null character comes so we'll stop. Finished!! So in this way you printed 'Ba'. So you have now understood the logic, fine! If I'll have multiple characters i'll stop at the first null character, simple! Perfect! We have understood how 'cin' behaves, how 'cout' behaves and things are clear for us. Perfect! Now I gave you a question , so let's do our first question. Its easy, not very difficult.
First question is to find the length of a string. Fine, now how to find the length of string, what is this new thing? Just as I gave you 'name' above, you asked name and I gave Babbar So count Babbar, 1, 2, 3, 4, 5, 6, it has 6 characters. You gave me the answer as 6, this is what I want in this question. How will you find it? Its very easy, how will this Babbar be stored in the memory? B a b b a r and a null character, really, so I know that this index
is 0 and my string ends at the null character, will I be able to find the length? Its very easy I'll start from 0, one, two, three, four, five, six Now we reached the null character, but we don't have to count here so we'll stop Understood? we started counting from 0th Index 1, 2, 3, 4, 5, 6 Just as we found the null character, we'll stop its over. So the last answer will be printed, that is answer =6. It was easy, right! Let's write it once , that way we'll feel much easier. Make a function,
int getLength and here you passed the character array. Assume that its called 'name', if you remember the last time, the array questions that we did whenever we passed the array in it, we had to pass its size as well but we don't need to do that in character array. Why? We'll know that now. for( int i =0;) I'll start the index from 0 and till where will I go until I get the null character, which means -- name[i]!= \0, ok!! And we moved the 'i'. Understood!! so until we get the null character I'll move forward
continuously. We had to store the counting, so we made a variable, int count =0; Then we are just increasing the count . Alright! You came out of the loop and returned the count. So the length function is ready. Right! Here it'll be semi-colon. So in this way you found the length. I'll remove this and then here-- cout <<" Length;" << getLength(name)<< endl; Alright! Now i'll try to run it once after clearing. I ran it, it says to tell the name, I said Babbar. It told that the length is 6. So in this way, from the
0th index until we get the null character, we prepared our length function. We know how to find the length of the string!! Our first code is ready. I hope that you have understood with ease. Alright! Perfect! If we have understood this much then we'll move forward. Now let's do the next question, which says, to reverse a string. If you have been given a string in input whose value is 'Babbar'. And you have to reverse it, then your answer should be 'rabbaB' You wrote it just the opposite- which is r a b b a B, right!
So you have to reverse it, this is your reverse string. If it was an integer array, it would have been more easier, I would give you an integer array, for example , 1 2 3 4 5 Reverse this, then you would have given me 5 4 3 2 1. You know it as you have learnt it, start, swap, end, right! Do we have to apply the same logic? Exactly, its nothing hard nor dynamic, its simple. Alright! Let's do it. I said, we have to reverse it, do void reverse( char name[]) , right! Here suppose you
gave the length, you have figured it out already, So here what is the problem, return value is not matching.... oooh.. here we have written it so that is the problem We have done this much. Alright! Now, int s =0; perfect! now int e= n-1; This is what we used to do in the case of integer if we had to reverse, perfect! now while( s<e), exact same code will be placed here, swap(name[s++], name [e--]); I said this was it for the code. Now let's print once again after reversing reverse(name,) you stored it there itself Let's run
it once. It said to enter your name, I said Babbar. It reversed it and gave the answer, so its exactly the same as in case of integer. It was very easy for us. Perfect! We are going on the right track! Let's do a question where we'll pass all the test case and check. Its a very logical approach. So let's move on. Write a function that reverses a string. You have been given a string of this type, in the form, vector<char> whether we make vector<int> or array of int, its the same thing. So same thing will
be followed here as well. Just as we did for character array, its nothing different. It told to reverse it in-place which means do not take extra memory, do it in the given memory, ok, simple, no problem We'll do the same function here. Alright! So this is the function and .... leave it i'll pick it and put it here itself I picked and put it here, we don't need it It said if we are reversing a string then it should start from 0 and end at n-1 In this case n is s.size Done! Have we understood
it?? let's move on while (s< e), swap name and the name of vector is 's' We have to change it here, so its 'st' now. Indentation is messed up, let's clear it, perfect! Let's try to run it once! We ran it. Let's try it, successful! All the test cases have been passed. Now let's do it again. It says you are too fast, just wait, I'll try once again. Alright! So everything is working fine, so now I'll make a test case myself. I removed all the variables and kept only one, and in another case I didn't
keep any which might be a difficult one. Let's check it once. Here it says that length of the string should at least be till index 1. So this test case is not valid and we can remove it. Now let's run it. Its working fine. Let's try submitting it, the answer might be correct. Not might, the answer is correct so in this way you have learnt to reverse a string. You have cleared the question as well, passed all the test cases that too easily. The understanding ability that was there at the time of array same you
have applied here. Your work is done. Alright! So we have understood this question. Shall we move to the next question? Next question says to check if a string is a palindrome, ok! So first I have to explain what is palindrome. Third question says to check palindrome. This is the question I was asked during my college times and I was like .... oh god how can anyone ask such a difficult question. But it isn't difficult, its easy. So here, what is a palindrome? A string, assume it as character array, of the type It has a, b,
c, b, and a therefore what is a palindrome? If my string = s and I'll find a reverse-- rev_s = a , b, c, b, a, it will be same. If both of them are exact same, then its palindrome. That is the logic. I said that a string is given to me and if the reverse of that string generates the same string, then it is the palindrome. These type of strings are called a palindrome. Can I say that 'car' is a palindrome? How, 'car' is your original string, rev_s = rac , are they equal, nope,
so its not a palindrome. Is cat equal? no, cat != tac Wil Babbar be equal? what will be its reverse? 'rabbaB', its not equal. understood! There might be something..... 'NOON' This is the string, if I reverse it then also I'll get NOON' Are they both equal? of course, then its a palindrome. Alright! So in this way you have understood that , if an original string is exactly equal to its reverse string and of same type then this type of string is called palindrome, alright! Now let's move on to the question, what it was saying!! Question
gave us this example, it said that this is palindrome, let's check it. If we carefully read the question then we'll see that it says to consider alphabet and numbers only, ignore the symbols and whitespaces, ok, so I can give this question as homework to you. I'll show you the normal variation. alright! Let's do it. So I need to check the given string in the input and I have to tell in the output, if it is a palindrome or not, I have a question of this type in front of me. This is my question, I'll give
that question as homework, if you understand this then you'll be able to do that as well. Its very simple! very simple,. First approach that you know is, very simple, I'll tell you, ok fine. You have given me the original string containing values, so i'll make a new string, reverse string, reversed_s I'll make a new string after reversing, fine, and then I'll run a for loop through which I'll compare both the strings, for the comparison and after comparing i'll figure out if its equal or not. And on that basis I'll print yes or no. Absolutely correct!
But here you have taken extra space for a new string. Here you have taken extra space, complexity is fine, you made a reverse string-O(n), comparison - O(n), so the complexity is O(n). Time complexity is fine! But you have taken extra space, show me without this array, so i'll explain that thing here!! Here its your approach 2nd. Alright! Its very easy, suppose your string is = a, b, c, b, a, right! So i'll make it clear to you. here is your a, b, c, b, a, and the index is 0 1 2 3 4 Now focus
here, basically , if we reverse it so the answer would have been - a b c b a so we compared these two, in short we compared these two as well which would have reversed and came here if you have not understood then i'll explain once again. In approach 1, we used to reverse it, a b c b a and then I compared these two If I look carefully then which character is it, this and this one, alright! If this was a b c b a then I would have compared these two here and then
this one If we look carefully then which character is it, this and this one. so in short, you are comparing these two characters and then these two And then you are comparing this with itself, if it was even then with the one which is in front of it. So we have to compare these two and then these two and then the third one with itself Alright! So I have understood this a little, so this is 's' and this is 'e' Perfect! we started s from 0 and e with n-1th index, now I compared them the
string that i have, I compared it and if str[s] != str[e] then simply return 0, means to return false; Alright! If its coming then move forward, then the next case would be, both would have moved, we compared these two and they were equal If they are equal then move forward, means s++, I'll write it here If equal, then s++ and e--, perfect! now your s = 1 , which is here on 1st index and e= 3 , here Alright! you compared these two, they are coming equal, which means we have to move them forward Done!
So s =2 and e= 2, we compared 'c' with 'c' itself, its equal so you moved the s forward, here and e will be placed here Now s> e, in this case we'll stop, right! So in this way we can write this code, its easy. We have already done these type of questions, its very easy, if we carefully see the reverse logic, we compare it this way only. So why will I reverse it if I can solve the question in this way? So I made a function that bool checkPalindrome (char a[], char b[]), of this
type, now let's see what would have happened. But why is it two, we have to check single so here it should be single. I assumed that you must have included the length as well, right! Its not a big deal to find it, you must have seen it. Now int s= 0; and int e= n-1; in this way while( s<=e), you compared the character array, if( a[s] !=a[e]) then return 0. Perfect! and here.....ok fine...otherwise in the case of else, I'll write it properly so that you understand clearly. if its like this then return 0 and if
not then move 's' forward and 'e' to the back. If its not equal then, you'll do s++ and e--, like this. Alright! You have done this and came out and if you've reached till here then this is a palindrome. OK! We have done this, now we'll check it once, cout << " Palindrome or Not; " << checkPalindrome and I sent the string which is name and the length that is 'len'. Now I'll run it. I cleared it first and then 'Run', it says to enter your name, i said Babbar It said your name is Babbar,
length is 6 and your reverse is of the following type and is it a palindrome or not?? It said 0 that is false, its not a palindrome. Ok fine! I said that my name is NooN and then enter. Is it a palindrome or not? Yes it is. The answer is 1. Shall we try a variation? I said my name is Noon, one n is capital one is smaller. It compared and said that its not a palindrome, you're wrong. So keep this in mind and read the question once, if you have to assume the upper case
and lower case as one or not. The logic that we have written, it assumes upper case and lower case differently, so that is why it said that its not a palindrome. Read the question once and understand it , if its the same thing or not. Alright! So you have understood how to write the logic of the palindrome, its nothing difficult! You took the start and the end, put two pointers one at the starting and one at the ending, you compared them, if its same, move inward 's' will be forward and 'e' will be backward, again
same come inward, inward and again when 's' will surpass 'e', then its the palindrome, and if nothing matches then its not a palindrome So in this way you learnt how to make a palindrome, now let's move on to the questions. let's read what it says, it says that you have been given a string, but you haven't taught us My friend, that is the point, I'll explain you once again. Your task is to check whether the string is palindrome or not. And you have to consider only alphabets and numbers, that is from a-z as valid and
from 0-9 as another valid thing and the extra characters for example, @, whitespace don't consider that, they are useless, alright! Ignore the symbols and whitespaces and 'S' is not case sensitive. Understood 's' is not case sensitive which means either its upper case or lower case, assume both of them as the same. Just like in our case, it assumed capital N and small n as different, but in this question, you have to assume both of them as same. How will I assume both of them as same, I can't understand? You'll understand now, its very easy. Bring
them in the same format. Alright! so assume that I have any character, so how will I do it? In this question itself, if I had to make it palindrome that is to make it 'not case sensitive' So what shall I change in the code that it accepts? Right! So if I make a function-- char toLowercase... of this type I passed a character and if any character comes then it'll check if its already in lower case then it'll return it and if its in upper case then first it'll convert to lower case and then return Let's
understand it once again. If nay character is in lower case that is small 'a' then t will return it as it is. If ch >= 'a' && ch<= 'z' then return as it is. Return ch. In the case of else means that its an upper case character, then char temp = ch Here its ch, so what should be done with the upper case character subtract 'A' from it. Understood!! subtract 'A' from it and add 'a' . Are we able to understand this?? return this. Now let's see what will we get? Let's understand it. Suppose I
did.... or leave it I will run further on I'll run further on, here its 'cout' and " character is to lower case" suppose I passed 'a' or 'b Its the first line and in the 2nd line I passed the character of upper case. I passed capital 'B' or 'C', alright! now I Ran it I ran it. I said my name is Noon and I ran it. Now let's see what would have happened. For small 'b' your answer is 'b' itself and for capital 'C' in the Lower case function , the answer is small 'c' Perfect!
Means that this is working successfully, I'll give a character between a-z, if it's in Lower case then it'll come as it is, If I took 'b' then I got 'b'. If I give capital 'C' then convert it in the lower case, how did we do it, its working but how?? Let's understand this.. I said I know that my character lies from 'A' - 'Z'. Alright! Understood this and I have to make it between 'a' --- 'z'. Perfect! So suppose its 'B', and I subtracted 'A' from it, what will be the difference? 1 And I added
'a' to it. I did 1 + 'a' so the answer will be 'b' , that is what we had to do, we converted 'B' to 'b' No problem if you haven't understood I'll explain once again, I said that I have a character that is 'B', I made a simple function, take a simple difference that if its a capital character, uppercase character, then subtract it with upper case character ch - 'A', what will you get from this?? You will get a difference which will be added in the lower case so that you get your resultant character
Have we understood this? Let's understand once again. Suppose if I had '1' right! if I had to convert it to integer then How would I have done it? I would have subtracted 0 from1 and the answer would have been 1. What is happening, I can't understand? Alright!! So this '1' has an ASCII value, suppose its 45 and the value of 0'' is 44 It will be one less from it definitely, common sense! 45-44 =1 so you found the integer 1, like this. In the same way when you subtracted 'A' from ch, that is you subtracted
'A' from 'B' Suppose the ASCII value of 'B' is 65 and for 'A' its 64 as 'A' comes before 'B' so what will be its difference?? it will always be 1, right! and I know that this is 'a' and the value is 97 and I have to add 1. then it will make 'b'. Understood!! It the game of ASCII value, right! So if I have to convert from upper case to Lower case. then how will I do it?? Its simple!! there is a character 'ch' which is in upper case and we have to change it
in Lower case I subtracted 'A' so that we get the difference and then add 'a' and done! Suppose if I have to change from lower to upper case, then its simple again! there is a character in ch and I subtracted 'a' from it and added 'A' Perfect! right! Suppose a number is given in character form and I have to convert it in numeral form for example '1', I'll just subtract '0' from ch and I would get the answer. done! We are talking about ASCII value, when its subtracted we get the result from there, right! From
here you subtract and get the difference of ASCII value and you add that difference and get the character I think you must have got the logic here very clearly, right! so we have understood this. and now If I put the lower case here whatever the character is here, first convert it in lower case and then compare it. Right! First convert both the characters to lower case and then compare it. now let's run for Noon, earlier the palindrome was coming false for Noon, now let's see what comes. Noon Now the palindrome is '1', so now it
not case - sensitive. Understood this part. Perfect! right! Now it will not bother in the question as we know how to do it now. Let's move on. I'll take a simple example so that I can explain you. So I took this simple example. Right! I took the simple one and not difficult one. What dies this example say?? Let's understand it. It says that here its A 1 b 2 2 B a, perfect! first start will be placed here and 'e' here and then you compared both of them, are they same? yes they are, ok, let
it go. this is 's' and this is 'e', now are they same? no, then its not a palindrome. It can't be the palindrome one. alright! so this was the test case and the answer was no. once let's see the first case, its so long, let's make it small. I'll explain this to you. What is written there? 5 ? 3 6 @ ? 3 5 we have to consider only number and characters and nothing other than that. So start is here and end is here. I compared 5 with 5, are they same? yes, alright, perfect! then
you went to '?' but we don't need it. You skipped to '3' and in the case of 'e' you'll move back, so 'e' is here. Now you compared 3 with 3 , are they same?? yes, perfect! Then you moved forward, here its '6' and for 'e' here's a '?' we have to go back once again at 6 , now we'll compare both the 6, they are same, then we moved on, but we have to skip it Why can't we go on? because its '@' so we skipped, here is the start and we moved back the
'e', here its again '@' so we have to skip back Now, s> e, therefore we'll stop, understood! Things are the same , just one extra condition is there, don't consider useless symbols and whitespaces Simple, whenever you'll move it forward, do check if you have to stop at it, if it is one of the alphabets or numbers, if yes , then stop here else we'll skip and move forward, we'll move this way, alright! So where will the changes be applied in the code.... Here.. at s++ and e-- Here there will be some changes, we might have
to move forward a little more, if its a special character , symbols or whitespaces. So this question is a homework for you, you have to solve it yourself that is ' Check if the String is a Palindrome or not, its a pretty question, you'll have fun. Next question, valid palindrome, what is this question? We have just done it. It says to phrase a palindrome, after converting all its uppercase letters into lowercase letters and removing all non- alphanumeric characters. It reads the same forward and backward. Alphanumeric characters include numbers and letters. Its the same exact like
the previous question. I think its similar to it, given a string.....string isn't known , ok, let's do it. Now suppose we are stuck in a topic, where we can't understand a particular topic and from where Suppose there is a topic related to c++ and we don't know what a string is. Its for your own good, so that in future if you get stuck then you should know where you shall head to. I said string cplusplus, did you get the website 'cplusplus', I entered here You'll gain all the knowledge about the string from here, you know
what is a string? It is a type of class, we are going to talk about the string class, We'll discuss further , what is a class? here it's just nothing, its the same thing that we did in character arrays. But let's understand, how to create it. So let's see how to create a string? I said string s; its created. Alright! so in this way you created a string 's', then string 'str' = "Babbar", alright! Its ok we understood on the surface. we have to create it, but what will be happening in the memory? nothing much.
The method that has been taught that is happening. Nothing much is happening in memory. The same thing is happening which has been taught, here its B a b b a r and null character, Done! In this way, is your string present, we had the same characters, done! Understood! It has an in- built function which can be used to find length, that is, str.length We got the length, ok, so you didn't have to fuss over it , now what other functions are there. What else can I use? So here there are different which you can use
to figure out. You want to include something in the string means I want to add something after 'r', therefore, str.push_back('c'); So your answer would be, Babbbar c, right! And how is it stored? B a b b a r c , of this type, we have understood it. Alright! So in this way we push_back. We also have an option for pop_back. Suppose I have to re move 'r' from Babbar, therefor, str.pop_back() Now my answer is- Babba, and how will it be stored? Babba \0, just like this. Understood!! There are many things here, how can I use
the find function, copy function How can I use 'substr' , compare function, these are those things which appear every time, so do read it, that how is it being done!? In case of compare if '!= 0' comes, then simply, it means its not equal. alright! Just read it once, just refer to the website and study from here. you've understand a little, how to create a string, how to find the size, what is the internal functioning, how it works. Perfect! done! Bro do a question with a string and then we'll understand it clearly, ok, my friend.
So we have understood this much that we have some in - built functions we can use it in the library, we have understood that this is a type of class. In which we are using a character array for implementing the string, and we have many in- built functions which are beneficial for us. You have to do a homework. It says you have to see the key differences between character array and string. So, can you just tell me a difference between them, that you have to do in homework. You have to see what is the difference
between character array and string. How are they different? If I give you an example, you have a character array, which consists of, a b \0 c d and you are doing 'cout' So just see what is the output coming? and this is your string and it contains the same thing in it a b \0 c d and then at last \0 and then in the array as well. So you will do 'cout ' here then see the output that it gives. Alright! compare them and you have to study the differences, right! so this is your
homework, do it. Now let's move on to the next question, which is valid palindrome Read it once and then we'll do the process. What do we have to do here? We have to remove useless characters. Second thing, everything should be in lowercase. right! and then check palindrome and here the element has finished, we have to do something here. suppose here, 1, 2, ............... 8 , so the 'i' starts from here, just as I will make a for loop, which will go from here to here. when I will get a valid character what is the valid
character? either between a- z or between A-Z, or between 0-9, among these three According to question , that is alpha numeric. So if its one of these three, then simply I'll swap it. with -- the value of 'i' and then with 'j', done! You swapped it and then I'll move forward the 'i', as we've entered the valid value then 'i' will move forward. done! Then you have made an algorithm if its valid character, then swap, then implement 'i', done! Then there will come a position where loop will finish and enter a new character in place
of 'i'. Alright! when loop ends, I'll show you by coding then you'll understand properly. So logically, we've three things, first remove the useless characters, then convert everything to lowercase and then check palindrome. Alright! we have understood this!! So let's go step by step, nothing just start it. Remove useless characters, first do this much, right! which ones are useless? Alphanumeric characters means, if I explain it here then the part that we discussed just now. apart from them everything is useless so I made a function here which will depict these things. bool valid( char ch) Alright! now
we'll check if its valid or not. if ch>='a' && ch<= 'z' then its a valid character. Alright! then space , again space, and if its in between 'A- Z' and 0-9 then also its a valid character Perfect! In this case return 1 as its valid, else return 0; Perfect! So we've to remove useless characters, now, int i=0; and make a new string; String temp = ""; its an empty string and I'll make a loop, right! You have understood the logic, for ( int j=0; j<s.length(), j++); In this way. now if( valid ( s[j])), if this
is valid then you'll swap it with s[i]. Alright! Let's understand which one we are swapping. In new string we will just include it and not swap it. Temp.push_back(s[j]); as its a valid character. perfect! As its a valid character, we used the push_back function...... we don't need it now. So you have made a temp string in which useless characters have been removed, perfect! Now, convert them to lowercase, ok, fine! We have written the function I guess, here it is, the function to convert in lower case. we missed one thing, let's correct that first. We missed one
thing, it can be number too, right! In this way, return the same thing, perfect! Run a loop, right! and temp[i] = and you put the function here and its temp[j]; You've done this as well. third part was to check for palindrome, you picked the palindrome function , which we have already made, we added it here. Let's see carefully once, is it wrong, yes. Keep in mind that its a string-- string 'a' and a.length, sometimes we have to write as well. So I think, it shall work and we have already converted it in lowercase so no
need, let's remove this as well. And your code is ready. perfect! return and then you copied the function 'checkpalindrome' (temp); Let's run it once; Perfect! its working fine! here I added in the comment so that you understand. Let's take a dynamic case, in one it will be empty, in second one it will be space, and in third it will be ',' and in the another one there will be only one character. Let's see what comes. We should at least see these useless cases once. perfect! It says invalid test case, so this case doesn't exist. Remove
it, and then run it. So the code is working fine! Now I can submit it, I submitted it. And success. 100% solution is it. We had three steps, remove useless, convert in lowercase and check palindrome and in this way, we have done the question, done! alright! If you put above then you might have to make changes in case of lowercase, if it is kept below then its ok, it'll work. So you have understood this question, perfect! Now let's move on to the next one. Reverse words in a string to, this had been in your daily
challenge. I'll show you. right! so this had been in your daily challenge, its a simple question, its not so difficult. It has these things and it has whitespaces along with character, it has only two things You have to reverse every thing and... i'll explain an example, then you'll understand clearly. If your input is ---My name is love, then your output shall be yM eman si evol. So this is your output. You have to reverse the words and keep the spaces intact. Ignore the spaces and reverse the words, can you do this question yourself, let me
tell you the approach. Approach says that it will go on making numbers/ string with character. Fine with character array, you created a string, just as I will get a space, then what will I do, let's understand this. When I get the space means one word is completed, perfect! then the word created will be reversed. Perfect! so when i'll get a space, we'll reverse a word, ok, there will be a case when you'll not get a space but the word will be completed That is the last case, this is 'love' and then there is no space
after, but there is a \0, so here I got a null character then I'll reverse it again. Alright! You know how to handle space, if I tell you everything then that'll be no fun, right! Try solving this yourself, its written medium here but its actually easy. right! So you have to do this, reverse words in a string and it will be fun solving. I solved it on the night before yesterday. He is lying, no really I have done it. So let's move in to our next question that is return maximum occurring character in an input
string. So this question that I gave you, you have to do it in homework. Alright! next question. Which means a string has been given to you and it has some characters for example, test, so 't' has occurred 2 times. 'e' is there 1 time and 's'-- 1 time, so which one is maximum , its 't', so you stored this as answer. Done! which character is occurring maximum times?? store that one! alright! So let's try to code it, its easy and not difficult, but how will we code it? That is what I'm going to explain, right!
So I made a string s; cin>> s; your string is ready. You have to tell the maximum occurring character, here. char getMaxOccurringCharacter(), so I made a function here and I included string s; in it, perfect! Let's look carefully, how many total number of characters do we have, alright! How much alphabet do we have? Let's understand it and read the constriants. Perfect! it says, that the length of the string is till 100 and let's read the question once again. It will have characters only. Ok! So there are a total 26 alphabet characters, so we have to
store 26 characters, now see the alphabet carefully then it can be from a-z or A-Z , it can be either of them. No problem! I'll assume both as same, i'll just do '+1' , ok! Now let's understand it, so If I made an array of [26] and of int type. of the size 26 and I started to apply 'count' function in it, ok! fine! the logic is correct but how will we do it, let's understand. Can I assume 'a' as '0' and 'b' as '1'....... 'z' as '25'? Ok! you have done one- to -one mapping,
I assumed this as 'A', 'B'.......'Z' , perfect! So let's understand this, if I have a lowercase character then I'll subtract 'a' from the character. So from here I got my number, by using it, arr[number]++, you increased the count. If I have an uppercase character, then we'll subtract 'A', then I'll get a number again. arr[number] ++. right! Done! This was it! How to do it? no problem, I'll show you! You made an int array of size 26 and initialized it with 0. Perfect! I'll traverse the string, int i=0; i<s.length(); i++, in this way I wrote the
for loop, in a clear way. and char ch =s[i]; its not needed but just for your understanding. We could use void as well! I stored it in the character, now, if(ch >= 'a' && ch <= 'z'), do this one and if its not then, it will be in the uppercase, so this case depicts the lowercase and this case depicts uppercase, in case of lowercase, int n = ch - 'a'; so this way you found your int n. Alright! How can we write it more clearly? int value = 0; OR int index = 0; OR int
number =0 Come inside and write number = ch - 'a' and then again write inside : number = ch - 'A'; and come out of the loop and write arr[number]++; just like this, so what happened? Now your array has been prepared, you created array of 'count'. You created an array of count. Perfect! alright! Its simple, just do another traverse, it will pass: (int i=0; i<s.length; i++) ,make a Max, that is, int maxi = -1; maxi = max(maxi, arr[i]); Let's understand this! so count will come here. But we don't need count , therefore, if... ok perfect!
if( maxi< arr[i]), then in this case, store the answer, ans = i; and maxi = arr[i], perfect! I made a variable for answer, ans = -1, like this. Or ans = 0; it doesn't matter here, you can write anything because we'll be updating the answer. Alright! Here, char ch Or char finalAns = 'a' + ans ; and you returned it. Perfect! Let's run it once and then I'll explain, I said run it. Get....what was the name of the function? getMaxOccCharacter, and we included string in it. I said to run it. Then I gave the input
and said 'test'. You said here 'a', why? Let's understand this once. Ok! we wen to our test, if maxi < arr[i], then we stored the answer and in this way, maxi = arr[i] and we got the maximum count, perfect! So going on..... ok! So we ran a loop ranging from 0 till s.length, but we have to run the loop for all the characters. so here i< 26, now let's run it. So we have done somewhere..... ok , now let's run it, Here I entered 'test', here the answer is 't', ok! Let's run one more case,
which is given here, the answer in the test sample should come 'e'. Alight! We ran it again and wrote the answer test sample and the answer came as 'e', perfect! Its working fine! Now in the next one the answer should come 't', we cleared it here and then ran again. The answer is 't', so the code is working dynamically and if you want you can check it here, by submitting, it'll work. Now let's see what we have done! Ok! Now I'll explain the approach here, alright! I think, we didn't need to to double here because
our factor was going to be in single and lowercase. We have uselessly attached check here. Alright! Its not given in the constriants but it seems logical to me as you have to give the answer, then you should know that time. Or we shall assume it as different characters. Alright! So we have understood this, now assume that in our code the input string will be in the lowercase. So we'll optimize it this way, remove that and add this one! Ok! let's run once again, I hope there's no mistake. Its Fine! Now let's see what we have
done! Firstly, let's make an array. We made an array. Whose index will range from 0 - 25 and it depicts--- a, b, c, d,.......z, ok! So 0 is with 'a', 1 with 'b', 2 with 'c' ........ same way 'z' will be with 25, alright! And the value depicts count. Count of that character in string, in the starting everything is 0, 0,...., In input string how many times does this particular character occurs. then we ran a loop, suppose our input is-'output', here we enquired where is the character 'o'. So , we did ++ in the block
containing 'o', earlier it was 0 and now do it as 1 for 'o'. Where is 'u', its here, so remove 0 and include 1, where is 't', its somewhere here so covert it as1. where is 'p', so like this........you again got 't' so earlier it was '1' and now its '2', therefore here you stored count in every block. And you know the mapping of the block, 0 with 'a' , 1 with 'b', then 2 with 'c'........ It is mapped this way. Alright! So here you keep the count of each character, how much times does 'a'
occur, then 'b' , and then 'c' and then 'd'..... you know this count now. Right! Now we have such an array which says that from 'a , b, c, d.......z', which character occurs how many times? This is 1 time , this is 2 times, and then thrice. Or this is also occurring 2 times and this occurs 0 times, just like this. Now can I take the maximum number from here? Very easy, as here its a variable, this is 'f' and it occurs 3 times. I came to know that this is maximum. So when i get
my maximum number then I store the answer, I am storing index in the answer. So here 5 is written in the Index, so I stored 5 in the answer. I just hade to make an array from the string, simple add 'a' in it, here I 'll get the answer. Alright! If it doesn't come with '5', so here you have understood the working, right! So in this way, first we removed the count and then found the maximum index and then simply we added 'a' and returned it after making it the final answer. Our work is done!
ok! We don't need to do it, I write it so that you understand, otherwise you can write it this way directly! Alright! Code will still work. There won't be any problem. Alright! I am expecting that you have understood the code, and what is the complexity? The first loop runs till the length of 's' which can be a variable 'n'. Its O(n), second loop runs 26 times, it'll remain constant so its O(1), alright! So our time complexity is O(n),then how much is the space complexity? Here 26 is fixed size and constant space, so the value of
space complexity is constant space. O(1) and time complexity O(n), where 'n' is the length of your string. Perfect! So in this way we solved one more question! You have to do it as well! submit it here and then tell me if its working or not, Or bro has made a fool of us. Alright! So I am expecting that you have understood this thing. Further on, we have five questions left, we'll solve them as well, don't worry I'll explain it to you. Right! So now, we have a total of 5 questions left. So one thing I'll
explain before starting We have skipped one thing, I had told you that 'cin' stops the execution when it encounters space character or tab or you pressed enter or newline character. Alright! In these three cases, if I want space then suppose there is a line written My name is Khan and I want all of the whole sentence, how will I obtain it? Tell me! Its very easy, If you want to store it in the character array, then we have to use 'cin.getline', Alright! You have to enter then name of the string, which is suppose - str
and then we have to input length which is - 'len'. You can take the input as following, I have already discussed this thing, right! When we started the array and we talked about 'cin' and you have to explore this once! Alright! So just study and come once 'sin.getline', I have already told about it, this is for your homework. And If we have to put custom delimeter then how will you do it? This is also your homework. Nothing, you just search it in google, how you have to apply custom delimeter? You have to focus a little
bit. Now in character array we learnt, char ch[10]; which also has the inbuilt functions, which I'll tell you, alright! In- built functions are already present and you can use them in many ways, ok, its not difficult, only simple; Suppose we have to find length, then I'll store the length inside a variable, that is : int len= strlen(name) Found the length, right! Now I want to compare two strings. One is s1 and other is s2, then, I can use the function: strcmp(s1 , s2) This will give :!= 0, when t+ I hey won't be equal. else
it'll be equal, understood! So you can easily compare the string! Now if you want to copy any string, then you'll write strcpy and the first argument will be the destination string and the second value will be source string, alright! So you'll be copying from source string to destination string. Perfect! If you want to do these three inside a string, as you have done this in character array, I had told you to find the length from the function: s.length() here, strcmp......no I had shown in the website of 'cplusplus', that there is a compare function which we
have verified how it works. In 'copy', simply write, st = s1 =s2 or s2 = s1, whatever you want to do, it'll be copied. So these are the things which you should know. Now let's move on to our next question, which will give you more knowledge, today the concept is straightforward. we are doing only that much which is needed and today we are doing more questions as children are demanding. Sure! Th question says that you have been given STR, a string, which has many words. You have to remove all the spaces and replace it with
'@40" If its written 'My name is KHAN' then the output will be- 'My@40name@40is@40KHAN' You have to do it like this, ok, perfect! The function is given. You have to do this way. It easy, we did the previous question where we removed useless characters by creating valid function, we'll be doing the same thing here. You have a string, that is, temp = ""; and run a loop which will titrate in our input string. It will traverse. Alright! understood this! Perfect! if( s[i] == ' ') then we have to do something , else we'll do anything else.
Of its a space then I have to replace it with '@40' in the answer array, so I'll put three things in 'temp' temp.push_back('@'); Alright!! and then I'll put '4', then '0' and if its not a space then i'll put itself: temp.push_back(s[i]), perfect! Simple! Now we'll return it. Done! Now let's run it. Work!!! I think once I have to load it. Run it! I went to run it! and it said that there is something wrong in there! What is the mistake? It said, do check the name first, here its 'str'....oh! sorry! Now let's check it. We
ran it. Everything is correct, now submit it and it said that your answer is correct. So in this way we have done this question as well, it was easy. What shall I give you in homework? If I give that, you don't have to make a string separately, make the changes in the 'str' string itself You are not allowed to take extra space. All the changes will be made in this string, so that is your homework! Do it, if you are not able to do then I'll tell you. First check if its possible or not, think
once and if yes, then how? I want in-place solution. Show me in the O(1) space, now what is its time complexity? look carefully, one for loop is running till the 'length' So the time complexity will be- O(n) and n= length of string and what is the space complexity? Look carefully here you have created a new string named 'temp' and inside it the space is also- O(n). Time and space both are O(n) and 'n' is the length of string. OK! Understood this! Perfect! This question was easy. Let's move on! Next question says to remove all occurrences
of a substring. OKAY! Let's understand this one. This question is pretty good! You have been given two strings, first 's' and secondly 'part' and you have to do the following operations until all the parts are removed. Find the leftmost occurrence o the substring 'part' and remove it from 's'. Fine! Return 's' after removing all occurrences of 'part'. And what is a substring? continuous sequences of characters in a string, perfect! You have been a main string, this your main string in this case and a small part is given. It says that wherever, this part exists in
this string, remove it. I'll explain how. Let's write the question- Remove all occurrences of substring. Let's write what is given in input, it says, d a a b c b a a b c b c It says that 'part' is 'abc'. It said to remove 'abc'. One is here, we got it, and then my array will be d a b a a b c b c, then I found another one here and now my array is d a b a b c , and then I found it here again 'abc', then my answer is d
a b, this should be my final answer, in this way we removed all of it, so now the answer is 'dab'. Understood! So we have to work this way on it! Let's read the constriants so that our approach is easy, right! Perfect! It says that the length of string is 1000. and this is then length of 'part', so if the length of string is 1000 and I have to traverse it once. Then let's start, we are going to implement what we have studied, therefore, while( s.length != 0) till then along with it the particular pattern
or part should be present, we said s.find(part) < s.length(), alright! What is this and why find is being applied here? you'll get many things in the website of 'cplusplus' What all functions are present? and what can you use? So you'll get 'find' over here and then you can get 'erase' as well then further on you can see what is the use of 'find', here it says that it searches the first occurrence. in the question it said to delete the leftmost occurrence so it matches with the function of 'find' and then we used it. How will
we use it? Examples have been given, 'erase' - how do we do it? Examples have been given, just visit this page and then you'll understand it. s.erase and now I have to delete that particular pattern, therefore, s.find(part), part.length() Try to run it once. What will happen? Error will come and what else, because you haven't return anything. Try it now! Perfect! so you'll definitely delete right! So one case can be, when string will completely exhaust, nothing is left. Its length is 0 in that case, right! Then come out of the while loop, or else, when its
!= 0 then only you'll enter into while loop along with it go only when the pattern is present. So I checked it here, if that part is in the main string and if yes then in which index. If that index is less than s.length then its working totally fine! If you find it then delete it. So you just checked if the 'part' variable is present in the main string, yes, then delete it. Is it still present? yes, then delete it again. What we did in the logic here, this entire algorithm that we saw, we implemented
this , ok! Let's submit it but let's run the example test case first, we ran the code. Accepted! we submitted it! Success! perfect! 84.03% , perfect! So in this way we used the functions of STL, we wrapped the code in two lines, right! So you can see how s.find works internally OR how erase works, so you can work all these things out! Right! What do we have to do? Go here and read. Understood! So just work hard this much. Let's move on! Permutation in string. So the next question is permutation in string. Perfect! You have
been given two strings and you have to return true, if s2 contains permutation of s1. otherwise return false. In other words return true.....ok! If s1's permutation is the substring of s2, perfect! So, if s1's permutation exists in s2 as a substring, then return true, else return false. Understood! Let me show you the example. here s2 = e i d b a o o o and s1 = ab, so what can be its permutation, one might be 'ab' and the other might be 'ba' and we have to check if either of it exists. 'ba' exists here,
so the answer is true, like this. Alright! Remember it should be continuous , not separately, if any of the pattern is present then only the solution exists and it will be true. Otherwise no, understood! In this case, 'a' is here and 'b' is here, separately so it'll come false, right! We have to solve this, now let's think how can it be done. Let's understand. First, I'll store the count of the character in the string s1. Remember we did a question earlier, int arr[26] and we had stored the count there. Similarly, we'll store the count, we'll
do the mapping of characters with the indexes, will it be done!? Done! Then, I'll traverse in string s2, and how will I do that? By making a window of the size( s1.length) , like in this case s1 = 2 , then I'll traverse in the window of 2. What does that mean? If the s2 = e i d b a o o o and s1= ab whose size is 2, so you'll make 2 windows. First, you checked this, is it a permutation, no, then you moved forward, is this it? No. Is this the permutation? No
So is this the one? Of course. Yes, So i'll travel in this window form and done! So I stored the count in s1, that how many characters are present!! Why am I storing this count? So that later on I can use 'compare' as well, right! When I am traversing in this window, then I'll do the comparison right! By using arr[26] And then I'll come to know if the answer is true /false, how? Let's understand it! I know that 'a' occurs once and 'b' occurs once as well. Now I went in this window, here one is
'e' and other is 'i' , 'a' and 'b' aren't there. No, this window is useless, then I went to this window, I subtracted 'e' with 0 and added this new character with 1. So that is now '1', so now your 'i' is 1 and 'd' is 1 as well, it doesn't match 'a' and 'b', in this way, the element that has been left subtract it and increase the count of the new character and then compare it, comparing is for the constant as we have 26 characters only Its constant. Its very simple but interesting, it'll be
fun while doing it. Let's try doing it. You must have understood half of it because I haven't implemented it, now you will understand it fully. Right! First, store the count of character, so make an array first. Int count[26]; So i'll be storing all the characters in it. In the beginning every thing is 0. Right! Now traverse it, int i=0; i < s1.length(), i++, and in this case count[s1(i)]; let's make it simple for you. int index =s1[i] - 'a'; just like this; and then count[index] and I'll implement it, perfect! So here I have done the count.
Character count array has been made. Perfect! It's ready for s1 string. Alright! Now I have to run a window and what will be its size, equal to the length of s1. Now i'll run a window, now traverse s2 string in window of size s1 length and compare. Ok! you have to do this much, so you ran the first window, how will we run it let's understand? int i = 0; and let the size of window be 'k', int windowSize = s1.length(); while(i < k/ i< windowSize), you are running this for the first window. Running for
first window and let's see what happens for the first window. You ran for the first window and in this case, suppose you prepared one more count here. Let's assume it as count2 and this as count1, right! So here count[2]......windowsize is less than i, perfect, perfect!!! ok, understood!! count2, so first we found the index here. what will be int index equal to? What will be int index equal to? yours s two of I-A very good! then i will do ++ in count2 index very good how much it has done? I ran first window i ran first
window and remember we have to increment this 'i' also i ran first window like that okay! i ran first window Now what i did i checked it once check = in it i have to check count1 and count2 okay! if this comes true return simply one if it comes true send true else process further process the window further very good lets see the counting method once okay! We said for(int i=0;i<26;i++) i++ if any count is different a of I is not equal to b of I then return zero else return 1 okay! so if these
two array are not equal then it comes false else it will be true very good! process the window further your count I will be at the same place What we said while i less than while i less than while i less than s2.Length run the loop till then so what you will do, if this case happened lets understand it once you are going to add a new element in your count array and removing the old one lets assume k is two and you are on 4th index Lets see it once this way if we talk
about this index this is zero this is one and this is two now we came here now what we will do is we will subtract this D from A whatever number will be stored we will do arr of N ++ very good and the one which is behind it, what index would it be lets understand carefully we have to go behind the current index how much time ? two times that is K so i-k Understood? understood? so first we will remove the i-k character first like that or like that and then will repeat the same
process we will minus y with a and then whatever value n stores we will do -- it how? let me explain it to you very good! you understood it . don't worry i know you are worrying we went there and then by going in second window we have to increment the new element what does it means lets understand this once So what is the new element char ch = ,what is the new element let me write it new char... what it become? lets see it carefully what it becomes it is ith element of S2 very
good i said what will be the index index will be new char - a. very good you created the index then you incremented this index in your count variable because it is new one. this has come newly inside the window So you incremented it but brother the one that has exited the window like in this case in this case this has added newly inside the window so you incremented it but this one exited the window before window was this now window is this so it has come out so you have to decrement its count so
what you said that char old char What is the old character i have s2 of I -k very good what is K ?k is nothing okay! and then what you said that index = old char minus a index found then you subtracted this index in count two very good! in this way you did this work very good . now you have to check it This thing you have to check them always that if they become equal if they have come outside and did nothing then they cannot be equal lets run it once we forgot to
increment this loop i i++ now run it it is accepted lets run all the test cases once lets run it they ran lets check once that if anything stuck? string length is going to be atleast one Lets see the case where string length is given only one i said when it is given only one come on friend. i said any character that doesn't exist inside it code is running correct i think lets submit once milk's milk and water's water submit woho! runtime error has come . we have done a mistake somewhere very big ! lets
see We found index s2 of i s2 of i running for first window s2 of i minus a correct! and.... you subtracted it from a and found insex s2 of i okay! we started i from zero and did ++ with window lets understand the question . if we did a mistake? we don't know okay! went into submission there is some mistake in b and a Means it is greater means it is greater . okay! so what is our mistake we tried going into window but we didn't checked whether will you be out of the string
or not? we have to check this condition altogether we have to check that if it is not like that we exit the string while checking this window who knows if your window exceeded the length of second string lets submit once very good! so you understood it better than 97,35 solutions so speedwise we are sorted lets understand the logic behind this Lets understand again what happened what we did is we prepared count array of string s1 that we have to check we got to know that inside it a is this times, b is this times, c
is this times, d id this times, z is this times we found all the factors about it then what we did the second string that we have to check if s1 exist we went into it in its first window we checked what is count array coming? we created another count array for that for first window Then we compared both the count array if they are equal. if yes then return 1 else check further then you created first window check if both count array is not equal then what you did you went to next window how
will you go to the next window? last one. The left most character of windows lets see here once we will remove the left most character of windows and add the new character of window understand? in beganing this was the window in beganing this was the window we added a new vector and new window becomes this And removed the old one like that we are going so if see here new vector is added here old one is removed from here and here you checked it. now is it equal? like that you are checking so what is
the complexity of this solution here length of o of s1 if i consider length of s1 as M then this becomes o of M. okay! till there then i move further so what is it? it is window size What is window size? if window size is o then o of m will be result this is also o of M so o of M +o of M +o of M is complexity till now then i came till here this is goin till there. this is going till s2 length that is... lets consider it N o of
n so what it is yours? o of m + o of n is your complexity is going on and in this way you found your syllabus So what is complexity in this case lets see carefully took one space here of 26 that is fixed means it is constant space took one space here of 26 that is fixed means it is constant space means space complexity comes out to be 1 and any other space we haven't used so in this way we did this question and it is a lovely question like i can say this type
of question can come to your online test you apply in any company , this type of questions can come in internal assessment lets move to next question what is next question? okay! What does the question say lets write about this what is it remove adjacent duplicated see here b and b are same characters let me show you what have you given in input?abdaca it said wherever you find duplicate character remove it. so what can i see first that see duplicate i removed them now what becomes my string? a This will be removed aaca this much
part will be removed now i can see it again comes duplicate i said no problem remove this also now what is left ? ca and this is our answer okay! you can se ca here what we have next input here? azxxzy what can i see is same remove them now your answer be comes azzy now i see this again comes same Okay! now what is the answer? ay and this is the answer you have to play like that okay! we have to play this type of game here it can exist upto ten raised to the
power 5 okay! so you understood what is the question saying so till now we have solved every question or i have told you to same so you have to solve this question from scratch it is easy level question easy is written at top So it is going like homework for you you have to solve it by yourself if you are not able to solve then we will do this otherwise discord is there okay! you have to do this question now lets move to our next question so here is a mistake i have already shooted the
remaining part of this video but my laptop recording didn't take place so i have to shoot it again i have to do it once more , okay! So here i told you you have to do nothing things are very simple . you can understand the logic that like this we have to do things we did the same type of question before you have to treat this question as homework try to solve it yourself spend 1-2 hour in thinking the approach if it comes in 5 min. it is good . but do not see the answer
too early first try yourself it is nice and frequently asked question do not go on easy. it is asked . it can come in online test as it is as it is okay! So in this way you have to do this. try it once in homework and you will understand it. lovely question lets move to the next question it was a lovely question. i even did code of this . See now lets do it again. let me reset it. reset it! lets read it again. what does the question says? we have to do string compression
if you read the question carefully what will you understand? first read the question once that you have given something something in input it is given in input like a,a,b.b.b.c.c.c it is given like that Okay! what you have to give in output? a is how many times? 2 times so a,2 b is how many times?3 times so b,3 and c is how many times ? 3 times so c,3 this is your output understood? we have to tell the character and how many times it came what is the exception? exception is if any character is one time
then only that character will be shown No nee to count what brother ? didn't understand it. don't worry i will explain it. means if the input is like this lets assume a,a b is one time ,c,c and d,d two times a is 2 times b is one time if it is one time no need to write write simple b c is two times , d is two times so this is your answer i an talking about this thing okay! So in this way we have to solve our question and things are understandable what we have
to see. It is nothing we will just travel forward and see which character has come how many times so see last one we have written what we wrote if some count is one if something has come only once so simply write that character like b came in last example here b came only once so we wrote b simply if someone has come only once if its count is one write it simply If count is more than 1 so what you will do is you will write the character with count means a2 or it can happen
that it has two cases or count is less than 10 or count is less than 10 or greater than 10 if it is less than 10 means single digit so you simply write your answer else if it is double digit or it become large then convert it to single digit and then copy it like this we created the algorithm lets understand again. If count is one so simply write that character in it else means if your count is greater what you will say that simple first put a character just put it see its count whether
it is single digit or not if it is less than 10 or not else case if it is less than 10, then no problem simply put it it is not then first convert it into single digit lets assume 12 then convert it into 1,2 then put it in answer this type of thing we have done Now you have understood that you have to simply count and write if it is 1 then i will not write simple thing. You have understood it remember that here ab has come 12 then we will not write it as 12
answer will be of type a,b,1,2 like that because only one element will go into character either one or two 1,2 cannot go together we learnt it long ago in basics lets try to solve it i have done it. so lets go . Int i = 0 we created this count we created one variable that is showing us the starting of character array okay! then i created an answer index zero what is answer index what it said to you? one important thing that whatever answer you will find like you have found this answer do not take
separate space for that the answer that you created separately Incumbent this answer inside this only incumbent it only into the vector that is given do not take any vector, any string separately so that is why we write this answer that whatever is the valid point whatever is the valid answer,valid element, valid character i will put it in answer index and keep incumbent it like we saw in valid type question right! what will the ecompress function give us in output? it will give us the output that we have found its size. What is its size That
size we have to return read the question carefully . i haven't told you the question properly . you have to read it yourself once after that you have to see the video let me explain it again you have to solve it like that and the answer that will come , you have to put it in that array only and the size of that answer you have to give it into the output of that function okay! you did this much. now you said that while i less than First do it a little bit good lets assume
a number i put it into int n char.size it will help in writing now so till when i is less than n do nothing you go and pick up its next element what is its next element i+1 you showed it in j what you have to do basically? you have to compare every character with next one If it is same you will count ++ and keep increasing the count and if it is different you will stop. after stopping what will you do? first you will put it character in answer then you will see its count
whether it is single digit or double digit and out accordingly. okay! so first what we do lets compare we compared y j less than n firstly don't run out of the track j less than n and then compare it Chars of i == chars of j it it is equal then keep doing and keep increasing j like that okay! so you have done this work now when you will come out of this loop when will you come here in two cases only either vector is completely traversed or what had happened? new or different character is
encountered assume in starting it was a Now it got b and it came out as soon as it got b that wait first let me store answer of a then i will talk about b okay! let me first store the character . how you will do it? chars of answer index++ what you will do with that? chars of i you stored it. okay! what will i call it is store old character you did it Then you found count once let me store count also so you found count. How will you find count? j-i okay! i
is your first most presence j can be your new element, new character or the last index of your vector means last index of character means basically we have come out of the vectors so you will find your count by doing j-i now count has also condition that if count is greater than one only then you put it This is the condition if count is greater than one only then you put it otherwise not so you said if count is greater than one then i will do something what will i do? i know i have to
put the count after converting it into single digit i have to put the count after converting it into single digit so i said first let out it in the string i said string cnt = two string count so i converted count into string so why did i do this? see that's why. i ran a for loop char ch belong to cnt So in this way very cleverly i put out a character from an integer very nice solution we did it very cleverly and we put it in our answer char of answer index ++ in that
answer becomes ch in this way you put it and nobody noticed so for what this whole logic it? converting counting into single digit and saving you have done this much. now what we have to do So our all work has done for first character what we do we saved first character means you saved that particular character here and you also saved its count here both works has done. now i can move to the next character where is next character i told you that you will come out from here only when you find next character next
character is present at j okay! i = j very good! done! now what i have to give in output in this function I have to give the size of answer array i said i was storing that inside the answer index we were storing that earlier .What is the problem in that? code has done. ok egoistic person lets do run it once we ran it once I took all test cases and ran it It is working fine. Now we will submit it Accepted Our accuracy improved with this solution So I hope that you must have understood
this solution But I will explain it once again Int l =0 for traversing the entire vector ans index is to keep the track of the valid index then we have a simple while loop I will simply keep incrementing J as long as I am getting the same character If I come out of this loop, then I mean that I have got a new character Then we will store our character Now we are on a new character here we returned our answer This is how we have solved this question I hope that you must have understood
this We have solved many questions in this video. Its 6:31 in the morning I haven't slept yet This was for today's video I hope that you must have understood and enjoyed this whole session This question was asked in an online round as it is let's have a look at its complexity once We haven't used any extra space, so space complexity is constant We just have used one while loop to traverse this vector, so the time complexity is O(N) and space complexity is O(1) I hope you got it Once have a look at its discussion forum
and editorial You may get other approaches there The solution which we did is very much optimized, but you can have a look at other approaches too Tell me in comments whether you enjoyed this session or not. Also mention whether you watched this 2 hr session complete or not The video length has crossed 2 hr This was for today's video. Will see you in the next one. Tata bye bye, see you.