File tree Expand file tree Collapse file tree 2 files changed +60
-0
lines changed Expand file tree Collapse file tree 2 files changed +60
-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
+
5
+ def isUnique (string_ ):
6
+ for i in range (len (string_ )):
7
+ i = 0
8
+ for j in range (1 ,len (string_ )):
9
+ if string_ [i ] == string_ [j ]:
10
+ return False
11
+ return True
12
+
13
+ print (isUnique ("GeeksforGeeks" ))
14
+ #This soluation is O(n^2)
15
+
16
+ #let's see if we can go faster
17
+
18
+ def isUnique_ (string_ ):
19
+ d = {}
20
+ for i in string_ :
21
+ if i in d :
22
+ d [i ] += 1
23
+
24
+ else :
25
+ d [i ] = 1
26
+ for keys ,value in d .items ():
27
+ if value > 1 :
28
+ return False
29
+ return True
30
+
31
+ print (isUnique_ ("camilo" ))
Original file line number Diff line number Diff line change
1
+ def perm_check (str_1 ,str_2 ):
2
+ if len (str_1 ) != len (str_2 ):#If both str are diiferent lenghts return false
3
+ return False
4
+ d = {}
5
+ for i in str_1 : #takes all the letter in string one and add them to the hash map
6
+ if i in d : #counts the number of letter
7
+ d [i ] += 1
8
+ else :
9
+ d [i ] = 1
10
+
11
+ for j in str_2 : #
12
+ if j in d :#Looks at the letter in the string and subtract the number
13
+ #of the same letter from the value
14
+ d [j ] -= 1
15
+ else :
16
+ d [j ] = 1
17
+
18
+ for keys ,value in d .items ():
19
+ if value > 0 :
20
+ return False
21
+ return True
22
+
23
+
24
+ #O(n) sol?
25
+ print (perm_check ("camilo" ,"pop" )) #False
26
+
27
+ print (perm_check ("camilo" ,"camplo" )) #false
28
+
29
+ print (perm_check ("camilo" ,"olimac" )) #True
You can’t perform that action at this time.
0 commit comments