We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
In this ,I am returning false in starting if the string length is not same. Now, when they are of same length, then I am simply taking two int arrays of size 26 (total number of alphabets), where each index represents the location of each alphabets. For example, 0-'A', 1-'B',......upto..,25-'Z'. Then i am making the whole string in uppercase to avoid Case sensitivity. Now i am traversing both the strings of equal length with a single loop and doing this simple operation:
" character at ith position of string - 'A' ". This gives me the supposed index location of each character in array.
For ex: if charAt(i) is 'A', then, 'A' - 'A' = 0, similarly, for B, 'B' - 'A' = 1.......upto...'Z' - 'A ' =25..Thus i get the indexes of the characters , and I increment the value of that index on each occurance of the character, which was initially 0.
Note: Array 'c' represents Occurances of each elements in string 'a', and array 'd' is for string 'b'.
Now we compare the occurances of each character in both the arrays. If any occurance is not equal then we return False. When all the occurances were same and for loop terminates , then we reutrn True.
can i tell me in details how occurance of last for loop works means
suppose c[0] has value 2 and d[0] has value 4 and if we are checking both in last for loop if condition then both will false because ther are not equal that thing i did not understant explain to me?? like Hello and llohe how it works for that
it's simple. If you get different values, then that means there are NOT EQUAL NUMBER of same character/alphabets. so it will immediately return false to the calling function, thus it won't go to the next ("return true" ) statement.
if we don't get any different numbers in both the arrays, then the forloop will finally be terminated and the next instruction after forloop will be excuted and return True to the calling function.
But my simple question is B-A will be equal to 1
that will be stored in your character array
and the next array will store D-A that will be equal to 2 ;
but when am trying to print it always print 0 & 1.
The interesting fact is count of 0 & 1 is always equal but are in different order .
If possible for you to explain how this comparison works?
I know i sound like a big idiot ..
But still if possible plz explain
To answer your question let's suppose you take CAT and TAC as input;
Now in the 1st for loop the total occurrence of each character of the given input has been subtracted by the integer array defined i.e c[] & d[];
If it's not an anagram then the value of c[] at ith occurrence would not be similar to d[] at the same ith occurrence.
plz tell me answer of this question
if(c[i] != d[i] ) return false;
it show that if i=0 in CAT and TAC
i on that here is C and T respectively then how code will prove that it is anagram.
NO ! its not how you are imagining it to be. what the code is doing is creating an array of 26 elements (for 26 alphabets in english). so c[0] = A, c[1] = B...and so on . now he is traversing the string and for each index in the string the value of that elements in the array increases by 1 . therefore, if there are 2 A's in the string then c[0] = 2 and so on . he does this for both the strings and then checks if the position and respective values match or not
if they match then they are anagrams else not
He converted the string into UpperCase and then in the loop he is subtracting the ASCII value of the character at ith position with A to get the difference which will be less than 26.
for example, peh and hpe were the inputs. After converting it to uppercase we get PEH and HPE.
c[a.charAt(i) - 'A']++;
This statement inside the loop fetches the character from the string
Pass1: c['P'-'A']++ will subtract the ASCII values of P and A i.e 80-65=15. It'll increase the value at c[15] by 1.
Basically c represents all the alphabets. c[0] will hold the frequency of A, c[1] will hold the frequncy of B,... c[15] will hold the frequency of P, and so on. Then in the last loop we're just comparing all the values in array and if they're not equal thenbreak the loop by returning false
can anyone explain me how this " I am simply taking two int arrays of size 26 (total number of alphabets), where each index represents the location of each alphabets. For example, 0-'A', 1-'B',......upto..,25-'Z'."
Java Anagrams
You are viewing a single comment's thread. Return to all comments →
Could you please explain your code a bit? From for loop(6th line onwards)
In this ,I am returning false in starting if the string length is not same. Now, when they are of same length, then I am simply taking two int arrays of size 26 (total number of alphabets), where each index represents the location of each alphabets. For example, 0-'A', 1-'B',......upto..,25-'Z'. Then i am making the whole string in uppercase to avoid Case sensitivity. Now i am traversing both the strings of equal length with a single loop and doing this simple operation:
" character at ith position of string - 'A' ". This gives me the supposed index location of each character in array.
For ex: if charAt(i) is 'A', then, 'A' - 'A' = 0, similarly, for B, 'B' - 'A' = 1.......upto...'Z' - 'A ' =25..Thus i get the indexes of the characters , and I increment the value of that index on each occurance of the character, which was initially 0.
Note: Array 'c' represents Occurances of each elements in string 'a', and array 'd' is for string 'b'.
Now we compare the occurances of each character in both the arrays. If any occurance is not equal then we return False. When all the occurances were same and for loop terminates , then we reutrn True.
Excelent code
Thanks, Happy to help
can i tell me in details how occurance of last for loop works means suppose c[0] has value 2 and d[0] has value 4 and if we are checking both in last for loop if condition then both will false because ther are not equal that thing i did not understant explain to me?? like Hello and llohe how it works for that
But my simple question is B-A will be equal to 1 that will be stored in your character array and the next array will store D-A that will be equal to 2 ;
but when am trying to print it always print 0 & 1.
The interesting fact is count of 0 & 1 is always equal but are in different order . If possible for you to explain how this comparison works?
I know i sound like a big idiot .. But still if possible plz explain
To answer your question let's suppose you take CAT and TAC as input; Now in the 1st for loop the total occurrence of each character of the given input has been subtracted by the integer array defined i.e c[] & d[]; If it's not an anagram then the value of c[] at ith occurrence would not be similar to d[] at the same ith occurrence.
plz tell me answer of this question if(c[i] != d[i] ) return false; it show that if i=0 in CAT and TAC i on that here is C and T respectively then how code will prove that it is anagram.
NO ! its not how you are imagining it to be. what the code is doing is creating an array of 26 elements (for 26 alphabets in english). so c[0] = A, c[1] = B...and so on . now he is traversing the string and for each index in the string the value of that elements in the array increases by 1 . therefore, if there are 2 A's in the string then c[0] = 2 and so on . he does this for both the strings and then checks if the position and respective values match or not
if they match then they are anagrams else not
Its count the frequency of index number not character.
same character will come then....
Thnx man... nice logic... nice explanation...
in the constraints it is given then length of string "a" as well as "b" can be upto 50 characters then how could you take the length of array as 26.
it is not lenght , it is the count of alphabets , couting them checking in both the arrays
thumbs up
Great logic !! never seen this method anywhere... nice job.
Thanks man, nice logic.
Please if anyone can explain the line "character at ith position of string - 'A' "
He converted the string into UpperCase and then in the loop he is subtracting the ASCII value of the character at ith position with A to get the difference which will be less than 26.
Sorry, but I didn't get you. If difference is found out, after then what is the logic
Brilliant code man !!!!!
Superb !!
nice explanation
Brilliant battlizer
what a code bro great
wow..!!!
can anyone explain me how this " I am simply taking two int arrays of size 26 (total number of alphabets), where each index represents the location of each alphabets. For example, 0-'A', 1-'B',......upto..,25-'Z'."
Very Good code bro...nice thinking..
Brilliant code ............
That's really an awesome approach..
Brilliant Code Man! What a logic!
amazing man
I had the same idea but I wrote it differently and my code was long.
Nice approach and thank you for sharing.
can you please explain the first part of the code
its checking the letter and storing the respective time it occurs in the slot