File tree Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Original file line number Diff line number Diff line change
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" )))
Original file line number Diff line number Diff line change
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" )))
You can’t perform that action at this time.
0 commit comments