Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Here is a problem given in my class:

Java
boolean stringIntersect(String a, String b, int len)

Given 2 strings, consider all the substrings within them of length len. Returns true if there are any such substrings which appear in both strings. Compute this in O(n) time using a HashSet. Len will be 1 or more.

Java
T Collections int sameCount(Collection T a, Collection T b)

In the Appearances class, the static Appearances.sameCount() method takes in two collections -- A and B -- containing a generic element type. Assume that the T elements implement equals() and hashCode() correctly, and so may be compared and hashed. The elements are in no particular order. Every element in A appears in A one or more times, using .equals() to compare elements for equality (the standard definition of equals() for java collections). Likewise every element in B appears one or more times. sameCount() counts the number of elements that appear in both collections the same number of times.

For example, with the collections {"a", "b", "a", "b", "c"} and {"c", "a", "a", "d", "b", "b", "b"} it returns 2, since the "a" and the "c" appear the same number of times in both collections. Use hashing to compute the number of appearances efficiently.

I have couple of questions about this.

in the boolean stringIntersect(String a, String b, int len) how can I load a string in a hashset? Should i insert each letter to a hashset? if so then what about the duplicate letters? I can compare elements and do the tasks of T Collections int sameCount(Collection T a, Collection T b) I guess.

There's is a parameter "len" in "boolean stringIntersect(String a, String b, int len)". But in the example the two strings are of different length. So what will be "len"?
Posted
Comments
PIEBALDconsult 7-Nov-14 12:52pm    
" So what will be "len"? "
Ask your teacher.
Sergey Alexandrovich Kryukov 7-Nov-14 12:52pm    
Not clear what your concern is. You don't "load", you add an element to a hash set. A character or a string? It's up to you. What's unclear about those hash sets?

The second question makes no sense. If this your function, design it the way you like. If it's not yours, ask the one who delivered this function. What length to use when using a function? Again, it depends on what you want.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900