2015-05-01 05:45:08 +08:00
{
"cells": [
2015-06-18 04:36:55 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<small><i>This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://bit.ly/code-notes).</i></small>"
]
},
2015-05-01 05:45:08 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-05-03 05:14:29 +08:00
"## Problem: Implement an algorithm to determine if a string has all unique characters\n",
"\n",
"* [Clarifying Questions](#Clarifying-Questions)\n",
"* [Test Cases](#Test-Cases)\n",
"* [Algorithm 1: Sets and Length Comparison](#Algorithm-1:-Sets-and-Length-Comparison)\n",
"* [Code: Sets and Length Comparison](#Code:-Sets-and-Length-Comparison)\n",
"* [Algorithm 2: Hash Map Lookup](#Algorithm-2:-Hash-Map-Lookup)\n",
"* [Code: Hash Map Lookup](#Code:-Hash-Map-Lookup)\n",
"* [Algorithm 3: In-Place](#Algorithm-3:-In-Place)\n",
"* [Code: In-Place](#Code:-In-Place)"
2015-05-01 05:45:08 +08:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Clarifying Questions\n",
"* Is the string in ASCII (extended?) or Unicode? \n",
2015-05-03 05:14:29 +08:00
" * ASCII extended, which is 256 characters\n",
2015-05-01 05:45:08 +08:00
"* Can you use additional data structures? \n",
" * Yes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
2015-05-03 05:14:29 +08:00
"* '' -> True\n",
"* 'foo' -> False\n",
"* 'bar' -> True"
2015-05-01 05:45:08 +08:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-05-03 05:14:29 +08:00
"## Algorithm 1: Sets and Length Comparison\n",
2015-05-01 05:45:08 +08:00
"\n",
2015-05-03 05:14:29 +08:00
"A set is an unordered collection of unique elements. \n",
2015-05-01 05:45:08 +08:00
"\n",
2015-05-03 05:14:29 +08:00
"* If the length of the set(string) equals the length of the string\n",
" * Return True\n",
"* Else\n",
" * Return False\n",
" \n",
2015-05-01 05:45:08 +08:00
"Complexity:\n",
2015-05-03 05:14:29 +08:00
"* Time: O(n)\n",
"* Space: Additional O(m), where m is the number of unique characters in the set"
2015-05-01 05:45:08 +08:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-05-03 05:14:29 +08:00
"## Code: Sets and Length Comparison"
2015-05-01 05:45:08 +08:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def unique_chars(string):\n",
2015-06-20 11:07:35 +08:00
" return len(set(string)) == len(string)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def test():\n",
" print(unique_chars(''))\n",
" print(unique_chars('foo'))\n",
" print(unique_chars('bar'))\n",
2015-05-01 05:45:08 +08:00
"\n",
2015-06-20 11:07:35 +08:00
"test()"
2015-05-01 05:45:08 +08:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-05-03 05:14:29 +08:00
"## Algorithm 2: Hash Map Lookup\n",
2015-05-01 05:45:08 +08:00
"\n",
2015-05-03 05:14:29 +08:00
"We'll keep a hash map (set) to keep track of unique characters we encounter. \n",
"\n",
"Steps:\n",
"* Scan each character\n",
2015-05-01 05:45:08 +08:00
"* For each character:\n",
2015-05-03 05:14:29 +08:00
" * If the character does not exist in a hash map, add the character to a hash map\n",
" * Else, return False\n",
2015-05-01 05:45:08 +08:00
"* Return True\n",
"\n",
2015-05-03 05:14:29 +08:00
"Notes:\n",
"* We could also use a dictionary, but it seems more logical to use a set as it does not contain duplicate elements\n",
"* Since the characters are in ASCII, we could potentially use an array of size 128 (or 256 for extended ASCII)\n",
"\n",
"Complexity:\n",
"* Time: O(n)\n",
"* Space: Additional O(m), where m is the number of unique characters in the hash map"
2015-05-01 05:45:08 +08:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-05-03 05:14:29 +08:00
"## Code: Hash Map Lookup"
2015-05-01 05:45:08 +08:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def unique_chars_alt(string):\n",
2015-05-03 05:14:29 +08:00
" chars_set = set()\n",
2015-05-01 05:45:08 +08:00
" for char in string:\n",
2015-05-03 05:14:29 +08:00
" if char in chars_set:\n",
2015-05-01 05:45:08 +08:00
" return False\n",
2015-05-03 05:14:29 +08:00
" else:\n",
" chars_set.add(char)\n",
2015-06-20 11:07:35 +08:00
" return True\n",
"\n",
"test()"
2015-05-01 05:45:08 +08:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-05-03 05:14:29 +08:00
"## Algorithm 3: In-Place\n",
"\n",
2015-06-20 11:07:35 +08:00
"Assume we cannot use additional data structures, which will eliminate the fast lookup O(1) time provided by our hash map. \n",
2015-05-03 05:14:29 +08:00
"* Scan each character\n",
"* For each character:\n",
" * Scan all [other] characters in the array\n",
" * Exluding the current character from the scan is rather tricky in Python and results in a non-Pythonic solution\n",
" * If there is a match, return False\n",
"* Return True\n",
"\n",
"Algorithm Complexity:\n",
"* Time: O(n^2)\n",
"* Space: In-place"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code: In-Place"
2015-05-01 05:45:08 +08:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
2015-05-03 05:14:29 +08:00
"def unique_chars_alt(string):\n",
" for char in string:\n",
" if string.count(char) > 1:\n",
" return False\n",
2015-06-20 11:07:35 +08:00
" return True\n",
"\n",
"test()"
2015-05-01 05:45:08 +08:00
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
2015-06-18 04:36:55 +08:00
"version": "2.7.10"
2015-05-01 05:45:08 +08:00
}
},
"nbformat": 4,
"nbformat_minor": 0
}