|
3 | 3 | What if you can not use additional data structures?
|
4 | 4 | '''
|
5 | 5 |
|
6 |
| -'''Algorithm Design |
| 6 | +''' |
7 | 7 | Confusions:
|
8 | 8 | 1. What do you mean by "all unique characters"? I assume it is ACSII string with
|
9 | 9 | value 0~255.
|
10 | 10 | 2. What do you mean by "cannot use addtional data structures"? I assume I can
|
11 | 11 | only use string.
|
| 12 | +3. Does the string need to include all 256 characters? Or just need to have no |
| 13 | +duplicates. (assume the later one) |
12 | 14 | Examples:
|
13 | 15 | 1) agshdhfkd -> False
|
14 | 16 | 2) agsdhfk -> True
|
15 |
| -Design: |
16 |
| - Create a hash function to map all unique characters |
17 | 17 | '''
|
18 | 18 |
|
19 |
| -def hash_function(char): |
20 |
| - return ord(char) |
21 |
| - |
| 19 | +# Idea: hash table |
22 | 20 | # time: O(n)
|
23 | 21 | # space: O(1) - fixed size 256
|
24 |
| -def all_unique_string(string): |
| 22 | +def all_unique_character_1(string): |
25 | 23 | table = [0 for x in range(256)]
|
26 | 24 | for char in string:
|
27 |
| - index = hash_function(char) |
| 25 | + # ord returns the integer ordinal of a one-character string |
| 26 | + index = ord(char) |
28 | 27 | if table[index] == 0:
|
29 | 28 | table[index] = 1
|
30 | 29 | else:
|
31 | 30 | return False
|
32 | 31 | return True
|
33 | 32 |
|
34 |
| -if __name__ == '__main__': |
35 |
| - print all_unique_string('agshdhfkd') |
36 |
| - print all_unique_string('agsdhfk') |
| 33 | +# Idea: hash table |
| 34 | +# time: ? |
| 35 | +# space: O(1) |
| 36 | +def all_unique_character_2(string): |
| 37 | + table = [] |
| 38 | + for char in string: |
| 39 | + if char in table: |
| 40 | + return False |
| 41 | + table.append(char) |
| 42 | + return True |
37 | 43 |
|
| 44 | +# Idea: use bit vector, since Mac OS is in 64 bit, we need 4 vectors |
| 45 | +# time: O(n) |
| 46 | +# space: O(1) |
| 47 | +def all_unique_character_3(string): |
| 48 | + checker_1 = checker_2 = checker_3 = checker_4 = 0 |
| 49 | + for char in string: |
| 50 | + order = ord(char) |
| 51 | + if order < 64: |
| 52 | + if (checker_1 & (1<<order)) != 0: |
| 53 | + return False |
| 54 | + checker_1 |= 1<<order |
| 55 | + elif order < 128: |
| 56 | + order -= 64 |
| 57 | + if (checker_2 & (1<<order)) != 0: |
| 58 | + return False |
| 59 | + checker_2 |= 1<<order |
| 60 | + elif order < 192: |
| 61 | + order -= 128 |
| 62 | + if (checker_3 & (1<<order)) != 0: |
| 63 | + return False |
| 64 | + checker_3 |= 1<<order |
| 65 | + else: |
| 66 | + order -= 192 |
| 67 | + if (checker_4 & (1<<order)) != 0: |
| 68 | + return False |
| 69 | + checker_4 |= 1<<order |
| 70 | + return True |
| 71 | + |
| 72 | + |
| 73 | +''' Below are Unit Tests ''' |
| 74 | + |
| 75 | +def unit_test_1(func): |
| 76 | + input_string = 'agshdhfkd' |
| 77 | + expected = False |
| 78 | + if func(input_string) == expected: |
| 79 | + print "Pass: Unit Test 1" |
| 80 | + else: |
| 81 | + print "Fail: Unit Test 1" |
38 | 82 |
|
| 83 | +def unit_test_2(func): |
| 84 | + input_string = 'agsdhfk' |
| 85 | + expected = True |
| 86 | + if func(input_string) == expected: |
| 87 | + print "Pass: Unit Test 2" |
| 88 | + else: |
| 89 | + print "Fail: Unit Test 2" |
39 | 90 |
|
| 91 | +def unit_test_3(func): |
| 92 | + input_string = 'agsdhfkA123BGW' |
| 93 | + expected = True |
| 94 | + if func(input_string) == expected: |
| 95 | + print "Pass: Unit Test 3" |
| 96 | + else: |
| 97 | + print "Fail: Unit Test 3" |
40 | 98 |
|
| 99 | +def unit_test_4(func): |
| 100 | + input_string = 'agsdhfkA123BGW278' |
| 101 | + expected = False |
| 102 | + if func(input_string) == expected: |
| 103 | + print "Pass: Unit Test 4" |
| 104 | + else: |
| 105 | + print "Fail: Unit Test 4" |
| 106 | + |
| 107 | +def run_unit_tests(func): |
| 108 | + unit_test_1(func) |
| 109 | + unit_test_2(func) |
| 110 | + unit_test_3(func) |
| 111 | + unit_test_4(func) |
| 112 | + |
| 113 | +if __name__ == '__main__': |
| 114 | + run_unit_tests(all_unique_character_1) |
| 115 | + run_unit_tests(all_unique_character_2) |
| 116 | + run_unit_tests(all_unique_character_3) |
0 commit comments