Skip to content

Commit 35831a8

Browse files
authored
Merge pull request #3 from lomeli-noe/master
python solution to 1.1 and 1.2
2 parents ea93c8f + 3eded17 commit 35831a8

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Implement an algorithm to determine if a string has all unique charactersself.
2+
# What if you cannot use additional data structures.
3+
4+
# For this problem I will assume we are working ASCII characters for which there
5+
# are 128 unique values.
6+
7+
import array
8+
9+
def isUnique(str):
10+
11+
# if the string is longer than 128 characters, it means there are duplicates
12+
if len(str) > 128:
13+
return false
14+
15+
# create an array to map the values
16+
mask = array.array('i',(0,)*128)
17+
18+
for i in str:
19+
# the first time we find a character we change the value at that index
20+
# from 0 to 1
21+
if mask[ord(i)] == 0:
22+
mask[ord(i)] = 1
23+
# if the value at that index is already 1, it means this is a duplicate
24+
else:
25+
return False
26+
return True
27+
28+
29+
print("%s" % (isUnique("test")))
30+
print("%s" % (isUnique("abc123")))
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Given two strings, write a method to decide if one is a permutation of the other
2+
3+
def isPermutation(str1, str2):
4+
if len(str1) != len(str2):
5+
return False
6+
7+
sort1 = sorted(str1)
8+
sort2 = sorted(str2)
9+
10+
return sort1 == sort2
11+
12+
print("%s" % (isPermutation("stop", "pots")))
13+
print("%s" % (isPermutation("stops", "ponds")))

0 commit comments

Comments
 (0)