Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3acb1dd

Browse files
committed
day9:python uniquechars
1 parent 9b51a94 commit 3acb1dd

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

‎README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
| Current Status| Stats |
88
| :------------: | :----------: |
99
| Total C++ Problems | 188 |
10-
| Total Python Problems | 13 |
11-
| Current Daily Streak| 8 |
10+
| Total Python Problems | 14 |
11+
| Current Daily Streak| 9 |
1212
| Last Streak | 06/20/2019 - 06/21/2019|
13-
| Current Streak | 06/23/2019 - 06/30/2019|
13+
| Current Streak | 06/23/2019 - 07/01/2019|
1414

1515
</center>
1616

@@ -82,7 +82,7 @@ Include contains single header implementation of data structures and some algori
8282
### Cracking the coding interview problems
8383
| Problem | Solution |
8484
| :------------ | :----------: |
85-
| Problem 1-1 : Edition 6: Write an algorithm to determine whether a string has unique characters or not. Can we do it without using additional data structures? | [1-1-hasUniqueChars.cpp](cracking_the_coding_interview_problems/1-1-hasUniqueChars.cpp)|
85+
| Problem 1-1 : Edition 6: Write an algorithm to determine whether a string has unique characters or not. Can we do it without using additional data structures? | [1-1-hasUniqueChars.cpp](cracking_the_coding_interview_problems/1-1-hasUniqueChars.cpp), [1-1-hasUniqueChars.py](cracking_the_coding_interview_problems/1-1-hasUniqueChars.py)|
8686
| Problem 1-2 : Edition 5: Reverse a string when you are a pass a null terminated C string.|[1-2-edi5-reverseString.cpp ](cracking_the_coding_interview_problems/1-2-edi5-reverseString.cpp)|
8787
| Problem 1-2 : Edition 6: Given two strings, determine if one is permutation of other.|[1-2-perm-strings.cpp](cracking_the_coding_interview_problems/1-2-perm-strings.cpp)|
8888
| Problem 1-3 : Edition 5: Write an algorithm to remove duplicate chars from a string.|[1-3-edi5-removeDuplicates.cpp](cracking_the_coding_interview_problems/1-3-edi5-removeDuplicates.cpp)|
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
Cracking the coding interview, edition 6
3+
Problem 1.1
4+
Write an algorithm to determine whether a string has unique characters or not
5+
Can we do it without using additional data structures?
6+
"""
7+
8+
def is_unique(string):
9+
""" Checks if string has unique characters.
10+
Args:
11+
string: the string which needs to be checked for unique characters.
12+
This string only contains a-z lowercase (26 chars).
13+
'check' is 32-bit integer, 1 bit per alphabet.
14+
Returns:
15+
True if string has unique characters False otherwise
16+
17+
"""
18+
check = 0
19+
for char in string:
20+
c = ord(char) - ord('a')
21+
if check & (1 << c) :
22+
return False
23+
check |= (1 << c)
24+
return True
25+
26+
def is_unique2(string):
27+
""" Checks if string has unique characters
28+
Args:
29+
string: ASCII string which needs to be checked for unique characters
30+
Returns:
31+
True if string has unique characters False otherwise.
32+
"""
33+
check = [0 for _ in range(8)]
34+
for char in string:
35+
v = ord(char)
36+
# find int bucket which will represent this char
37+
idx = int(v / 32)
38+
# find the bit in this int bucket which will represent this char
39+
shift = v % 32
40+
41+
if check[idx] & (1 << shift):
42+
return False
43+
check[idx] |= (1 << shift)
44+
45+
return True
46+
47+
48+
if __name__ == "__main__":
49+
str1 = "hello"
50+
print(F"'{str1}' has unique characters? :", is_unique(str1))
51+
str2 = "norepat"
52+
print(F"'{str2}' has unique characters? :", is_unique2(str2))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /