Added the set solution for str_diff_solution

This commit is contained in:
Abok Isaac 2017-04-25 00:48:57 +03:00
parent 83ad7ff23a
commit d96b6179a9

View File

@ -31,7 +31,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Constraints\n", "# Constraints\n",
"\n", "\n",
"* Can we assume the strings are ASCII?\n", "* Can we assume the strings are ASCII?\n",
" * Yes\n", " * Yes\n",
@ -76,6 +76,15 @@
"\n", "\n",
"Complexity:\n", "Complexity:\n",
"* Time: O(m+n), where m and n are the lengths of s, t\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)" "* Space: O(1)"
] ]
}, },
@ -88,7 +97,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 19,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -122,7 +131,13 @@
" result ^= ord(char)\n", " result ^= ord(char)\n",
" for char in str2:\n", " for char in str2:\n",
" result ^= ord(char)\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", "cell_type": "code",
"execution_count": 2, "execution_count": 20,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -173,7 +188,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 21,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -192,10 +207,11 @@
} }
], ],
"metadata": { "metadata": {
"anaconda-cloud": {},
"kernelspec": { "kernelspec": {
"display_name": "Python 3", "display_name": "Python [Root]",
"language": "python", "language": "python",
"name": "python3" "name": "Python [Root]"
}, },
"language_info": { "language_info": {
"codemirror_mode": { "codemirror_mode": {
@ -207,7 +223,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.5.0" "version": "3.5.2"
} }
}, },
"nbformat": 4, "nbformat": 4,