Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>my effort was as follows trying to eliminate unnecessary string compares, i read isaacs blog but it is in c how would this translate to c#, i even went as far as using arrays everywhere to avoid the string immutability factor but there was no improvement</p> <pre><code> int Rtn = 0; int len = S.Length; if (len &gt; 1000000000) return 1000000000; for (int i = 1; i &lt;= len; i++) { string tofind = S.Substring(0, i); int tofindlen = tofind.Length; int occurencies = 0; for (int ii = 0; ii &lt; len; ii++) { int found = FastIndexOf(S, tofind.ToCharArray(), ii); if (found != -1) { if ((found == 0 &amp;&amp; tofindlen != 1) || (found &gt;= 0)) { ii = found; } ++occurencies ; } } if (occurencies &gt; 0) { int total = occurencies * tofindlen; if (total &gt; Rtn) { Rtn = total; } } } return Rtn; } static int FastIndexOf(string source, char[] pattern, int start) { if (pattern.Length == 0) return 0; if (pattern.Length == 1) return source.IndexOf(pattern[0], start); bool found; int limit = source.Length - pattern.Length + 1 - start; if (limit &lt; 1) return -1; char c0 = pattern[0]; char c1 = pattern[1]; // Find the first occurrence of the first character int first = source.IndexOf(c0, start, limit); while ((first != -1) &amp;&amp; (first + pattern.Length &lt;= source.Length)) { if (source[first + 1] != c1) { first = source.IndexOf(c0, ++first); continue; } found = true; for (int j = 2; j &lt; pattern.Length; j++) if (source[first + j] != pattern[j]) { found = false; break; } if (found) return first; first = source.IndexOf(c0, ++first); } return -1; } </code></pre>
 

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