|
|
|
|
|
by RogerL
4666 days ago
|
|
Okay, now make it faster. I'm not trying to be obnoxious, but your answer is not optimal. Which is not a big deal, and it would give us a chance to talk about ways of optimizing code. Sooner or later we'd figure out if you knew what a hash table is and how to use it (no real points off if you never really got, under the pressure of an interview, the hash table solution to this problem), how you would go about profiling code, how you might solve this if they weren't strings but social security numbers, and so on. It's a launching off point to more interesting discussions. At least that's how I'd use it in an interview. Before long we'd be talking about a, I dunno, search engine or something. |
|
Hashtable, huh? I don't like them because they're synchronized, but let's use a HashMap instead. Regardless, I'm stumped.
Let me think out loud. You'd probably want me to stick two entries in there with the words being the values. I suspect the keys are irrelevant. But I need the values themselves sorted prior to being placed in the map. Lets assume I stick the char arrays in the map. I still can't see how the map helps. Unless, I abandon the HashMap and go with a TreeMap instead. That negates the need to explicitly sort and maybe that's the time savings, but....
My head hurts. :-)
I don't know. I'd have to code this out and experiment a bit. But I like the push. You're forcing me to dig deeper.
PS. I can't build a search engine.
EDIT: A quick search of the net yields at least one answer (don't know if this works):
"We can solve this problem in O(n) time by using hashtable. That is, create a hashtable which record the times that the characters a-Z appear in S1 and create another hashtable for S2. If the two hashtables have the same content, then the strings are identical. This solution requires O(n) time to create two hashtables and compare them, and O(52) = O(1) space to store the hashtable."
I'm so disgusted. I was way off. Not even close. I wasn't even thinking in this direction. Does anyone have the number to that truck driving school?