2015-05-01 05:45:08 +08:00
{
" cells " : [
2015-06-18 04:36:55 +08:00
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
2016-07-31 20:11:18 +08:00
" This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges). "
2015-07-04 07:57:24 +08:00
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" # Solution Notebook "
2015-06-18 04:36:55 +08:00
]
} ,
2015-05-01 05:45:08 +08:00
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
2016-12-18 20:20:35 +08:00
" ## Problem: Implement an algorithm to determine if a string has all unique characters. \n " ,
2015-05-03 05:14:29 +08:00
" \n " ,
2015-06-30 17:55:58 +08:00
" * [Constraints](#Constraints) \n " ,
2015-05-03 05:14:29 +08:00
" * [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 " ,
2015-06-25 06:25:48 +08:00
" * [Code: In-Place](#Code:-In-Place) \n " ,
" * [Unit Test](#Unit-Test) "
2015-05-01 05:45:08 +08:00
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
2015-06-28 19:13:27 +08:00
" ## Constraints \n " ,
2015-06-25 06:25:48 +08:00
" \n " ,
2016-06-13 11:19:09 +08:00
" * Can we assume the string is ASCII? \n " ,
2015-06-25 06:25:48 +08:00
" * Yes \n " ,
" * Note: Unicode strings could require special handling depending on your language \n " ,
2015-07-04 07:57:24 +08:00
" * Can we assume this is case sensitive? \n " ,
" * Yes \n " ,
2016-06-13 11:19:09 +08:00
" * Can we use additional data structures? \n " ,
" * Yes \n " ,
" * Can we assume this fits in memory? \n " ,
2015-05-01 05:45:08 +08:00
" * Yes "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Test Cases \n " ,
" \n " ,
2016-06-13 11:19:09 +08:00
" * None -> False \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 " ,
2015-07-05 21:24:33 +08:00
" * Space: Additional O(n) "
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 " ,
2015-06-25 06:25:48 +08:00
" execution_count " : 1 ,
2015-05-01 05:45:08 +08:00
" metadata " : {
" collapsed " : false
} ,
" outputs " : [ ] ,
" source " : [
2016-08-13 18:42:05 +08:00
" class UniqueCharsSet(object): \n " ,
" \n " ,
" def has_unique_chars(self, string): \n " ,
" if string is None: \n " ,
" return False \n " ,
" return len(set(string)) == len(string) "
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 " ,
2015-07-05 21:24:33 +08:00
" * Space: Additional O(n) "
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 " ,
2015-06-25 06:25:48 +08:00
" execution_count " : 2 ,
2015-05-01 05:45:08 +08:00
" metadata " : {
" collapsed " : false
} ,
" outputs " : [ ] ,
" source " : [
2016-08-13 18:42:05 +08:00
" class UniqueChars(object): \n " ,
" \n " ,
" def has_unique_chars(self, string): \n " ,
" if string is None: \n " ,
2015-05-01 05:45:08 +08:00
" return False \n " ,
2016-08-13 18:42:05 +08:00
" chars_set = set() \n " ,
" for char in string: \n " ,
" if char in chars_set: \n " ,
" return False \n " ,
" else: \n " ,
" chars_set.add(char) \n " ,
" return True "
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 " ,
2017-05-16 12:30:12 +08:00
" * Excluding the current character from the scan is rather tricky in Python and results in a non-Pythonic solution \n " ,
2015-05-03 05:14:29 +08:00
" * If there is a match, return False \n " ,
" * Return True \n " ,
" \n " ,
" Algorithm Complexity: \n " ,
" * Time: O(n^2) \n " ,
2015-07-05 21:24:33 +08:00
" * Space: O(1) "
2015-05-03 05:14:29 +08:00
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Code: In-Place "
2015-05-01 05:45:08 +08:00
]
} ,
{
" cell_type " : " code " ,
2015-06-25 06:25:48 +08:00
" execution_count " : 3 ,
2015-05-01 05:45:08 +08:00
" metadata " : {
" collapsed " : false
} ,
" outputs " : [ ] ,
" source " : [
2016-08-13 18:42:05 +08:00
" class UniqueCharsInPlace(object): \n " ,
" \n " ,
" def has_unique_chars(self, string): \n " ,
" if string is None: \n " ,
2015-05-03 05:14:29 +08:00
" return False \n " ,
2016-08-13 18:42:05 +08:00
" for char in string: \n " ,
" if string.count(char) > 1: \n " ,
" return False \n " ,
" return True "
2015-06-25 06:25:48 +08:00
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Unit Test "
]
} ,
{
" cell_type " : " code " ,
" execution_count " : 4 ,
" metadata " : {
" collapsed " : false
} ,
" outputs " : [
{
" name " : " stdout " ,
" output_type " : " stream " ,
" text " : [
2015-07-04 07:57:24 +08:00
" Overwriting test_unique_chars.py \n "
2015-06-25 06:25:48 +08:00
]
}
] ,
" source " : [
2015-07-04 07:57:24 +08:00
" %% writefile test_unique_chars.py \n " ,
2015-06-25 06:25:48 +08:00
" from nose.tools import assert_equal \n " ,
2015-06-20 11:07:35 +08:00
" \n " ,
2015-07-04 07:57:24 +08:00
" \n " ,
" class TestUniqueChars(object): \n " ,
" \n " ,
2015-07-12 03:34:14 +08:00
" def test_unique_chars(self, func): \n " ,
2016-06-13 11:19:09 +08:00
" assert_equal(func(None), False) \n " ,
2015-06-25 06:25:48 +08:00
" assert_equal(func( ' ' ), True) \n " ,
" assert_equal(func( ' foo ' ), False) \n " ,
" assert_equal(func( ' bar ' ), True) \n " ,
" print( ' Success: test_unique_chars ' ) \n " ,
" \n " ,
2015-07-12 03:34:14 +08:00
" \n " ,
2015-07-04 07:57:24 +08:00
" def main(): \n " ,
" test = TestUniqueChars() \n " ,
2016-08-13 18:42:05 +08:00
" unique_chars = UniqueChars() \n " ,
" test.test_unique_chars(unique_chars.has_unique_chars) \n " ,
2015-07-04 07:57:24 +08:00
" try: \n " ,
2016-08-13 18:42:05 +08:00
" unique_chars_set = UniqueCharsSet() \n " ,
" test.test_unique_chars(unique_chars_set.has_unique_chars) \n " ,
" unique_chars_in_place = UniqueCharsInPlace() \n " ,
" test.test_unique_chars(unique_chars_in_place.has_unique_chars) \n " ,
2015-07-04 07:57:24 +08:00
" except NameError: \n " ,
" # Alternate solutions are only defined \n " ,
" # in the solutions file \n " ,
" pass \n " ,
2015-07-12 03:34:14 +08:00
" \n " ,
" \n " ,
2015-07-04 07:57:24 +08:00
" if __name__ == ' __main__ ' : \n " ,
" main() "
]
} ,
{
" cell_type " : " code " ,
" execution_count " : 5 ,
" metadata " : {
" collapsed " : false
} ,
" outputs " : [
{
" name " : " stdout " ,
" output_type " : " stream " ,
" text " : [
" Success: test_unique_chars \n " ,
" Success: test_unique_chars \n " ,
" Success: test_unique_chars \n "
]
}
] ,
" source " : [
" %r un -i test_unique_chars.py "
2015-05-01 05:45:08 +08:00
]
}
] ,
" metadata " : {
" kernelspec " : {
2016-06-13 11:19:09 +08:00
" display_name " : " Python 3 " ,
2015-05-01 05:45:08 +08:00
" language " : " python " ,
2016-06-13 11:19:09 +08:00
" name " : " python3 "
2015-05-01 05:45:08 +08:00
} ,
" language_info " : {
" codemirror_mode " : {
" name " : " ipython " ,
2016-06-13 11:19:09 +08:00
" version " : 3
2015-05-01 05:45:08 +08:00
} ,
" file_extension " : " .py " ,
" mimetype " : " text/x-python " ,
" name " : " python " ,
" nbconvert_exporter " : " python " ,
2016-06-13 11:19:09 +08:00
" pygments_lexer " : " ipython3 " ,
" version " : " 3.5.0 "
2015-05-01 05:45:08 +08:00
}
} ,
" nbformat " : 4 ,
" nbformat_minor " : 0
}