title: Hackerrank String Function Calculation
body: <p>I'm trying to solve the <a href="https://www.hackerrank.com/challenges/string-function-calculation" rel="nofollow">String Function Calculation problem from Hackerrank</a>. In this problem, we're given a string as input and asked to print a number that represents the maximum of the following function, among all substrings of the input string:</p>
  
  <h3>f(s, t) = number of times the substring 's' appears in string 't' * length of substring 's'</h3>
  
  <p>I submitted the following as an answer:</p>
  
  <pre><code>import Data.List
  
  main :: IO()
  main = do
      stringInput &lt;- getLine
      print $ solution stringInput
  
  solution :: String -&gt; Int
  solution input = maximum $ map sum $ map (map length) $ group $ sort $ substrings input
  
  substrings :: String -&gt; [String]
  substrings s = tail . inits =&lt;&lt; tails s
  </code></pre>
  
  <p>The idea was to:</p>
  
  <ol>
  <li><p>Get all substrings of <code>s</code>. <code>let s = "aaaaaa"; substrings s  = ["a","aa","aaa","aaaa","aaaaa","aaaaaa","a","aa","aaa","aaaa","aaaaa","a","aa","aaa","aaaa","a","aa","aaa","a","aa","a"]</code></p></li>
  <li><p>Sort it. <code>["a","a","a","a","a","a","aa","aa","aa","aa","aa","aaa","aaa","aaa","aaa","aaaa"
  ,"aaaa","aaaa","aaaaa","aaaaa","aaaaaa"]</code></p></li>
  <li><p>Group it. <code>[["a","a","a","a","a","a"],["aa","aa","aa","aa","aa"],["aaa","aaa","aaa","aaa"],["aaaa","aaaa","aaaa"],["aaaaa","aaaaa"],["aaaaaa"]]</code></p></li>
  <li><p>Get the individual lengths of each substring. <code>[[1,1,1,1,1,1],[2,2,2,2,2],[3,3,3,3],[4,4,4],[5,5],[6]]</code></p></li>
  <li><p>Sum the resulting lists. <code>[6,10,12,12,10,6]</code>.</p></li>
  <li><p>Get the maximum. <code>12</code>.</p></li>
  </ol>
  
  <p>This passes the preliminary tests. However, when I submit it it fails all other tests, by 'Runtime Error'. </p>
  
  <p>Test case no. 2, the first to fail, takes 1.47 seconds to run and has the following input:</p>
  
  <p><code>"aacbbabbabbbbbaaaaaaabbbbcacacbcabaccaabbbcaaabbccccbbbcbccccbbcaabaaabcbaacbcbaccaaaccbccbcaacbaccbaacbbabbabbbbbaaaaaaabbbbcacacbcabaccaabbbcaaabbccccbbbcbccccbbcaabaaabcbaacbcbaccaaaccbccbcaacbaccbaacbbabbabbbbbaaaaaaabbbbcacacbcabaccaabbbcaaabbccccbbbcbccccbbcaabaaabcbaacbcbaccaaaccbccbcaacbaccbaacbbabbabbbbbaaaaaaabbbbcacacbcabaccaabbbcaaabbccccbbbcbccccbbcaabaaabcbaacbcbaccaaaccbccbcaacbaccbaacbbabbabbbbbaaaaaaabbbbcacacbcabaccaabbbcaaabbccccbbbcbccccbbcaabaaabcbaacbcbaccaaaccbccbcaacbaccb"</code></p>
  
  <p>Could you help me figure out what I'm doing wrong, or what's going on?</p>