Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Another option is:</p> <ol> <li>Strip all characters you don't care about from the string (punctuation, whitespace)</li> <li>Make it lowercase</li> <li>Sort the string</li> <li>Compare to the reference string (with <code>.equals</code>)</li> </ol> <p>I suspect your way is faster though.</p> <p>EDIT:</p> <p>Since @nibot disagrees with my even suggesting this, and I'm not one to argue back and forth without proof, <a href="http://pastebin.com/evkL4qZe" rel="nofollow">here's three solutions</a>.</p> <p>They're all implemented very similarly:</p> <ol> <li>Convert line to lowercase</li> <li>Ignore non-alphabetic characters</li> <li>?</li> <li>Check of the result of 3. matches the result from the first line</li> </ol> <p>The ? part is one of:</p> <ul> <li>Make a <code>HashMap</code> of character counts</li> <li>Sorting the characters</li> <li>Making a 26-int array (the ultimate hash table solution, but only works for the Latin alphabet)</li> </ul> <p>I ran them all with this:</p> <pre><code>public static void time(String name, int repetitions, Function function, int expectedResult) throws Exception { long total = 0; for (int i = 0; i &lt; repetitions; i++) { System.gc(); long start = System.currentTimeMillis(); int result = function.call(); long end = System.currentTimeMillis(); if (result != expectedResult) { System.out.println("Oops, " + name + " is broken"); return; } total += end - start; } System.out.println("Executution of " + name + " took " + (total / repetitions) + " ms on average"); } </code></pre> <p>My file is similar to the one the OP posted, but made significantly longer, with a non-anagram about 20 lines from the end to ensure that the algorithms all work.</p> <p>I consistently get results like this:</p> <pre><code>Execution of testWithHashMap took 158 ms on average Execution of testWithSorting took 76 ms on average Execution of testWithArray took 56 ms on average </code></pre> <p>The <code>HashMap</code> one could be significantly improved if:</p> <ul> <li><a href="http://b010.blogspot.com/2009/05/speed-comparison-of-1-javas-built-in.html" rel="nofollow">There was a way to make a <code>HashMap&lt;char, int&gt;</code></a></li> <li>There was a way to specify the default value of in a <code>HashMap</code> and a way to get-and-increment (so there would only be one lookup instead of 2)</li> </ul> <p>But, these aren't in the standard library, so I'm ignoring them (just like most programmers using Java would).</p> <p>The moral of the story is that big O isn't everything. You need to consider the overhead and the size of <strong>n</strong>. In this case, <strong>n</strong> is fairly small, and the overhead of a <code>HashMap</code> is significant. With longer lines, that would likely change, but unfortunately I don't feel like figuring out where the break-even point is.</p> <p>And if you still don't believe me, consider that GCC uses <em><a href="http://en.wikipedia.org/wiki/Sort_%28C%2B%2B%29" rel="nofollow">insertion sort</a></em> in some cases in its C++ standard library.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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