• Post Reply Bookmark Topic Watch Topic
  • New Topic

my HashSet syntax problem/comprehension  RSS feed

 
Greg Sully
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello masters
here I have a question from a site : Given two strings, and , that may or may not be of the same length, determine the minimum number of character deletions required to make and anagrams. Any characters can be deleted from either of the strings.
and my code :


while the first string is : imkhnpqnhlvaxlmrsskbyyrhwfvgteubrelgubvdmrdmesfxkpykprunzpustowmvhupkqsyjxmnptkcilmzcinbzjwvxshubeln
and second String : wfnfdassvfugqjfuruwrdumdmvxpbjcxorettxmpcivurcolxmeagsdundjronoehtyaskpwumqmpgzmtdmbvsykxhblxspgnpgfzydukvizbhlwmaajuytrhxeepvmcltjmroibjsdkbqjnqjwmhsfopjvehhiuctgthrxqjaclqnyjwxxfpdueorkvaspdnywupvmy
the output is 102 but mine is 0.
as I understand the question it should be "0" and seem Im wrong.
so why has been 102 ? I checked they all same letters :-s

Thanks
 
Paul Clapham
Sheriff
Posts: 22829
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what your algorithm is supposed to be doing, but I'm totally confused by the question you asked about it. I can't tell whether the output of your code was 0 or 102, and I can't tell whether you were supposed to get 0 or 102, and I can't tell whether you got something different than what you thought you were supposed to get. And for that matter I'm not sure what the question is either... Did you copy and paste it from that site, or did you retype what you thought it said?
 
Greg Sully
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
forgive me for my bad explanation and details.
for example if :

the output will be 4 cause remove d and e from cde to get c and remove a and b from abc to get c. now they are anagrams of each other.
the tow Strings in first post are from the site.it randomly input some kind of String to check my code.
I think it should be 0 cause there isnt any diffrent between two Strings in first post but the answer is 102..here I confused
and yes its as copy from that site.
 
Tim Cooke
Marshal
Posts: 4044
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the purposes of copyright and correct attribution can you provide a link through to the site that this question has come from please?

I might approach this problem by representing each String as a Map of character to character count. Let's call the two strings s1 and s2, if s1 has 2 'a' characters and s2 has 5 'a' characters then I have to remove 3 characters from s2. Repeat for all found characters, sum the character deletions.

Does that make sense? Try it out and see how you get on.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the two strings in your opening post differ in length, there must be some removal of characters for the remaining strings to be anagrams. So, 0 is certainly incorrect.

Suppose you have two frequencymaps of the chars in both strings, and say you have a Set that contains all the chars in both strings.
For instance: string 1 has the map [ (a, 3), (b, 4) ], string 2 has the map [ (b, 2), (c, 1) ], and Set S = [a, b, c].
What to do when a char from S is present in both frequency maps, or only in one of the two?
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I personally think you need to work out what sort of algorithm you are going to use before you try any coding. As Paul C says, you need to understand the problem first.

I can see things wrong with your first approach. Start by working out what the output will be if you put the letters from the following two Strings into a Set<Character>:-
  • 1: Coderanch
  • 2:CCooddddddddeerrranchhhhhhhh
  • I think that will show you that Sets won't work in this occasion. Tim's suggestion looks better. Hint, hint: there is a nice section about Maps in the Java™ Tutorials.
     
    Greg Sully
    Greenhorn
    Posts: 18
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Tim Cooke wrote:For the purposes of copyright and correct attribution can you provide a link through to the site that this question has come from please?

    https://www.hackerrank.com/challenges/ctci-making-anagrams
    Tim Cooke wrote:
    Let's call the two strings s1 and s2, if s1 has 2 'a' characters and s2 has 5 'a' characters then I have to remove 3 characters from s2.

    but all are the same('a') right ? does should deleted when they are same ? I thought if String s1 = "aaa" and String s2 = "aaaaaaa" the result must be 0 cause all the same.

    Thanks Piet Souris and Campbell  for information.. the question guaranteed that two string consist of lowercase .
    I think I misundesrtood the meaning of anagrams.

    Ill checked as Tim said.

    Thanks
     
    Tim Cooke
    Marshal
    Posts: 4044
    239
    Clojure IntelliJ IDE Java
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It's certainly worth completely understanding what an Anagram is.

    Wikipedia wrote:An anagram is direct word switch or word play, the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once


    So on that basis "aaa" is not an anagram of "aaaaaaa".
     
    Campbell Ritchie
    Marshal
    Posts: 56546
    172
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hackerrank is supposed to be good, but I have never used it
    . . . Thanks Piet Souris and Campbell . . .
    That's a pleasure
    .. the question guaranteed that two string consist of lowercase . . . .
    Although anagrams are usually written as case‑insensitive, it doesn't make any difference to the point I was trying to make.
     
    Greg Sully
    Greenhorn
    Posts: 18
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    My problem has been solved(it pass all 17 test successfully) . Im sure it could be easier than I wrote .. it should be more brief and functional.

    if is there any big edit needed let me know.

    Thanks
     
    Piet Souris
    Master Rancher
    Posts: 2044
    75
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No need for an edit, the code is clear and straightforward. Compliments and enjoy a cow!

    This is the code that I wrote about. It is a little shorter, but it does exactly the same.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!