{"id":942,"date":"2019-03-17T14:40:52","date_gmt":"2019-03-17T18:40:52","guid":{"rendered":"https:\/\/resrvoir.com\/?page_id=942"},"modified":"2019-07-15T18:32:55","modified_gmt":"2019-07-15T22:32:55","slug":"string-compression","status":"publish","type":"page","link":"https:\/\/resrvoir.com\/?page_id=942","title":{"rendered":"String Compression"},"content":{"rendered":"\n<p>Implement simple string compression where 4 or more identical characters in sequence produces [Character]#[count] in a compressed output string. Consecutive identical characters of count 3 or less are left as-is in the output string. For example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ABC           -> ABC\nAAABBBCCC     -> AAABBBCCC\nAAAABBBBBC    -> A#4B#5C\nAAAABBBBBCC   -> A#4B#5CC\nAAAABBBBBCCC  -> A#4B#5CCC\nAAAABBBBBCCCC -> A#4B#5C#4<\/pre>\n\n\n\n<p>The approach taken here is to count identical characters in a row and when the current character is different from the previous (or we get to the end of the string), generate more of the output string. If 4 or more identical characters are counted, concatenate to the output string using the #[Count] notation. Otherwise concatenate the actual character 3 or fewer times.<br><br>For n characters, the runtime is O(n). Each character in the input loop is visited once. On the output, any identical character sequence produces at most 3 characters on the output.<br><br>The output worst case is a string with m identical character sequences of length 3. m * 3 = n chars on the output. So, each input char is visited and n chars are also produced on the output. n inputs + n outputs = 2n total chars = O(n).<br><br>Finally, for any input string length n, with one or more identical character sequence where count >= 4, the output string length will be less than n, since any count >= 4 results in its output representation being 3 characters.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">func encode(_ input: String) -> String {\n  \n  var count:Int = 0\n  var output:String = \"\"\n  \n  guard var char = input.first else { return \"\" }\n  \n  for c in input {\n    if c == char {\n      count = 1 + count\n    }\n    else {\n      output += subencode(char:char, count:count)\n      char = c\n      count = 1\n    }\n  }\n\n  output += subencode(char:char, count:count)\n\n  return output\n}\n\nfunc subencode(char:Character, count:Int) -> String {\n  var output = \"\"\n\n  if count > 3 {\n    output += \"\\(char)#\\(count)\"\n  }\n  else {\n    for _ in 0 ..&lt; count {\n      output += \"\\(char)\"\n    }\n  }\n  \n  return output\n}\n\nprint(encode(\"\"))\nprint(encode(\"ABC\"))\nprint(encode(\"AAABBBCCC\"))\nprint(encode(\"AAAABBBBBC\"))\nprint(encode(\"AAAABBBBBCC\"))\nprint(encode(\"AAAABBBBBCCC\"))\nprint(encode(\"AAAABBBBBCCCC\"))<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Implement simple string compression where 4 or more identical characters in sequence produces [Character]#[count] in a compressed output string. Consecutive identical characters of count 3 or less are left as-is in the output string. For example: The approach taken here is to count identical characters in a row and when the current character is different [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":2,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"_links":{"self":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/942"}],"collection":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/resrvoir.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=942"}],"version-history":[{"count":24,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/942\/revisions"}],"predecessor-version":[{"id":1000,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/942\/revisions\/1000"}],"up":[{"embeddable":true,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/2"}],"wp:attachment":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=942"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}