Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Building in @Karl Knechtel's answer (and addressing your concern about supporting multiple alphabets):</p> <ul> <li><p>Create interfaces (say) AnagramKey and AnagramKeyFactory. Design the rest of the application to be agnostic of the type of key used.</p></li> <li><p>Create one implementation of the AnagramKey interface that internally uses an <code>int[]</code> to represent the character counts.</p></li> <li><p>Create a second implementation of the AnagramKey interface that uses a <code>HashMap&lt;Character, Integer&gt;</code> to represent the character counts.</p></li> <li><p>Create the corresponding factory interfaces.</p></li> <li><p>Choose between the two ways of representing the keys using a command line parameter, the Locale, or something else.</p></li> </ul> <p>Notes: </p> <ol> <li><p>It is not clear that "anagrams" make sense in the context of non-alphabetic languages, or for utterances that mix multiple languages into a "sentence". Also, I don't know whether anagrams in (say) French ignore the accents on characters. At any rate, I would be tempted to rule all of these cases as "out of scope" ... unless you have an explicit requirement to support them.</p></li> <li><p>The break-even density at which an <code>int[]</code> uses less space than a <code>HashMap&lt;Character, Integer&gt;</code> is asymptotically around 1 character in 15 across the range of characters in your count array. (Each entry in a HashMap with those key/value types occupies in the region of 15 32-bit words.) And that doesn't take account of the overheads of the <code>HashMap</code> node and the hash array node ... </p></li> <li><p>If you place limits on the length of the anagrams, you can save more space by using a <code>short[]</code> or even a <code>byte[]</code> for the character counts.</p></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload