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.
take second string into stringbuilder and if char from first string is present in second string remove char at that position from stringbuilder if char is not present in second string then increment count final answer is count.... thanks DanLuo
I did something similar.Converted String to StringBuffers sb1 and sb2.
Upon iterating characters through sb1:
if the character is found in sb2,then change that character value in sb2 to some other value(say '/') so that it couldn't be found in further iterations.
This is essential as sb1 may have repeating characters.
I understood the problem in your code but I a, little bit confused about the mentioned solution. actually I am not much familier with Java, I am currently learning, So can you please explain me the solution??
I failed with c++ because of map.count(element). when the element count is 0, it still registers as a hit. Somewhat confusing if you choose to use this language reference. its better to check with if(map.count(element) && map.count(element) != 0). good problem.
Anagram
You are viewing a single comment's thread. Return to all comments →
Guys TC 1 an 14 executed but others failed. when i tested in Netbeans IDE i dont find any prob. can anyone look at it and leem know the issue .
if there is 2 "a" in str1, but only 1 "a" in str2, diff will not increase
can you please explain this further
take second string into stringbuilder and if char from first string is present in second string remove char at that position from stringbuilder if char is not present in second string then increment count final answer is count.... thanks DanLuo
I did something similar.Converted String to StringBuffers sb1 and sb2. Upon iterating characters through sb1: if the character is found in sb2,then change that character value in sb2 to some other value(say '/') so that it couldn't be found in further iterations. This is essential as sb1 may have repeating characters.
Use stringbuilder for first string, remove the char from builder if exist and at the end, answer = string builder length.
Got it. thnq :)
I understood the problem in your code but I a, little bit confused about the mentioned solution. actually I am not much familier with Java, I am currently learning, So can you please explain me the solution??
but in that case also mine are passing test cases 1 and 14 but rest are failing
static int anagram(String s) { int result = 0; Map map = new HashMap();
for String xaxbbbxx I obtain 0.
Can someone one say me why?
you should take the string input inside the testcase loop.
Your code don't work for input aabacb
The last test case fails. Try with xaxbbbxx.
I failed with c++ because of map.count(element). when the element count is 0, it still registers as a hit. Somewhat confusing if you choose to use this language reference. its better to check with if(map.count(element) && map.count(element) != 0). good problem.
its not full solution
This wont work for: "fdhlvosfpafhalll" which is the input string in the first test case
Plz send the modified code