From d96b6179a97a33ab6e3b51027670476e32a86b6c Mon Sep 17 00:00:00 2001 From: Abok Isaac Date: Tue, 25 Apr 2017 00:48:57 +0300 Subject: [PATCH] Added the set solution for str_diff_solution --- .../str_diff/str_diff_solution.ipynb | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/arrays_strings/str_diff/str_diff_solution.ipynb b/arrays_strings/str_diff/str_diff_solution.ipynb index 23b1471..c3610f2 100644 --- a/arrays_strings/str_diff/str_diff_solution.ipynb +++ b/arrays_strings/str_diff/str_diff_solution.ipynb @@ -31,7 +31,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Constraints\n", + "# Constraints\n", "\n", "* Can we assume the strings are ASCII?\n", " * Yes\n", @@ -76,6 +76,15 @@ "\n", "Complexity:\n", "* Time: O(m+n), where m and n are the lengths of s, t\n", + "* Space: O(1)\n", + "\n", + "### Set - Difference\n", + "\n", + "* A set is an unordered collection of unique elements.\n", + " * Return a string of a new set with an element in set(t) that is not in set(s).\n", + "\n", + "Complexity:\n", + "* Time: O(m+n), where m and n are the lengths of s, t\n", "* Space: O(1)" ] }, @@ -88,7 +97,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 19, "metadata": { "collapsed": false }, @@ -122,7 +131,13 @@ " result ^= ord(char)\n", " for char in str2:\n", " result ^= ord(char)\n", - " return chr(result)" + " return chr(result)\n", + "\n", + " def find_diff_set(self, str1, str2):\n", + " if str1 is None or str2 is None:\n", + " raise TypeError('str1 or str2 cannot be None')\n", + " else:\n", + " return ''.join(set(str2) - set(str1))" ] }, { @@ -134,7 +149,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 20, "metadata": { "collapsed": false }, @@ -173,7 +188,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 21, "metadata": { "collapsed": false }, @@ -192,10 +207,11 @@ } ], "metadata": { + "anaconda-cloud": {}, "kernelspec": { - "display_name": "Python 3", + "display_name": "Python [Root]", "language": "python", - "name": "python3" + "name": "Python [Root]" }, "language_info": { "codemirror_mode": { @@ -207,7 +223,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.0" + "version": "3.5.2" } }, "nbformat": 4,