my HashSet syntax problem/comprehension

Greg Sully

Greenhorn

Posts: 18

1

posted 1 month ago

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

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

posted 1 month ago

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.

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.

Tim Driven Development

Piet Souris

Rancher

Posts: 1979

67

posted 1 month ago

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: 55670

161

posted 1 month ago

1

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

posted 1 month ago

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?