My Project
My Project
Hello, this is Rupesh. You are watching CppNuts video series on string related
interview question series. And this is Pangram, like check if it is pentagram or
not. So you'll be given some string. Let's say, and you have to check that if this
string is pangram or not. So let's see, what is pangram first?
So pangram, meaning if there is a string, then it should contain all the characters
of alphabets. It can be big A, it can be smaller, or the combination doesn't
matter. And this string can. Very big, or at least 26 characters. I mean, it can go,
uh, below 26 characters. If it is like that, then for sure it is not a pangram.
So if you consider this is your given string. Then you can see that if you pause
the video and count, you will find that it is having all the characters from
Alphabet. And I told you like this T can be a capital T and that would still be
considered. So it is like combination of capital and small letter.
Should include all the characters. Now let's talk about the implementation part
here. You know that this, this is string what we have here, it can be very big
string, and the solution, what we are looking for is like O(N). So what we are
going to do is, first of all, we know that we have 26 characters to deal with,
right?
So I can assume an Array or a Vector. 0 2 25. Right, And I can fill all the values
here. False. Initially, we'll put every element as false and we can map like 'a' to
'z'. We know that it is 26 characters. We can map 'a' to 0 and Z to 25. How we
are going to do that? Actually, it's very simple. Whatever the character you
have, let's say t, T minus.
A, if you calculate this, this is going to give you the index of this T in this Array
or vector. So for simplicity, if you take B, so B minus A is equal to what we
know this is going to get calculated in ASCII . So 'b' ASCII is 98 minus. 'a'
ASCII is 97 is going to be one. And. Will be indexed at first position. And
similarly, A, so A minus A is zero.
So A will be here. So this is how you will map it. And it doesn't matter if it is
small or capital. The only point is you have to check each and every character if
it is capital letter or small letter. If it is capital later, then you. Do this minus
element with the capital now, and then you'll find the result.
So if it is capital, you'll use this formula. If it's small, we will use this formula.
So let's quickly see this program. So this is your leetcode . This is the question,
and here we have this solution. I told you right, we can use array or vector. I
have taken this vector name, flag. It will be of 26 size and all will be initialised
with falls for the beginning.
And then we have this for loop. We'll it right over this sentence. Whatever we
get here it right over it. And we'll just check if this element is small. So this is
for small. Okay. And obviously this is for capital, so we know flag is initialized
with falls. And then we'll just simply. This true initialization, and this is the
formula E minus small A.
If it is, I mean the E is small. Otherwise this formula, and once we are done with
the full sentence, we can check like flag is left with any false because we'll ,
iterate over flags and if it is having any false in any of. Index, then we'll use
written false. If we don't hit any falls here, then we are going to obviously return
true.
So this is the solution, but we have another, uh, quicker way. Just check if
sentence dot length is greater than or equal to or for simplicity, we can say less
than 26. Then we will return false. You know why I'm doing? If your strings are
like very small, why would you? I mean very small. Meaning if your string
length is at least not 26, then it is not possible that it can be a pangram, because
in order to be a pangram, all the 26 characters should exist in this string.
So let me just run this. I've already done this and so this is accepted. Okay,
good. So this is. So string questions are like very important in the interviews
because it is really very easy to check the logic of the candidate. So thanks for
watching, guys. Bye bye. Take care. I'll see in the next videos.