mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Cleaned up string compress notebook. Refactored duplicate algorithm description.
This commit is contained in:
parent
ef6c382a90
commit
e8060f4b6e
|
@ -4,7 +4,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Problem: Compress a String Such that 'AAABCCDDDD' Becomes 'A3B1C2D4'. Only Compress if it Saves Space.\n",
|
||||
"## Problem: Compress a string such that 'AAABCCDDDD' becomes 'A3B1C2D4'\n",
|
||||
"\n",
|
||||
"* [Clarifying Questions](#Clarifying-Questions)\n",
|
||||
"* [Test Cases](#Test-Cases)\n",
|
||||
|
@ -25,7 +25,9 @@
|
|||
"* Can you use additional data structures? \n",
|
||||
" * Yes\n",
|
||||
"* Is this case sensitive?\n",
|
||||
" * Yes"
|
||||
" * Yes\n",
|
||||
"* Do you compress even if it doesn't save space?\n",
|
||||
" * No"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -48,7 +50,7 @@
|
|||
"\n",
|
||||
"![alt text](https://raw.githubusercontent.com/donnemartin/algorithms-data-structures/master/images/compress_string.jpg)\n",
|
||||
"\n",
|
||||
"Since Python strings are immutable, we'll use a list to exercise array manipulation. Note using a list vs a bytearray will will result in additional space to create the list and to convert the list to a string.\n",
|
||||
"Since Python strings are immutable, we'll use a list of characters to exercise string manipulation. Note using a list vs a bytearray will will result in additional space to create the list and to convert the list to a string.\n",
|
||||
"\n",
|
||||
"* If string is empty return string\n",
|
||||
"* count = 0\n",
|
||||
|
@ -78,7 +80,7 @@
|
|||
"\n",
|
||||
"Complexity:\n",
|
||||
"* Time: O(n)\n",
|
||||
"* Space: O(m) where m is the size of the compressed bytearray"
|
||||
"* Space: O(2m) where m is the size of the compressed list and the resulting string copied from the list"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -145,35 +147,9 @@
|
|||
"\n",
|
||||
"![alt text](https://raw.githubusercontent.com/donnemartin/algorithms-data-structures/master/images/compress_string.jpg)\n",
|
||||
"\n",
|
||||
"Since Python strings are immutable, we'll use a bytearray to exercise array manipulation. We could use a list of characters to create the compressed string then convert it to a string in the end, but this will result in additional space.\n",
|
||||
"Since Python strings are immutable, we'll use a bytearray to exercise array manipulation. As seen above, we could use a list of characters to create the compressed string then convert it to a string in the end, but this will result in additional space.\n",
|
||||
"\n",
|
||||
"* If bytearray is empty return bytearray\n",
|
||||
"* count = 0\n",
|
||||
"* size = 0\n",
|
||||
"* last_char_code = first char code in bytearray\n",
|
||||
"* For each char code in bytearray\n",
|
||||
" * If char code == last_char_code\n",
|
||||
" count++\n",
|
||||
" * Else\n",
|
||||
" size += 2\n",
|
||||
" count++\n",
|
||||
" last_char_code = char code\n",
|
||||
"* size += 2\n",
|
||||
"* If the compressed bytearray size is >= bytearray size, return string\n",
|
||||
"* Create compressed_bytearray\n",
|
||||
"* pos = 0\n",
|
||||
"* For each char code in bytearray\n",
|
||||
" * If char code == last_char_code\n",
|
||||
" count++\n",
|
||||
" * Else\n",
|
||||
" * compressed_bytearray[pos] = last_char_code\n",
|
||||
" * compressed_bytearray[pos + 1] = count\n",
|
||||
" * pos += 2\n",
|
||||
" * count = 1\n",
|
||||
" * last_char_code = char code\n",
|
||||
" * compressed_bytearray[pos] = last_char_code\n",
|
||||
" * compressed_bytearray[pos + 1] = count\n",
|
||||
"* return compressed_bytearray\n",
|
||||
"The algorithm is the same, except we will need to work with the bytearray's character codes instead of the characters as we did above when we implemented this solution with a list.\n",
|
||||
"\n",
|
||||
"Complexity:\n",
|
||||
"* Time: O(n)\n",
|
||||
|
|
Loading…
Reference in New Issue
Block a user