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.
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
Java Anagrams
You are viewing a single comment's thread. Return to all comments →
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.