- 2 days ago
Transcript
00:00Next question, which we are going to solve, is called valid anagram. What are the anagrams? Anagrams are words that
00:07are the rearrangement of another. If I write AB and BA, then AB and BA will be both anagrams. They
00:16are both made by the same number of letters and made by the same letters. Number of letters means that
00:22if this string is S and this string is T, then in S and T both have the same number
00:27of letters.
00:29And the value of letters is the same. So, which letters are used in S? A or B. And which
00:36letters are used in T? A or B. And if both are used in A or B. So, this is
00:40the same. So, the anagram is those strings, which are the number of letters are the same. And the letters
00:45used the quantity and the value of letters are the same.
00:49So basically, if there are two strings S and T anagrams, then I have to return true, not then I
00:55have to return false. You have a definition of anagrams, such a word or phrase, which is formed by rearranging
01:02the letters of a different word or phrase. Typically, all the original letters are exactly once used. So if the
01:10first string comes in A one time, you cannot say A, A, B, it will be an anagram. This is
01:14not anagram. It should also be A one time.
01:18Let's see some examples here. First example is S value R A C Erase and T which is stored in
01:25the string C A R E. Now I will return true for this. Because here R is also coming, here
01:31A is also coming, here A is also coming, C is also coming.
01:38So basically all the letters are the same. Here are 4 letters and here are 4 letters. And after that
01:46the number of letters are the same. If I talk about heart and earth then we will return true to
01:51this. Now, as I talk about tulip and lipid, the number of letters are 5. But here is T which
02:00does not exist in my lipid.
02:03So the same number of letters is not. That is why in this case we will return false. So I
02:07basically have to track that in my first string and second string, what is the frequency of numbers, what is
02:13the count and what is the value of numbers. So what will I do? I will use a hash map
02:18here. Hash map which type will be? In the hash map, I will store my character and its frequency for
02:28that character. So I can make this character and I can make this character.
02:34integer type. Let's say I created a hash map in which I track characters and their frequencies. So what will
02:41I do? The characters and their frequencies will store in my map. This is my map. In S, I will
02:50store K. What will I do for K? I will store K. And in pair, I will add value as
02:56frequency. Then I will go to N. I will store its frequency which is 1.
03:02Then I store the frequency of e to e. Then I store the frequency of e to e. Then I
03:08store the frequency of e to e. Then I store the frequency of e to e. Then I start to
03:13traverse the frequency of e to e to e to e to e to e to e to e to
03:20e to e to e to e to e to e to e to e to e to e to
03:21e to e to e to e to e to e
03:320. As the frequency is 0, it will automatically remove my map. After that, I will go to E.
03:38What will I do for E? I will reduce the frequency of 1. After that, I will come back to
03:42E. I will reduce the frequency of 0. So what will happen? 0 will break my map. After that, I
03:51will come back to N. What will I do for N? I will reduce the frequency of 0. And N
03:55will also break my map. If my map is empty, what do I do for is empty?
04:02So what do I want to return from there? I want to return from there. But if asmt returned from
04:08there, what do I want to return from there? It is false. So the second string, like a character, will
04:14reduce the frequency of my map and remove the elements. So after that, after last, like t, I need to
04:20finish my s also. So my map should be empty. In that case, it will be true or not it
04:24will be false. Besides, if there is any condition in it, what do I do every time? Every time I
04:30run a loop.
04:31i equals zero to t dot length. So, every time I will traverse the t length. I will take here
04:39ch as my t dot character at i. If in my map,
04:47My ch character doesn't exist. So let's assume that there was Keen here. There was actually Keel. So Keel is
04:55cut in Keel. When it comes to L, he will see that there is L in the map. Because there
04:59was L in S. So what will happen in that case? I will know that there is a different character
05:04that exists in S. So in that case I have to return false. So if I got it, what do
05:10I do? I will decrease the frequency. And if I didn't get it, I will return it.
05:16false value को, इसको एक बार में हटा देती हूँ, मेरा first step हुआ to add character and frequency pair
05:25of s in map second step हुआ कि loop करना है मुझे on t string
05:35And looping in T string, my process will be something like that. I will remove T every time I will
05:39remove my character and see if it exists in the map. If it exists, then use its frequency to minus.
05:46As the frequency is 0, we will remove it from the map. But if it exists not, then I will
05:52return false. That means that it is only in T or in S. And the third step is that I
05:58will check in the last step if my map is empty.
06:02So in this case, return to true and if the map is empty, return to false because the map is
06:07empty if the elements and the characters are the same. Let's see an example here, when the element is not
06:15the same. So here, I take the value of S, and I take the value of T, U, L, I,
06:19P, I, D. In this case, my map will show something.
06:28First step is that I will add all the characters and its frequencies. First I went to T, so T
06:33added frequency to 1. Then I went to U, so U added frequency to 1. Then I went to L,
06:39so L added frequency to 1. Then I went to I, so I added frequency to 1. Then I went
06:45to P, so P added frequency to 1. Now what did I do? T added loop.
06:50I have a loop on T and I have a loop on T and I have a loop on L.
06:56I will check for L already exists and its frequency is 1. So I will change the frequency to 0
07:03and it will change my map. After that, I will come to I. The frequency is 1. The frequency will
07:09decrease by 1 and it will change my map. After that, I will come to I. The frequency is 1.
07:09The frequency will decrease by 1 and it will change my map. After that, I will come to P.
07:16For p, we will see that p already exists, its frequency is 1. I will also remove it because the
07:21frequency will decrease to 0. After that, I will come back to i. For i, we will see that i
07:27exists in the map. It means that its frequency doesn't match. So in this case, what do I do? In
07:34this case, I will know that if i exists in the map, I will return false. So from here, I
07:41will return false as my answer. For this, we will write code.
07:45I have my main classroom class in which I have a string in which I have a string in which
07:50I have a string in which I have a string in which I have a string in which I have
07:52a function. Public static boolean is anagram. The work of this anagram is to find out that there are two
08:02numbers anagrams or not. So this string will take s and the other string will take t.
08:07In the beginning, I will create a hash map. First, the key will be a character type. And the value
08:15will be a integer type. I will store a character from the string. Integer from the frequency. Now, I will
08:21give it a name here. Map, new hash map. First, let's put a loop on s. I is equal to
08:280 from i less than s dot length. I am going to put my loop in the map.
08:37character s, meaning s dot char at i. And with that, now I have to use that thing here, which
08:44I learned in the last question. I want to increase frequency every time. If the number doesn't exist, then frequency
08:50will be 0. If it exists, then frequency will be plus 1. So I will do map dot get or
08:56default, which I will pass the key as s dot character at i, meaning my character ch. And here I
09:04will pass the default value as 0.
09:06And here I will do the plus one. I have written the same line as the last line. In fact,
09:11I can define my character CH here. For this, I will write CH and here I will also write CH.
09:24Let's understand this one.
09:25I have removed my character in the ith index. I have to put my character in the map. And what
09:30do I have to do with my ith index character? If it doesn't exist, then 0 will come as default
09:35value. I will add 1, then 1 will come as frequency. And if it exists, then I will add 1
09:40in the previous frequency. Now I have to run the second loop on my t. For int i equals 0,
09:47i less than t dot length i++. I will check every time that if my map
09:56value exist kerti hai. Mere t.carat i ki. In fact, isko bhi mein ch ki form me store kar
10:03leeti ho. My character ch is equal to this value. Agar ch ki value exist kerti hai already. Isko mein
10:10keha sakati ho not equal to null. Toh us case mein mujhe kya karna hai? Us case mein mujhe frequency
10:15ko kam karna hai. Ya agar map ki frequency agar one hai. Toh us case mein toh directly usko remove
10:24kar deena element ko. Remove.
10:28This key. No, so in the other case, I have to decrease in the map frequency by 1. If in
10:47my map, my ch key exists, then in that case, not null value will come. If not null value will
10:52come, then what do I have to do? If frequency 1 is, then completely remove the element. In that case,
10:57we will make the frequency first 0, then remove it. No, we can remove it directly.
11:01and if the frequency is greater than 1 then we have to take the frequency and we have to minus
11:05it by 1 and we have to put it again with my key ch the else case will be that
11:10if map.get gives me null value then I will directly return false false return means that in t there
11:17is no character that exists in s in that case there are anagrams in that case what do I do
11:23in last? I will check that
11:27Map.isempty, what do I return? If map.isempty will return true, then I will return true. Because map is
11:34empty, so if both anagrams return false, then map is empty. In S, there were extra characters exist, which are
11:41also in map. So in that case, I have to return false. Let's call call. is anagram snt. Save and
11:49run. I have an output of true for snt, but if I change them, I will do tulip.
12:01And I will do lipid. So in this case, the output would be false. In some way. Here, the time
12:10complexity would be big of n. Where n will be the length of s and t. In fact, I can
12:18check in the beginning that if the length of s
12:22is not equal to T's length. Both length is not the same. In that case, I can't have anagrams in
12:28that case. So what do I have to do in that case? Directly, I can return false here. In that
12:33case, I need to create a map or a frequency. I need to track directly. So we will return false.
12:38Time complexity will be coded in N where N is the number of characters.
12:43in my string. But if we talk about worst case, then obviously we know that in the worst case, I
12:47have told you that if hash function internally works like this, the bucket index is the same, which is a
12:53very rare case. In that rare case, the frequency of my n2 can go to the worst case, but usually
13:01it doesn't happen, so if you write this hash map code on any coding platform, then hash maps and hash
13:06sets of code is calculated by constant time and will accept on the platforms.