diff --git a/arrays_strings/compress/compress_solution.ipynb b/arrays_strings/compress/compress_solution.ipynb index aa11798..3f14c6e 100644 --- a/arrays_strings/compress/compress_solution.ipynb +++ b/arrays_strings/compress/compress_solution.ipynb @@ -18,7 +18,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Problem: Compress a string such that 'AAABCCDDDD' becomes 'A3B1C2D4'. Only compress the string if it saves space.\n", + "## Problem: Compress a string such that 'AAABCCDDDD' becomes 'A3BC2D4'. Only compress the string if it saves space.\n", "\n", "* [Constraints](#Constraints)\n", "* [Test Cases](#Test-Cases)\n", @@ -51,7 +51,7 @@ "* None -> None\n", "* '' -> ''\n", "* 'AABBCC' -> 'AABBCC'\n", - "* 'AAABCCDDDD' -> 'A3B1C2D4'" + "* 'AAABCCDDDD' -> 'A3BC2D4'" ] }, { @@ -60,26 +60,24 @@ "source": [ "## Algorithm\n", "\n", - "Since Python strings are immutable, we'll use a list of characters to build the compressed string representation. We'll then convert the list to a string.\n", - "\n", - "* Calculate the size of the compressed string\n", - " * Note the constraint about compressing only if it saves space\n", - "* If the compressed string size is >= string size, return string\n", - "* Create compressed_string\n", - " * For each char in string\n", - " * If char is the same as last_char, increment count\n", - " * Else\n", - " * Append last_char to compressed_string\n", - " * append count to compressed_string\n", - " * count = 1\n", - " * last_char = char\n", - " * Append last_char to compressed_string\n", - " * Append count to compressed_string\n", - " * Return compressed_string\n", + "* For each char in string\n", + " * If char is the same as last_char, increment count\n", + " * Else\n", + " * Append last_char and count to compressed_string\n", + " * last_char = char\n", + " * count = 1\n", + "* Append last_char and count to compressed_string\n", + "* If the compressed string size is < string size\n", + " * Return compressed string\n", + "* Else\n", + " * Return string\n", "\n", "Complexity:\n", "* Time: O(n)\n", - "* Space: O(n)" + "* Space: O(n)\n", + "\n", + "Complexity Note:\n", + "* Although strings are immutable in Python, appending to strings is optimized in CPython so that it now runs in O(n) and extends the string in-place. Refer to this [Stack Overflow post](http://stackoverflow.com/a/4435752)." ] }, { @@ -100,38 +98,18 @@ "def compress_string(string):\n", " if string is None or len(string) == 0:\n", " return string\n", - "\n", - " # Calculate the size of the compressed string\n", - " size = 0\n", - " last_char = string[0]\n", - " for char in string:\n", - " if char != last_char:\n", - " size += 2\n", - " last_char = char\n", - " size += 2\n", - "\n", - " # If the compressed string size is greater than\n", - " # or equal to string size, return original string\n", - " if size >= len(string):\n", - " return string\n", - "\n", - " # Create compressed_string\n", - " compressed_string = list()\n", + " result = ''\n", + " prev_char = string[0]\n", " count = 0\n", - " last_char = string[0]\n", " for char in string:\n", - " if char == last_char:\n", + " if char == prev_char:\n", " count += 1\n", " else:\n", - " compressed_string.append(last_char)\n", - " compressed_string.append(str(count))\n", + " result += prev_char + (str(count) if count > 1 else '')\n", + " prev_char = char\n", " count = 1\n", - " last_char = char\n", - " compressed_string.append(last_char)\n", - " compressed_string.append(str(count))\n", - "\n", - " # Convert the characters in the list to a string\n", - " return \"\".join(compressed_string)" + " result += prev_char + str(count)\n", + " return result if len(result) < len(string) else string" ] }, { @@ -167,7 +145,7 @@ " assert_equal(func(None), None)\n", " assert_equal(func(''), '')\n", " assert_equal(func('AABBCC'), 'AABBCC')\n", - " assert_equal(func('AAABCCDDDD'), 'A3B1C2D4')\n", + " assert_equal(func('AAABCCDDDD'), 'A3BC2D4')\n", " print('Success: test_compress')\n", "\n", "\n", @@ -202,21 +180,21 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.4.3" } }, "nbformat": 4, diff --git a/arrays_strings/compress/test_compress.py b/arrays_strings/compress/test_compress.py index 7ce5142..4a52b98 100644 --- a/arrays_strings/compress/test_compress.py +++ b/arrays_strings/compress/test_compress.py @@ -7,7 +7,7 @@ class TestCompress(object): assert_equal(func(None), None) assert_equal(func(''), '') assert_equal(func('AABBCC'), 'AABBCC') - assert_equal(func('AAABCCDDDD'), 'A3B1C2D4') + assert_equal(func('AAABCCDDDD'), 'A3BC2D4') print('Success: test_compress')