From 80d4626ada33c7f717610871dea28f859c3aaf6d Mon Sep 17 00:00:00 2001 From: Cameron Yick Date: Mon, 26 Dec 2016 13:15:54 -0500 Subject: [PATCH 1/2] Optimize all-unique-characters.py Was thinking about this problem recently, so added a small constraint that avoids checking strings that must have duplicates by virtue of the fact that they are too long. Alternately, if it would be best to code up a separate solution in a different file, I could do that too. --- solutions/python/all-unique-characters.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/solutions/python/all-unique-characters.py b/solutions/python/all-unique-characters.py index 9224d9b..5e14bf4 100644 --- a/solutions/python/all-unique-characters.py +++ b/solutions/python/all-unique-characters.py @@ -3,8 +3,14 @@ import unittest def all_unique(string): + + # Optimization that assumes only 255 ASCII characters are possible (ignores possibility of unicode) + nLetters = len(letters) + if (nLetters > 255): + return False + letters = sorted(string) - for i in range(len(letters) - 1): + for i in range(nLetters - 1): if letters[i] == letters[i + 1]: return False return True From 58e6d425da4b160d1b7efb5082a9379bbdfd589c Mon Sep 17 00:00:00 2001 From: Cameron Yick Date: Thu, 9 Feb 2017 19:52:59 -0500 Subject: [PATCH 2/2] Set correct number of ASCII characters There are 256, not 255 characters The classic CS off-by-one error strikes again --- solutions/python/all-unique-characters.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/solutions/python/all-unique-characters.py b/solutions/python/all-unique-characters.py index 5e14bf4..b7b96fc 100644 --- a/solutions/python/all-unique-characters.py +++ b/solutions/python/all-unique-characters.py @@ -4,9 +4,9 @@ def all_unique(string): - # Optimization that assumes only 255 ASCII characters are possible (ignores possibility of unicode) + # Optimization that assumes only 256 (2^8) ASCII characters are possible (ignores possibility of unicode) nLetters = len(letters) - if (nLetters > 255): + if (nLetters > 256): return False letters = sorted(string)